PHP

Auto-refreshing dynamic content using AJAX and PHP

No GoodNeed ImprovementOKGoodExcellent (1 votes, average: 5 out of 5)
Loading ... Loading ...

The following script will use PHP and Ajax (XMLHTTPRequest) to retrieve current server time in every 10 seconds and then insert the date/time into the html page.

1, html file: time.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Auto-refreshing dynamic content using AJAX and PHP</title>
<script type=”text/javascript” src=”time.js“></script>
</head>

<body onload=”ajax(page,’scriptoutput‘)”>
<p>Current Server date & time (updated every 10 seconds):<br />
<span id=”scriptoutput“></span></p>
</body>
</html>

2, Ajax file: time.js

var page =“/time.php?time=”+date.getTime();
function ajax(url,target)
{
// native XMLHttpRequest object
document.getElementById(target).innerHTML = ’sending…’;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {ajaxDone(target);};
req.open(”GET”, url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject(”Microsoft.XMLHTTP”);
if (req) {
req.onreadystatechange = function() {ajaxDone(target);};
req.open(”GET”, url, true);
req.send();
}
}
setTimeout(”ajax(page,’scriptoutput‘)”, 10000); //10 seconds
}

function ajaxDone(target) {
// only if req is “loaded”
if (req.readyState == 4) {
// only if “OK”
if (req.status == 200 || req.status == 304) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML=”ajax error:\n” +
req.statusText;
}
}
}

3, PHP file: time.php

<?php
putenv(’TZ=America/New_York’);
echo date(”Y-m-d h:i:s A”, time());
?>

4, Save and upload these files to your server, open time.html and you will see the html page refreshes it’s time every 10 seconds.

Ref: http://www.openhosting.co.uk/articles/webdev/6004/

Popularity: 2%

Comments (1)

PHP function to display current year

No GoodNeed ImprovementOKGoodExcellent (4 votes, average: 5 out of 5)
Loading ... Loading ...

If your web site has copyright footer and you want it to display current year, you can use PHP date function to make year current. For example, the follow lines of code will display owner’s copyright notice from 2002 to current year (2007):

<STYLE TYPE="text/css" MEDIA=screen>
<!–
.Footnote
{
font-family: Arial, Helvetica, Sans-Serif;
font-size: 11px;
color: #999999;
}
–>
</STYLE>
<p align=center><font class=”Footnote”>© 2002-<?php echo date(’Y'); ?> Web Designed by
<a href=”http://www.innovitainc.com“>
<img src=”/images/InnoVitalogo1.jpg” valign=”TOP” border=”0″ height=”14″ width=”18″><b>
<i><font color=”red”>Inno</font><font color=”green”>Vita</font></i><font color=”blue”>
Inc.</font></b>.</a> All rights reserved.</font></p>

Popularity: 6%

Comments (1)

Set Up Self-Signed SSL Certificate for WordPress

No GoodNeed ImprovementOKGoodExcellent (1 votes, average: 5 out of 5)
Loading ... Loading ...

You can use self-signed SSL certificate for WordPress admin.

1. Home-made Self-Signed SSL Certificate

1.1, Use this command to generate a private key.

openssl genrsa -des3 -out www.my-whiteboard.com.key 1024

Generating RSA private key, 1024 bit long modulus
..++++++
…….++++++
e is 65537 (0×10001)
Enter pass phrase for www.my-whiteboard.com.key:
Verifying - Enter pass phrase for www.my-whiteboard.com.key:

1.2 Generate a CSR (Certificate Signing Request)

openssl req -new -key www.my-whiteboard.com.key -out www.my-whiteboard.com.csr

Enter pass phrase for www.my-whiteboard.com.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—-
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:Maryland
Locality Name (eg, city) []:Rockville
Organization Name (eg, company) [Internet Widgits Pty Ltd]:www.my-whiteboard.com
Organizational Unit Name (eg, section) []:IT Services
Common Name (eg, YOUR name) []:www.my-whiteboard.com
Email Address []:xxx@gmail.comPlease enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:my-whiteboard.com

1.3 Remove Passphrase from Key

openssl rsa -in www.my-whiteboard.com.key_orig -out www.my-whiteboard.com.key

Enter pass phrase for www.my-whiteboard.com.key_orig:
writing RSA key

1.4, Create a self-signed certificate

openssl x509 -req -days 3650 -in www.my-whiteboard.com.csr -signkey www.my-whiteboard.com.key -out www.my-whiteboard.com.crt

Signature ok
subject=/C=US/ST=Maryland/L=Gaithersburg/O=www.my-whiteboard.com/OU=IT Services/CN=www.my-whiteboard.com/emailAddress=xxx@gmail.com
Getting Private key

1.5 Edit your Apache server’s ssl.conf file, add the following lines for your web site (virtualhost configuration).

<VirtualHost 100.101.102.103:443>
ServerName www.my-whiteboard.com
DocumentRoot /home/xxx/webapps
SuexecUserGroup root root
Options Indexes Includes FollowSymLinks
ServerAlias “mail.my-whiteboard.com” “ftp.my-whiteboard.com”
ServerAdmin “xxxx@gmail.com”
DirectoryIndex index.html index.php index.jspErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warnSSLEngine on
SSLCertificateFile /home/xxx/www.my-whiteboard.com.crt
SSLCertificateKeyFile /home/xxx/www.my-whiteboard.com.key
SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown <Directory “/home/xxx/webapps”>
Options Indexes Includes FollowSymLinks
DirectoryIndex index.html index.php index.jsp
#Options +Includes
#AllowOverride None
AllowOverride All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</Directory></VirtualHost>

1.6, Restart Apache

apachectl restart

ps -ef |grep httpd

apache 4345 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4346 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4347 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4348 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4349 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4350 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4351 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd
apache 4352 3197 0 09:32 ? 00:00:00 /usr/sbin/httpd

2. Install WordPress SSL Plugin for Secure-Admin

2.1, Download WordPress SSL Plugin for Secure-Admin from http://haris.tv/.

2.2, Extract the zip file and copy secure-admin.php to the /wp-content/plugins/ directory.

2.3, Log into the WordPress admin panel and click on the Plugins button

2.4, Locate the secure-admin listing and click Activate

3. Test ssl web connection

https://www.my-whiteboard.com

Because the certificate was not generated from Trusted CA root certificates and does not offer any third-party proof to validate your site’s identity, most browsers will show warning/error message similar to these.

Popularity: 2%

Comments