简体   繁体   中英

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;
}

?>

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'];
} 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM