Shivam Pandya
Shivam Pandya

Reputation: 1061

PHP : Store SESSION Permanent

I try to use session for manage selected cities. I give function to user for select their city and it would be selected on all page, but my current code only work for one page. function is like if User select city "Vadodara" then all page display selected city as "Vadodara" and If He/ She change it to "New York" then all page display selected city as "New York", its work but only of one page. Here is my PHP code. session_start(); already added

<?php

if(isset($_REQUEST['location']))
{
    $_SESSION['location'] = $_REQUEST['location'];
    $location = $_SESSION['location'];
}
else
{
    $location = "All";
    $_SESSION['location'] = $location;
}

?>

Upvotes: 0

Views: 202

Answers (2)

SaschaM78
SaschaM78

Reputation: 4497

In your above example you keep on resetting the location. Try this version instead:

<?php
session_start();

$location= isset($_SESSION['location']) ? $_SESSION['location'] : 'All';

// if location has been changed, store it in session and update location variable
if(isset($_REQUEST['location']))
{
  $_SESSION['location'] = $_REQUEST['location'];
  $location = $_SESSION['location'];
} 

Upvotes: 1

user3706324
user3706324

Reputation: 23

Use session_start(); on top of all other pages you want to load your session

Upvotes: 0

Related Questions