表格提交后,PHP会话变量未更新

发布于 2025-01-25 09:11:19 字数 1636 浏览 4 评论 0原文

我有两个文件,一个包含表单和一个包含操作的文件,如果字段为空白,则操作将用户发送回形式。我知道我可以使用“必需”来使其必须在采取行动之前输入;但是,我应该这样做,我们检查他们是否进入用户名。如果不显示存储为$ _Session ['error']的错误消息。

尚未检查SQL数据库,我已经处理了该部分。

这是我的表格(login.php):

<?php   
session_start();
?>

<div id="login-form-wrap">
  <h2>Login</h2>
  <form id="login-form" method="post" action="action.php">
    <p>
    <input type="text" id="username" name="username" placeholder="Username">
    </p>
    <p>
    <input type="password" id="password" name="password" placeholder="Password">
    </p>
    <p>
    <input type="submit" id="login" name="login" value="Login">
    </p>
  </form>
  <div id="create-account-wrap">
    <p>Not a member? <a href="create.php">Create Account</a><p>
  </div>
  <?php
    if(isset($_SESSION['error'])) { 
        echo $_SESSION['error']; 
    } else{ 
        echo "No errors."; 
    } 
  ?>
</div>

这是我的action.php:

<?php 
if (isset($_POST['login'])){ 
// checking if they hit the submit/login button

    if(empty($_POST['username'])){
        $_SESSION['error'] = "Please enter a username."; 
        //should change the session variable error, but doesn't;
        
        header("location:../login.php");
        //redirects to login.php, this part works fine.

    } else {
    // means they entered a username, I have tried to change the session variable here too 
    // but it never changes.
        $name = $_POST['username'];
        header("location:../login.php");
    }
}

I have two files, one that contains the form and one that contains the action, and the action sends the user back to the form if the fields are blank. I know I can use the 'required' to make it have to be entered before the action is taken; however, I'm supposed to be doing it this way, where we check if they entered a username. if not display an error message stored as $_SESSION['error'].

No checking the SQL database yet, I have that part handled.

Here's my form (login.php):

<?php   
session_start();
?>

<div id="login-form-wrap">
  <h2>Login</h2>
  <form id="login-form" method="post" action="action.php">
    <p>
    <input type="text" id="username" name="username" placeholder="Username">
    </p>
    <p>
    <input type="password" id="password" name="password" placeholder="Password">
    </p>
    <p>
    <input type="submit" id="login" name="login" value="Login">
    </p>
  </form>
  <div id="create-account-wrap">
    <p>Not a member? <a href="create.php">Create Account</a><p>
  </div>
  <?php
    if(isset($_SESSION['error'])) { 
        echo $_SESSION['error']; 
    } else{ 
        echo "No errors."; 
    } 
  ?>
</div>

And here's my action.php:

<?php 
if (isset($_POST['login'])){ 
// checking if they hit the submit/login button

    if(empty($_POST['username'])){
        $_SESSION['error'] = "Please enter a username."; 
        //should change the session variable error, but doesn't;
        
        header("location:../login.php");
        //redirects to login.php, this part works fine.

    } else {
    // means they entered a username, I have tried to change the session variable here too 
    // but it never changes.
        $name = $_POST['username'];
        header("location:../login.php");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时常饿 2025-02-01 09:11:19

您的action.php是否与上述相同,因为您尚未开始会话。首先使用会话。

session_start();

does your action.php same like above because you haven't started session. Use session first.

session_start();

梦萦几度 2025-02-01 09:11:19

在您的action.php文件中,您未启动会话。
为了在PHP中访问会话值,您必须首先使用session_start();函数。

In your action.php file you did not start the session.
In order to access session values in PHP, you have to use the session_start(); function first.

永言不败 2025-02-01 09:11:19

表单提交后,您必须在

要清除会话值的其他部分中提交后使用此代码,您必须销毁会话值

    // destroy the session
    session_destroy(); 

you have to destroy session value after form submission

use this code after form submit in your else part where you want to clear session value and also you have to start session in action.php file before using session variable

    // destroy the session
    session_destroy(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文