0

On my page, users have the option to select a client. When they select one, this selection will be set in $_SESSION['selectedClient']. A user may then choose to pick a different client which would have to overwrite the current $_SESSION['selectedClient']. So when I try to update $_SESSION['selectedClient'] it will show me that the session variable has updated. But when I reload the page it will show the previous/first selected client.

Here is the piece of code I currently have to try this:

if($_POST['clientEmail']){
   $_SESSION['selectedClient'] = $_POST['clientEmail'];
   echo '<script>document.location=\'\?p=home\';</script>';
}

The echo leads to the same PHP file in which I set session var and try to update the session var, which is needed in this case.

I've tried to unset $_SESSION['selectedClient'] before assigning a new post like:

if($_POST['clientEmail']){
    unset($_SESSION['selectedClient']);
    $_SESSION['selectedClient'] = $_POST['clientEmail'];
    echo '<script>document.location=\'\?p=home\';</script>';
}

I haven't been able to find anything on google that could help me, which is why I decided to make a SO account and ask here. Feel free to ask if more information is needed.

EDIT: When $_SESSION['selectedClient'] is set and I select a new client, the post contains the newly selected value/client. After refresh, post is empty and session is reverted back to the client which was selected before.

selecting new client:

_POST['clientEmail'] = >lol@lol.com<
_SESSION['selectedClient'] = >lol@lol.com<

after refresh:

_POST['clientEmail'] = ><
_SESSION['selectedClient'] = >login@login.com<

  • I doubt `$_SESSION['selectedClient']` is the issue here. Most likely, `$_POST['clientEmail']` is changing the session to previous value. Please provide the value of it before and after page reload. – EternalHour Jan 22 '20 at 17:15
  • @Funk I've seen that question before but it didnt help me or I don't see the actual solution you're pointing at – SomewhatBeginner Jan 22 '20 at 17:47

1 Answers1

0

please make sure to use session_start in the beginning of file and use isset function to check if there is any POST data

session_start()
if(isset($_POST['clientEmail'])){
   $_SESSION['selectedClient'] = $_POST['clientEmail'];
   header("Location: .\?p=home");
   exit(0);
}
bob_1982
  • 715
  • 6
  • 16
  • The pages are all handled through index.php. Basically in index.php I require a file which calls a method that has session_start(). With that said, all other session related stuff works fine as it is now. I will try this with `exit(0);` – SomewhatBeginner Jan 22 '20 at 17:36