Saturday, June 28, 2008

PHP Session functions and notes

like any other web server-side scripts, PHP has its Session functions and methods. Every time a user opens your website, you consider it a session and when the user is inactive for sometime, the session is gone.

Also, when you want to pass parameters from one page to another, you might notice normal variables will not stay when loaded another page. However, you can declare (array)$_SESSION variables to keep the variables in the session and you can use it on any other page.

The only condition is that, you must start the session using function void session_start(void) at the start of PHP scripts that you want to use Session.

When your domain (eg uses subdomain (eg, using the normal session_start() is not enough. The session from your domain will be different from your sub domain.

So, you can add the following in front of session_start() to configure this: session_set_cookie_params(t, '/',dn,false,true); - where t represents the timeout before the session expires, '/' represents that the session can be used from the whole domain, and dn should be a .<yourdomainname>.com where you add a . in front of your domain name without the "www"

Also, during my own testing experience, i noticed that if you put localhost in the "dn", the session will not work on any browsers and PHP will keep renewing the session, which may result in undesirable returns.

Tuesday, June 17, 2008

PHP script include() function

One of the things I regard best as a developer, is the include() function of PHP.

include() is something like the MasterPages and ContentPages in ASP.NET. You can use it for defining site-wide variables and constants, defining functions and classes, or even putting site-wide processes and functions into it and call it from anywhere else.

For example, i need to display "hello world" on both "a.php" and "b.php" pages. I can use the include() function to do this. first, i put
<?php echo "hello world"; ?>
into "head.php", then i put
<?php include('head.php'); ?>
into both pages "a.php" and "b.php". Then, both pages "a.php" and "b.php" will show up "hello world".

both pages will run "head.php" at the line where include() function is. However, there are some unexpected results of using include().

For example, i put "head.php" into a folder call "folder", and i have this script "call.php" which have the following
<?php include('folder/head.php'); ?>
The current directory for "head.php" is no longer 'folder/', but it is now '/', since it is called by "call.php". Included scripts will always use the current directory of the parent script. (See more: Directory Functions and chdir() function)

The return statement can be used in the main scope of included scripts to return the control back to the parent script immediately. example:
<?php return; $I++; ?>
variable $I will not be incremented. but if you run this code as parent main scope, return will not work.

Wednesday, June 4, 2008

Upgrading MySQL - with xampp installed first

Hello all!

We have noted that the internet requires an article on how to upgrade your MySQL server with xampp installed first. MySQL server on your computer are be shared among many HTTP servers - Apache or IIS - on the same operating system. Thus, there are possibilities you might want to upgrade or downgrade your MySQL server.

xampp by Apache Friends is a package that includes the Apache server, PHP and MySQL together with many other wonderful functions for local web development(localhost). However, the MySQL server included is never up to date. So below is how you upgrade your MySQL server.

1) Download MySQL server from
2) When you set up MySQL server, one of the steps of the installer should show an "Advanced" button, click on it.
3) Following you should see where the server is about to install to. Change it to %your_xampp_installation_path%\mysql\ (e.g. C:\xampp\mysql\)
4) After installation, your MySQL is upgraded.

Note that if you do not change the installation directory of MySQL server, the server will not be upgraded as the default server is still in your xampp folder. After upgrading, you can use MySQL Administrator (GUI Tools) or MyPHPAdmin to check the server version.

Note that you are advised to back up your database information before performing such upgrade or maintenance work. I will not be held responsible for any problems caused directly or indirectly by my articles or whatsoever.

Directory Size Calculations with Code Examples

Calculating files are easy. Using the php filesize() function, you can easily find out the file size. However, have you encountered a case whereby you need to calculate the whole directory, including sub folders and the files in them?

In this article, we are going to talk about how to recursively calculate the total folder size of a directory. You can add thousands of folders and millions of files into a single folder, and this customized php function is able to calculate out the size of the directory. You can also use it for calculating your disk space too!

function dirsize($dirname) {
if (!is_dir($dirname) || !is_readable($dirname)) {
// check whether the directory is valid.
return false;

$dirname_stack[] = $dirname;
$size = 0;

do {
$dirname = array_shift($dirname_stack);
$handle = opendir($dirname);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && is_readable($dirname . DIRECTORY_SEPARATOR . $file)) {
if (is_dir($dirname . DIRECTORY_SEPARATOR . $file)) {
$dirname_stack[] = $dirname . DIRECTORY_SEPARATOR . $file;
$size += filesize($dirname . DIRECTORY_SEPARATOR . $file);
} while (count($dirname_stack) > 0);

return $size;

Hope this helps you in building it in as a useful function in your administration panel of your website.

Link to code example on pastebin: