thephpcode is where your PHP resources flow and you can find tips and tricks of PHP coding. Code snippets and examples are also provided. Best of PHP are included in thephpcode. Remember, if you have questions, do post a comment on the relevant post.

Saturday, June 28, 2008

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 http://www.johnsmith.com/) uses subdomain (eg http://sub.johnsmith.com/), 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

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

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 http://dev.mysql.com/downloads/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.

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!

Start Overflowing

Buy author coffee!

Write In/Requests

hello all! thank you for visiting thephpcode - where php codes are shared and explained. I really hope you like the every little tips and tricks i write in just for you.

In order to keep this going, I'd really love to hear from you. If you have any compliments or feedback write them to hellclanner at gmail dot com. OR if you have any requests for tutorials or walkthroughs for PHP codes, HTML, CSS, Javascript - do write them in.

Sponsored

thephpcode choice php help and guide website/blog that allows php developers to rapidly develop their applications with the use of guides from thephpcode. choice functions and hot news about php are all here. we also have code examples shown to help you understand php better. also we include portions of mysql, apache and other technologies that you might need on your php journey. cool huh?