在php中重置会话变量 [英] Resetting the session variable in php

查看:42
本文介绍了在php中重置会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在test.php"上有以下表单.

I have the following form on "test.php".

<?php
session_start();
if(isset($_POST['ph']))
    if(isset($_POST['submit']))
        $_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
  <body>  
    <form method="POST" action="order.php" id="custphoneform">
        <label for="PhoneNumber">Enter Phone Number:</label>
        <input type="number" name="ph" required>
        <input type="submit" value="Submit" name="submit">
    </form>
  </body>
</html>

order.php"看起来像这样:

The "order.php" looks like this:

<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
    echo ($_SESSION['ph']);
?>

我第一次加载 "test.php" 并输入电话号码时,它运行良好,并在 "order.php" 上给了我正确的输出,但是从第二次开始,"order.php" 给了我第一次输入的相同值,即使我输入了不同的值.我刷新了页面,结果一样.

The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.

我关闭文件并重新加载它,仍然是相同的值.为什么会这样,我该如何纠正?我希望 session 每当输入一个没有发生的新数字时改变值.

I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.

推荐答案

将新值更改为 SESSION 在您的 order.php 页面上,如下所示:-

Change the new value to SESSION ON your order.php page like below:-

<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
  $_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
   echo ($_SESSION['ph']);  
}
?>

还要像这样更改 test.php 代码:-

Also change test.php code like this:-

<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
  <body>  
    <form method="POST" action="order.php" id="custphoneform">
        <label for="PhoneNumber">Enter Phone Number:</label>
        <input type="number" name="ph" required>
        <input type="submit" value="Submit" name="submit">
    </form>
  </body>
</html>

这篇关于在php中重置会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆