表格提交后,PHP会话变量未更新
我有两个文件,一个包含表单和一个包含操作的文件,如果字段为空白,则操作将用户发送回形式。我知道我可以使用“必需”来使其必须在采取行动之前输入;但是,我应该这样做,我们检查他们是否进入用户名。如果不显示存储为$ _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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
action.php
是否与上述相同,因为您尚未开始会话。首先使用会话。session_start();
does your
action.php
same like above because you haven't started session. Use session first.session_start();
在您的
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.表单提交后,您必须在
要清除会话值的其他部分中提交后使用此代码,您必须销毁会话值
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