Back to Top

How automatically start session in PHP

Here, I come with the quick and simple article.Session in PHP gives you the ability to record any number of variables and store them in between the execution of the request. You all know about the session and if you want to use session in PHP, you need to explicitly start or resume that session by using session function in PHP and you need to call the session_start() function for each PHP script.

After a session is started, you will have access of the session ID by the session_id() function in PHP. The session_id() function allows you to either set or retrieve a session ID.Automatically starting session in PHP So, You don’t need to start session explicitly through session_start ()

NOTE: if you get same session ID after reloading a page,cookie is enabled in user’s browser.

Sessions remain same as long as the web browser is active. When the user restarts the browser, the cookie is destroyed from the browser. You can change this by setting session.cookie_lifetime in your php.ini file. The default value is 0 and you can set an expiry period in seconds.

You need to explicitly start or resume that session unless you have changed your php.ini configuration file or by .htaccess. By default, sessions do not start automatically.

1) PHP.INI METHOD

If you want to start a session automatically, you will have to find the below line in your php.ini file and change the value from 0 to 1 and restart your server.

Here is the example to set the value of auto_start using configuration file in PHP.

By replacing or adding the value of session.auto_start to 1, you can check that a session is initiated for every PHP files.

2) .HTACCESS METHOD

Now if you don’t have an access to your php.ini file so you can auto start a session from the .htaccess file as well. just place the following line of code in your .htaccess file.

You may also like:

SESSION SAVE PATH in PHP
Session_start( ) :: At the top of the page
How to use Session in Joomla?

I hope you have enjoyed this article. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (1)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to create Object Copy in PHP

Posted on 12 years ago

Bhumi