dongxing2692 2014-08-17 22:10
浏览 53

检查变量是否已更改,然后在每次变量更改时取消设置会话 - PHP

I read a lot of helpful info here when searching for answers to my programming questions. I just signed up because I want to ask a question about PHP variables that I am not able to find a concise answer on.

I want to check if a PHP variable has changed.

I have a form field that users type their city, state into. It gets stored in $_SESSION['location'].

I also have a variable that stores country.

When the $country variable changes, the city, state remain in the form field due to it being in session. This is an issue because obviously the city, state originally entered won't work for a different country that gets selected.

I need to be able to unset the $_SESSION['location'] each time the country variable changes so that the form field returns to empty. I'm not sure how to do this. But here is my code below with an attempt of trying to unset the session in my if statement.

// Country Selector
switch ($country) {
    case "USA":
        $example = "San Jose, CA or 94102";
        $placeholder = "city, state, or zip";
        break;
    case "Canada":
        $example = "Toronto, ON";
        $placeholder = "city, state";
        break;
    case "India":
        $example = "New Delhi, Delhi";
        $placeholder = "city, state";
        break;
    default:
        $example = "San Jose, CA or 94102";
        $placeholder = "city, state, or zip";
}

// Location Search
$location = '';
$prev_country = $country;

if ($country == $prev_country) {
    if (isset($_POST['location'])) {
        $_SESSION['location'] = $location = ucwords($_POST['location']);
        header('Location: '.($_SERVER['REQUEST_URI']));
    } elseif (isset($_SESSION['location'])) {
        $location = $_SESSION['location'];
    }
} else {
    unset($_SESSION['location']);
    unset($_POST['location']);
    $location = '';
}

UPDATE

I tried suggestions using if ($country == $_SESSION['country']) but still getting same result as before in previous code.

      // Location Search
  $location = '';
  $_SESSION['country'] = $country;
  if ($country == $_SESSION['country']) {
  if (isset($_POST['location'])) {
      $_SESSION['location'] = $location = ucwords($_POST['location']);
          header('Location: '.($_SERVER['REQUEST_URI']));
  } elseif (isset($_SESSION['location'])) {
      $location = $_SESSION['location'];
  }
  } else {
      unset($_SESSION['location']);
      unset($_POST['location']);
      $location = '';
  }
  • 写回答

2条回答 默认 最新

  • douqiaoru2583 2014-08-17 22:21
    关注

    This 2 lines of code are definitely not going to give any good results as the condition will always evaluate to true:

    $prev_country = $country;
    if ($country == $prev_country) {
    

    What you need to do instead, is to check the value that you have in session against the one that the user just sent:

    if($_SESSION['country'] != $_POST['country']) {
        // country changed! do something!
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能