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 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.

No comments: