Session Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jogen143
Forum Newbie
Posts: 15
Joined: Tue Mar 25, 2003 2:51 am

Session Problem

Post by jogen143 »

Hi guys

I have a common page for navigation and member login which I have included 'include ("lhs.php")' this common page in every page in the site.

When a user browse through the site and add items to the shopping cart and try to login to the site afterwards (since the login page is a include page it appears in the shopping cart pages as well) the variables used in the shoping cart were lost.

I did some coding to store the variables as a sessions, if the variable gets a blank value (happens when user logs in and page reloads) it will take the session value in to that variable.


Problem :

When the variable recieves a blank value the session variable is also get re initialised with blank value;


//gets value from a form

$Genre = trim($_POST['Genre']);

//$Genre is blank when page reloads

if ($Genre == "") {

//get session value into $Genre variable

$Genre = $_SESSION['Genre'];
}


// set $Genre variable as a session Genre

$_SESSION['Genre'] = $Genre;


// $Genre variable holds session Genre.
//I always want to use the value taken from the session

$Genre = $_SESSION['Genre'];


Need help!

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if(!empty($_POST['Genre']) && !empty(trim($_POST['Genre'])))
{
  $_SESSION['Genre'] = trim($_POST['Genre']);
}

if(!empty($_SESSION['Genre']))
{
  $Genre = $_SESSION['Genre']);
}
something like this?
Post Reply