我有一个简单的php页面,其中包含一个带有操作的表单,该表单指向一个名为getData.php
的文件,这里是PHP页面:
<?php
if(isset($_GET['login_attempt'])) {
echo 'LOGIN ATTEMP 1';
}else{
echo 'NO LOGIN ATTEMP';
}
?>
<form id="info" action="getData.php" method="post">
<input type="text" class="input" name="info1" /><br>
<input type="password" class="input" name="info2" /><br>
<input type="submit" value="" id="submit-button">
</form>
这是getData.php
文件:
if (!isset($_GET['login_attempt'])) {
header('location: /login.php?login_attempt=1');
} else {
header('location: http://www.stackoverflow.com');
}
正如您在getData.php
文件中看到的,在两次尝试登录php页面之后应该转到http://www.stackoverflow.com
,但是php页面在第一次尝试后永远停留在/login.php?login_attempt=1
,为什么?
答案 0 :(得分:0)
以下是处理此问题的方法
<?php
$attempt = 1;
if(isset($_GET['login_attempt'])) {
$attempt ++;//Now attempt will have 2
echo 'LOGIN ATTEMP 1';
}else{
echo 'NO LOGIN ATTEMP';
}
?>
<form id="info" action="getData.php" method="post">
<input type="hidden" class="input" name="login_attempt" value='<?php echo $attempt;?>' /><br>
<input type="text" class="input" name="info1" /><br>
<input type="password" class="input" name="info2" /><br>
<input type="submit" value="" id="submit-button">
</form>
这是“getData.php”文件:
<?php
if (isset($_POST['login_attempt']) AND $_POST['login_attempt'] !== 2) {
header('location: /login.php?login_attempt=1');
} else {
header('location:http://www.stackoverflow.com');
}
?>
修改
为什么您的网页在第一次尝试时卡住了这是答案。
当您重定向到登录页面时,第一个条件匹配。由于你没有标准告诉getData.php
哪个尝试是这样的,所以你被困住了。使用我在login.php中使用的变量$attempt
将其初始化为1.如果第二次尝试将其增加为1.将其放入h隐藏字段以便getData.php
其编号。然后根据您的需要重定向。
答案 1 :(得分:0)
更改行
<input type="submit" value="" id="submit-button">
到
<input type="submit" value="" id="submit-button" name="login_attempt">
和
来自档案getData.php
更改行
if (!isset($_GET['login_attempt']))
到
if (!isset($_POST['login_attempt']))
因为您在表单(<form id="info" action="getData.php" method="post">
)中使用post方法。
并将此行更改为
if(isset($_GET['login_attempt']))
到
if($_GET['login_attempt']==1)
我认为这可以解决您的问题。