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.
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".
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.
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.
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
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'] . '');
?>
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'] . '');
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With