0

After trawling through other posts, I could not find the answer. The problem is that when i create a custom session name, I am not able to access session variables on any other pages. How can I get this working with custom session variable?

Scenario A

Login page

after successful login, the following is called

function initiatenewsession($app, $userid){
    $session_name = getuniquesessionid($app,$userid);   // Set a custom session name
    session_name($session_name);
    session_start();
    session_regenerate_id(true);
    $_SESSION["loggeduserid"] = $user_id;
    echo("1a)SESSION NAME[".session_name()."]");
    echo("1b)logged user[".$_SESSION["loggeduserid"]."]");
}

Echo result

  • 1a) SESSION NAME[myappsessionid6520150528184534]

  • 1b) logged user[65]

Registration page (User clicks a link after logging in)

session_start();
echo("2a)SESSION NAME[".session_name()."]");
echo("2b)logged user[".$_SESSION["loggeduserid"]."]");
  • 2a)SESSION NAME[PHPSESSID]

  • 2b)logged user[]

Scenario B

Login page

after successful login, the following is called

function initiatenewsession($app, $userid){
    session_start();
    session_regenerate_id(true);
    $_SESSION["loggeduserid"] = $user_id;
    echo("1a)SESSION NAME[".session_name()."]");
    echo("1b)logged user[".$_SESSION["loggeduserid"]."]");
}

Echo result

  • 1a) SESSION NAME[PHPSESSID]

  • 1b) logged user[65]

Registration page (User clicks a link after logging in)

session_start();
echo("2a)SESSION NAME[".session_name()."]");
echo("2b)logged user[".$_SESSION["loggeduserid"]."]");
  • 2a)SESSION NAME[PHPSESSID]

  • 2b)logged user[65]

6
  • your session name is changing, obviously. you have to tell php what session name to use EVERYWHERE you're using the session, otherwise it'll use its default one. that means you've got TWO sessions floating around: phpsessid and myappetc... Commented May 28, 2015 at 18:21
  • To simplify the point: you need to set the session name on all the pages. Commented May 28, 2015 at 18:23
  • include a config file like config.php at the top of every page and inside config.php, put the name of your session variable (and other commonly used variables used by your app) Commented May 28, 2015 at 18:24
  • You seem to confuse the session ID with the session name; the session ID is already unique and by setting the session name to getuniquesessionid($app,$userid) - which I assume is something random and unique - you will never be able to restart the same session. Unless you store it somewhere of course, like in a database, but that would just be generating additional overhead to start the session. Commented May 28, 2015 at 18:30
  • To even further simplify this: you need to set the same session name for all page loads (not just the login) for the same user if you want the session to be maintained. Commented May 28, 2015 at 18:33

2 Answers 2

2

As per my comment, when you do session_start(), php will check if you set a session name via session_name(), otherwise it'll use its default.

Session startup is basically like this, in php-ish pseudocode:

if (custom_session_name_was_set()) {
   $session_name = get_custom_session_name();
} else {
   $session_name = ini_get('session.name');
}

if (isset($_COOKIE[$session_name])) {
   $id = $_COOKIE[$session_name];
} else {
   $id = generate_new_random_id();
   setcookie($session_name, $id);
}

$session_data = file_get_contents('/path/to/session/files/' . $id);
$_SESSION = unserialize($session_data);

For your first bit of code, you set a custom name, so that's the name that's used for the session cookie.

In your other code, you do NOT set a custom name, so php uses its default: PHPSESSID. Now you've got two sessions floating around, each with their own unique names, and their own different IDs, and their own separate data in $_SESSION.

If you're going to be using custom session names, you have do session_name($customName) EVERYWHERE you have session_start().

Sign up to request clarification or add additional context in comments.

2 Comments

[This has answered my query. Just posting the same comment as in I did above] Thanks. Its much clear now. Is there any showstoppers that I would have to face if i move forward not maintaining a session and use what PHP sets by default? I'm new to PHP and thought of creating a custom session name just in case I would require more control. If I may ask here, why would one have to use a custom session name in PHP projects?
the session name itself is mostly irrelevant, as long as it's consistent. it COULD be a showstopper if, for some reason, you need to maintain two parallel sessions. e.g. log into your system as a user, then needing to open a separate 'admin' level session to do some stuff. you might want two different session names at that point, but you could accomplish the same thing by just using a browser in private mode, which wouldn't share cookies with other windows.
0
  1. If using a custom session name you must call session_name().
  2. You must call session_start() before headers_sent().
  3. On servers with multiple PHP version support check phpversion() to ensure that the server did not decide to run the wrong version (and hence the wrong session_save_path()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.