Sometimes working with sessions can be hard. But in fact all of those hardness comes from an unproper php.ini setup.

In this entry i’ll discuss about common session problems and of course the solutions.

Problem : You’re trying to use sessions on your PHP project. But whenever you refresh a page or whenever you click a link on your pages you see that there was a new session file created on PHP’s session directory. Additionally you check the client for cookies (=session id’s) but the client has no cookie.

Solution : To solve this problem open php.ini file, search for session.use_cookies and set this param’s value to 1


Problem : You installed PHP and Apache together on your system. Now you try to use sessions for the first time. But you can’t.

Solution : By default the param called session.save_path is not proper on most systems/installations. To solve this problem you need to open php.ini file, search for session.save_path and set this param’s value to a proper directory. The directory is that, where you want to save the session files generated by PHP.

Problem : Everything goes fine on sessions. But when you close your browser sessions expires.

Solution : By default sessions expires at browser close. To solve this problem open php.ini, search for session.cookie_lifetime and additionally search for session.gc_maxlifetime and set their value to how much seconds you want your sessions to be saved by client.

Another way can be like this : If you have rights to use ini_set function from code you can write these codes at the top of you code before starting the session using session_start function or almost before using ob_start function :

ini_set("session.cookie_lifetime",(60*60*24*365));
ini_set("session.gc_maxlifetime",(60*60*24*365));

This will theorically set the session life time to maximum (=1 year)

Tags: , , ,
Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>