5

I've been attempting to figure this out for a little while now, and it's driving me nuts. Basically I have a form for US and Canadian users. There's a link at the bottom of the form for Canadian users, which directs users to can-sesssion.php, which contains:

<?php
if (isset($_SESSION['can'])) {
    session_start();
    session_destroy();
    session_unset();
    session_start();
    $_SESSION['can'] = 2;
}
else {
    session_start();
    $_SESSION['can'] = 1;
}
header('Location: '. $_SERVER['HTTP_REFERER'] . '');
?>

Basically, if they click on the link, it sets $_SESSION['can'] = 1. Now There's another option, and if they click that link, it takes them back to this page, and the session should be destroyed and a new value is set (well, that's what it's supposed to do). Problem is, I've printed out $_SESSION['can'], and it's still retaining that old value after going to that page. Is there a better way to do this, or is there something wrong w/ my code? Thanks for the help.

3
  • 1
    You should be aware that the HTTP_REFERER is not a header you can rely on being set, or being set to the actual referring page. It is optional and the browser can send whatever it wants. Commented Aug 9, 2011 at 15:57
  • Is there an option that would be more useful, or would it be more pragmatic to simply include this php within the page? Commented Aug 9, 2011 at 16:04
  • You put the code wherever you want to change the session variable. You can change things as many times as you need without sending anyone to a new page. Commented Aug 9, 2011 at 16:26

3 Answers 3

11

This is what you wrote:

if (isset($_SESSION['can'])) {
    session_start();

session_start is the function which reads the session file associated with the user's PHPSESSID cookie and populates $_SESSION, so you're trying to read from the array before it has any values.

You need to call session_start before you check if $_SESSION['can'] has a value.

You also do not need to destroy and create a new session just to change a value.

<?php
session_start();
if (isset($_SESSION['can'])) {
    $_SESSION['can'] = 2;
} else {
    $_SESSION['can'] = 1;
}
header('Location: '. $_SERVER['HTTP_REFERER'] . '');
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Gotcha. Yea, I was just destroying the session as another last ditch effort, but obviously it wouldn't have worked since I didn't initialize the session in the first place.
I'm showing the other response came up first on my end. I appreciate your quick response though.
Here's what shows on my end: answered 25 mins ago - Neal ----- answered 27 mins ago - Dan
Wow, sorry bud, my mind wasn't there for a minute. Fixed it.
2

Try this: (using only one session_start())

<?php
session_start();
if (isset($_SESSION['can'])) {
    $_SESSION['can'] = 2;
}
else {
    $_SESSION['can'] = 1;
}
header('Location: '. $_SERVER['HTTP_REFERER'] . '');
?>

2 Comments

Perfecto! Any idea why the multiple session_start()'s were breaking it?
they weren't, you were just calling it at the wrong time ^_^
0

You may want to include

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

instead of just plain

session_start();

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.