I have made a function to fetch data from database and show it in a HTML table. It's easy to use but hard to read. HTML text has been linked together.
function getResultAsTable ($actions, $result)
{
if ($rowcount =pg_num_rows($result)>0)
{
$resultHTML= "<table border="'1'" align="'center'">\n<tr bgcolor="'#e8eefa'">\n";
//Output the table header
$fieldCount = pg_num_fields($result);
for ($i=0; $i < $fieldCount; $i++)
{
$rowName = pg_field_name($result,$i);
$resultHTML .= "<th valign="'middle'">$rowName</th>\n";
}
$resultHTML .= "<th>Actions</th></tr>\n";
//output the table body
while ($row = pg_fetch_row($result))
{
$resultHTML .="\n";
for ($i=0; $i < $fieldCount; $i++)
{
$resultHTML .="<td>".htmlentities($row[$i])."</td>\n";
}
//Replace VALUE with proper primary key
$action = str_replace("VALUE", $row[0], $actions);
//Add action cell to the end of each row
$resultHTML .= "<td>$action</td>\n</tr>\n";
}
//Close table
$resultHTML .="</table>\n";
}
else
{
$resultHTML = "No Result Found";
}
return $resultHTML;
}
function pageLinks($totalpages, $currentpage, $pagesize, $parameter)
{
// Start at page one
$page = 1;
// Start at record 0
$recordstart = 0;
// Initialize page links
$pageLinks = '';
while ($page <= $totalpages) {
// Link the page if it isn't the current one
if ($page != $currentpage) {
$pageLinks .= "<a href="http://www.blogger.com/%5C"
parameter="$recordstart\">$page</a> ";
// If the current page, just list the number
}
else {
$pageLinks .= "$page ";
}
// Move to the next record delimiter
$recordstart += $pagesize;
$page++;
}
return $pageLinks;
}
Singleton pattern defines a class that hs only a single global instance. There are an abundance of places where a singleton is a natural choice. A browsing user has only a single set of cookies and has only one profile. Similarly, a class that wraps an HTTP request has only one instance per request. If you use a database driver that does not share connections, you might want to use a singleton to ensure that only a single connection is every open to a given database at a given time.
One successful method for implementing singletons in PHP5 is to use a factory method to create a singleton. The factory method keeps a private reference to the original instance of the class and returns that on request.
class Singleton {
private static $instance = false;
public $property;
private function __construct () {}
public static function genInstance ()
{
if (self :: $instance === false) {
self :: $instance=new Singleton;
}
return self :: $instance;
}
}
$a = Singleton :: getInstance()
$b = Singleton :: getInstance()
$a ->property = "Hello world";
print $b ->property;
//Amazing! The result will be "Hello world"//
In fact, if set the constructor methods private, you can only make instance inside the class, if you try to instantiate outside the class, you will get a fatal error.
We can also use CURL to post data to a form. The default method of sending form data with CURL is in GET, but in this example we will use POST to submit a search term to an imaginary form.
[php] // Setup a string with the form parameters in it
$strParameters = "query=dog";
// Initialize the CURL library
$cURL = curl_init($cURL);
// Set the URL to execute
curl_setopt($cCURL, CURLOPT_URL, "http://www.mywebserver.com/mysearch.php");
// Set options
curl_setopt($cCURL, CURLOPT_HEADER, 1);
curl_setopt($cCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_POST, 1);
curl_setopt($cCURL, CURLOPT_POSTFIELDS, $strParameters);
// Execute, saving results in a variable
$strPage = curl_exec();
// Close CURL resource
curl_close($cURL);
// This will print out the HTML contents
echo($strPage);
?>[/php]
It's lucky to have a sever on my own computer. So I get chance to know more about how to protect web server. It is not enough make strict privilege access to certain folders and files. One good way to protect communication between server and client is to assign a CA to client at the beginning of communication, and in this way, a unique encryption system can be set up every time client connect to server.
Normally, certification issuers, which are called Certificate Authority, are big security companies. Their CA sometimes play a more important role than a business certification, because this CA can not be modified or counterfeited. But in a cyber world, everybody can be an Authority, everybody can issue his own certification. Although this kind of CA cannot enjoy high prestige as commercial one. But it can still be very useful for encryption messages sent between server and client.
Here is a simple way to set up a HTTPS server on Ubuntu:
1. install Openssl and get root CA
sudo apt-get install openssl
/* setup a root CA, and make sure the password is complex enough */ openssl genrsa -des3 2048 > rootca.privatekey
Work out a self-certificate based on the root CA,change the valid time if needed.
openssl req -new -x509 -key rootca.privatekey -days 3650 -out rootca.cert
In the process,you will be asked to provide your information, including organization name and physical location and so on.
Open /etc/ssl/openssl.cnf modify the environment variable like this:
dir = /etc/ssl/CA
Set up several folders to reserve the private files: sudo mkdir -p /etc/ssl/CA/certs
sudo mkdir -p /etc/ssl/CA/newcerts
sudo touch /etc/ssl/CA/index.txt
sudo echo “01″ > /etc/ssl/CA/serial
copy the root CA to this folder
sudo cp rootca.privatekey /etc/ssl/CA/private/cakey.pem
sudo cp rootca.cer /etc/ssl/CA/cacert.pem
change privilege to deny all access but from root:
cd /etc/ssl
sudo chmod go-rwx CA -R
2. Set Up Certification for Apache SSL
Generate a user CA first, an the process is similar to set up a root CA:
openssl genrsa -des3 2048 > my.privatekey
Generate a key file based on the user CA:
openssl req -days 3650 -key my.privatekey -new -out my.csr
Private information needed,Orgnization Name and location must be the same with root CA. Common Name is your web site's FQDN(fully qualified domain name. For example www.gezhi.org,and note that www.gezhi.org and gezhi.org are different sites )
then use root CA to authorize the user certification we made for apache:
openssl ca -out my.pem -days 3650 -infiles my.csr
3. Config apache
Ubuntu apache2 intalled mod_ssl on default,so just enable the modulate: a2enmod ssl
/* combine my.privatekey and my.pem, copy it to /etc/apache2/ssl/apache.pem */
cat my.privatekey my.pem > apache.pem
sudo cp apache.pem /etc/apache2/ssl/apache.pem
Set up the virtual host in /etc/apache2/sites-enabled/youdomain.conf (or in /etc/apache2/apache2.conf), it must include following lines:
ServerName mydomain.com
ServerAdmin webmaster@localhost
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem
DocumentRoot /directory/to/mydomain/dir/
…
Add https listen port in /etc/ports.conf
restart the apache server:
sudo /etc/init.d/apache2 restart
when apache2 starts, it will ask for password for my.privatekey