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.

Leave a Reply

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

Most Popular Posts

What are Traits in PHP

Posted on 2 years ago

Bhumi

Welcome to Creative Dev

Posted on 6 years ago

Bhumi

How to access Remote File using PHP

Posted on 5 years ago

Bhumi

how you can add Voice Search in WordPress

Posted on 5 years ago

Bhumi

Overview of ECMAScript 6 new Features

Updated 18 years ago

Bhumi

Getting started with Amazon Web Services

Updated 18 years ago

Bhumi

Facade implementation in Laravel

Updated 18 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi