0

I'm new to PHP and havn't worked with sessions before, I have read up on it a bit and understand what they do and how i should be able to use them.

When I create my session however, it seems to create the session fine (code gets run and if I look into cookies I can see entries from my website) I can also set $_SESSION values on the same page, but as soon as I enter a different page, the session is reset it seems.

Here's my code:

$sessionid = md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
ini_set('session.auto_start', 1);
ini_set('session.lifetime', 2678400);
ini_set('session.use_cookies', 1);
ini_set('session.cache_limiter', 'private_no_expire');
session_start($sessionid);

// Also tested that setting these doesn't give an error either
$_SESSION['Username'] = //Code to get username
$_SESSION['UserID'] = // Code to get userID

echo "<script language='Javascript'>";
echo "window.location='" . $forwardpage . "';";
echo '</script>';
exit();

Any help would be appereciated

0

1 Answer 1

2

The problem is that every time you load the script a new session id is created and sent to the client. Skip the $sessionid variable and php will generate a sessionid for you and it will work.

So, use session_start() without $sessionid

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

3 Comments

Well that's how I did it at first, that's when it didn't work so I figured maybe if I create my own ID, but it still didn't work.
Normally the default settings for php sessions works good. Have you tried removing the ini_set() variables and tried with only a simple session_start() in the top?
Just to add to this, if you want to regenerate the session ID like you are doing above, use session_regenerate_id(); after starting the session to regenerate the ID and keep your session intact.

Your Answer

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