Archive for May, 2008

.htaccess Password Protection

Friday, May 30th, 2008

securelock.jpg

Even without using mySQl, users can still password protect their files through .htaccess password protection.

The code can be divided into 3 if-else statements:

1. If the user has not been validated, use the PHP header and request for a username and password.

2. Else, if the user’s name is “webworld” and the password is “webworld”, log in. Here, all the code for the user will be placed.

3. Else say the user/password failed.

//1st part
if (!isset($PHP_AUTH_USER))
{
header("WWW-Authenticate: Basic realm=\"webworld Password.\"");
Header("HTTP/1.0 401 Unauthorized");
exit;
}

//2nd part
else if(($PHP_AUTH_USER=="webworld") && ($PHP_AUTH_PW=="webworld"))
{
echo "You are logged in...";
//put the code for the whole user page in here
//you can also create a redirect to the user page if you want
}

//3rd part
else
{
echo "Failed entry”;
//fail try again
}
?>

Remember that the username and password will remain for the whole session as long as explorer windows is open.

Displaying Page Loading Time (Steps and Sample Code)

Monday, May 26th, 2008

stopwatch.gif

Here is how to display your page’s loading time:

1. Use the function microtime() to get the time in micro-seconds
2. Use the explode() function to turn the micro-time into an array.
3. Combine the two parts to the array (the micro-seconds to the seconds).
4. Repeat steps 1,2 and 3 for the bottom of the page
5. Take the time taken at the end of the page from the time taken at the top of the page to determine the total loading time.
6. After rounding the microtime, return it to the browser.

At the top of your page, place:

$m_time = explode(" ",microtime());
$m_time = $m_time[0] + $m_time[1];
$starttime = $m_time;
?>

At the bottom of your page, place:

$round = 3;// The number of decimal places to round the micro time to.
$m_time = explode(" ",microtime());
$m_time = $m_time[0] + $m_time[1];
$endtime = $m_time;
$totaltime = ($endtime - $starttime);
echo "Page loading took:". round($totaltime,$round) ." seconds";
?>

Creating a Simple User Log (Sample Code)

Friday, May 23rd, 2008

wwwglobe.jpg

//use the date() function <----- comment
$time = date("F jS Y, h:iA");

//use PHP variable $remote_addr to get ip address
$ip = $REMOTE_ADDR;

//use PHP variable $http_referer to get referer
$referer = $HTTP_REFERER;

//user PHP variable $http_user_agent to get browser
$browser = $HTTP_USER_AGENT;

//what page they came from
$page = $_SERVER['REQUEST_URI'];

//use the fopen() function
$fp = fopen("log.html", "a");

//use the fputs() function
fputs($fp, "
Time: $time
IP: $ip
Referer: $referer
Browser: $browser
Page: $page
");

fclose($fp);

?>

Note: Don’t forget to create a blank log.html page, upload it to the server in the same directory as the page where the code will be placed, and CHMOD it to 777. Also, you can put this code anywhere on your main page.

List of PHP Accelerators (Part2)

Monday, May 12th, 2008

zend-optimizer-phpinfo.png

4. Zend Optimizer – is a closed source solution which is purely a code optimizer with no code caching feature. This makes Zend Optimizer different from other PHP accelerators. Also, Zend Optimizer is very useful in running some types of protected scripts. However, one major setback is that the time taken to optimize scripts may exceed the performance gains, hence, resulting to an overall slowdown.

5. Zend Platform – originally Zend Cache and then Zend Accelerator, this commercial product was, at first, sold for $995.00 and $1495.00 US dollars per server. In 2006, this pricing was removed from Zend.com.

6. ionCube PHP Accelerator (PHPA)– is claimed to be the first freely available PHP performance solution to rival the peformance of the commercial Zend Cache product. It was launched in 2001 and was intended to maximize the performance potential of PHP. However, PHPA is now discontinued, although there is an announcement regarding the development of a more advanced replacement.

7. Turck MMCache – though discontinued, Turck MMCache is still widely used today.

List of PHP Accelerators (Part1)

Friday, May 9th, 2008

xcache.jpg

A PHP accelerator is an extension created to improve the performance of software applications written using the PHP programming language. Here is a list of PHP accelerators:

1. Alternative PHP Cache – a free and open framework used for caching and optimizing PHP intermediate code. The latest stable version is 3.0.18. It works with all PHP versions including PHP5.2.

2. eAccelerator – Originally part of the Turck MMCache project, eAccelerator used to have a PHP encoder and loader but was removed after December 2006. Its latest stable version is 0.9.5.3 and it supports PHP4 and all PHP5 releases including 5.2.

3. Xcache – is a powerful and stable PHP opcode cacher which can run on production servers under high load. It has been also tested on linux and supports ThreadSafe/Windows. Its latest stable version is 1.2.2 and supports PHP_5_1 PHP_5_2 HEAD (6.x)

A List of PHP Editors

Monday, May 5th, 2008

20070523_appz_0002.jpg

In case this may come in handy, here is a list of editors which can be used with the PHP scripting language.

Windows

ConTEXT – version 0.97.4; freeware
Crimson Editor – version 3.60; freeware
Programmer’s Notepad –
PSPad – version 4.3.0; freeware
Notepad++
NuSphere PhpED – version 5.0; commercial
NuSphere Nu-Coder – version 1.4; commercial

Mac OS X

skEdit
Smultron – version 1.0.1; freeware
TextWrangler
TextMate
Coda

Linux

Bluefish – version 1.0; other
Geany
gedit
gPHPEdit
Kate – version 2.2; freeware
Quanta Plus – version 3.2.1; freeware

Multiple OS

NetBeans
Eclipse
Aptana
Emacs – version 21; freeware
jEdit – version 4.1; freeware
SciTE – version 1.53; freeware
Vim – version 6.1; freeware
nano