<?php session_start();?> <a href="abc.php?color=red"> send the color </a> <br><br> <?php @$_SESSION['color'] = $_GET['color']; echo $_SESSION['color']; ?> <br> <br> <a href="abc.php"> check the session variable </a>
hi, i need help for the above code.
i want to pass a variable to session.
with above code im doing this but the session variable dissappears when i refresh the page or when i click the bottom link. i want the echo $_SESSION['color'];
sticky
what should i do? with regards
When $_GET['color']
is empty because color
is not in the query string of the URL, you still assign that empty value to $_SESSION['color']
.
Don't do that and the value you set won't be overwritten. Nothing was disappearing on its own.
You are assigning value of $_GET['color'] no matter if there is such GET variable or not. Because of this, when there is no $_GET['color'] you are loosing the session variable. it have to be:
if (isset($_GET['color'])) {
$_SESSION['color'] = $_GET['color'];
}
echo isset($_SESSION['color']) ? $_SESSION['color'] : '';