Using session_name () in PHP - unable to get data

When I use:

session_name( 'fObj' );
session_start();
$_SESSION['foo'] = 'bar';

      

Subsequently page load and launch:

session_start();
print_r( $_SESSION );

      

doe does not return session data.

If I remove session_name (); it works great.

Does anyone know how to use sessions with a different session name?

UPDATE

If I run the above code as two pages load then change to:

session_name( 'fObj' );
session_start();
print_r( $_SESSION );

      

I can access the data. However, if it only works if I first load the page without the line:

session_name( 'fObj' );

      

+3


source to share


3 answers


In light of nwolybug's post, I think it must be due to some environment setting. I can get this to work by following these steps:



if( $_COOKIE['fObj'] )
{
    session_id( $_COOKIE['fObj'] );
    session_start();
}
var_dump( $_SESSION );

      

+1


source


John Robertson is right, the expression he mentioned is straight from the PHP manual ( http://php.net/manual/en/function.session-name.php ).

Your default session name comes from the php.ini variable 'session.name' and this is usually set to "PHPSESSID". On every startup time request (as mentioned), the session will be renamed to PHPSESSID unless you call session_name ('fObj') before session_start () on every page, so page1:

<?php
  session_name( 'fObj' );
  session_start();

  $_SESSION['foo'] = 'bar';

      



page 2:

<?php
  session_name( 'fObj' );
  session_start();

  print_r($_SESSION);

      

Subsequently you can go to php.ini settings and change the session.name variable from PHPSESSID to fObj and all your created sessions will have the session name fObj.

+4


source


I can get it to work fine and return SESSION data with the following code:

session_name( 'fObj' );
$_SESSION['foo'] = 'bar';

session_start();
print_r( $_SESSION );

      

If I run it with the second session_start (); It comes back with an error telling me that the session has already started. If you are in dev make sure to enable ERROR_ALL in php.ini. Remember to turn it off during production. Link to php error reporting functions .

Update: Also works with: session_name ('fObj'); $ _SESSION ['foo'] = 'bar';

print_r( $_SESSION );

      

Using Ubuntu 14.04 LTS, PHP 5.5.9-1 (let me know if you need more system information to resolve the issue)

+1


source







All Articles