我需要提示,使我的PHP密码工作
问题描述:
我有这些代码的麻烦:我需要提示,使我的PHP密码工作
password.php:
<html>
<head>
<title>Password!</title>
<meta charset="UTF-8">
</head>
<body>
<ul>
<form action="check.php" method="POST">
<li><input type="password" name="no1" placeholder="enter your password"></li>
<li><input type="submit" value="GO!"></li>
</form>
</ul>
</body>
这里是password2.php:
<html>
<head>
<title>Password!</title>
<meta charset="UTF-8">
</head>
<body>
<ul>
<form action="check.php" method="POST">
<li><input type="password" name="name" placeholder="verify your password"></li>
<li><input type="submit" value="GO!"></li>
</form>
</ul>
</body>
这里是check.php:
<?php
$enter = $_POST['no1'];
if (empty($enter)) {
echo "Please enter password!";
}
if (!(empty($enter))) {
echo "Your password is $enter";
}
?>
<html>
<body>
<p><a href="password2.php">Move on!</a></p>
</body>
</html>
<?php
$check = $_POST['name'];
if ($check == $enter) {
echo "Acces Granted";
}
if (!($check == $enter)) {
echo "Acces denied!";
}
?>
我有的麻烦是:
- check.php不会password2.php
- 承认“名称”和我无法验证密码
答
因为$ _ POST变量不是它不会工作请求之间持久。您可以将第一种形式的值存储在$ _SESSION变量中,并从会话中检索它。
更多关于PHP会话信息here
离开一切,因为它是除了check.php
你的问题,下面是修改一个:
<?php
//starting the session in PHP
session_start();
$enter = null;
// this is all the logic you need for retrieving `no1` from POST or SESSION
if (isset($_POST['no1'])){
$enter = $_POST['no1'];
$_SESSION['no1'] = $enter;
}elseif(isset($_SESSION['no1'])){
$enter = $_SESSION['no1'];
}
if (empty($enter)) {
echo "Please enter password!";
}
if (!(empty($enter))) {
echo "Your password is $enter";
}
?>
<html>
<body>
<p><a href="password2.php">Move on!</a></p>
</body>
</html>
<?php
$check = $_POST['name'];
if ($check == $enter) {
echo "Acces Granted";
// you can comment the next line if you are debugging,
// but after that you should destroy de session so you don't have a password as plain text
session_destroy();
}
if (!($check == $enter)) {
echo "Acces denied!";
}
?>
+0
我不明白我该如何将它应用到代码中,我读了你的建议和链接,但你能给我一个示范。非常感谢! – user5301755
+0
就像一个建议,如果你正在学习PHP这个代码是好的,但你想把它放在生产中,请看看一个PHP框架,例如Laravel。 –
这是行不通的,因为$ _ POST不具有持续性,从一个请求到另一个。当你从password2.php提交时,没有$输入。 –
谢谢!所以我必须有另一个check.php吧? – user5301755
我觉得比较好。但是你也需要第一个密码的值。但是,你为什么不在单个请求中以单一形式发送两个密码? –