Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change SESSION variable value

Tags:

forms

php

session

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.

like image 990
William Orazi Avatar asked Aug 09 '11 15:08

William Orazi


People also ask

How to create a session variable in PHP?

Tip: If you need a permanent storage, you may want to store the data in a database. A session is started with the session_start () function. Session variables are set with the PHP global variable: $_SESSION. Now, let's create a new page called "demo_session1.php".

Is there an option to change session variable within the page?

It is optional and the browser can send whatever it wants. Is there an option that would be more useful, or would it be more pragmatic to simply include this php within the page? 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.

How to assign session values to a specific value in JavaScript?

It is impossible to assign session values directly using JavaScript, but we (as web developers) want to use session storage locally to save, retrieve, update data. We can use localStorage and sessionStorage. The localStorage object allows storing data in key-value pairs in the browser with no expiration.

How to access the session variable in form submission?

You can access the Session variable by firing Ajax request from client to the server Send the session value from server to client side (E.g., using HiddenField). Javascript can use or update this value. This value would be posted back to the server during form submission or postback. Below picture explains the problem and the available options


2 Answers

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'] . '');
?>
like image 150
Dan Grossman Avatar answered Sep 22 '22 18:09

Dan Grossman


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'] . '');
?>
like image 35
Naftali Avatar answered Sep 24 '22 18:09

Naftali