9

I usually end up adding session_start() to the top of every page on my PHP sites (or in a header file which is in turn included on every page). I recently discovered that you can have sessions start automatically by using the following setting in php.ini:

session.auto_start = 1

What are the potential downsides (if any) of using this setting?

David Jones
  • 10,117
  • 28
  • 91
  • 139
  • possible duplicate of [Is setting php.ini's session.auto\_start to 1 considered bad practice?](http://stackoverflow.com/questions/8257083/is-setting-php-inis-session-auto-start-to-1-considered-bad-practice) – Pang Sep 25 '14 at 09:31

2 Answers2

6

If you turn on session.auto_start then the only way to put objects into your sessions is to load its class definition using auto_prepend_file in which you load the class definition else you will have to serialize() your object and unserialize() it afterwards. See.

Amade
  • 3,665
  • 2
  • 26
  • 54
Lion
  • 18,729
  • 22
  • 80
  • 110
  • So what you're saying is that if I use session.auto_start, then I won't be able to freely use $_SESSION variables? – David Jones Jul 16 '12 at 05:40
  • 1)if you use `session.auto_start` you'll not be able to use named sessions but since you have access to `php.ini` I dont think that will be much of a problem. 2)URL may look messy, if the client does not support cookies. – Lion Jul 16 '12 at 05:43
  • Also check [this](http://stackoverflow.com/questions/8257083/is-setting-php-inis-session-auto-start-to-1-considered-bad-practice) question. – Lion Jul 16 '12 at 05:50
0

Maybe this helps. It will create a session if there is no session created on page load.

if(!isset($_SESSION)): session_start();endif;

If you want to start a specific session, then use something like this:

if(!isset($_SESSION['your_session'])){             
            $data = array('default data');
            $_SESSION['your_session']=$data;
        }