我有一个重置密码的表单。 PHP脚本将所有3个密码存储在文本文件中。我知道这样做是不安全的,但这只是为了学习目的。
<form action="login.php" method="POST" accept-charset="utf-8">
<p>
<label for="id_current_password">Current password:</label>
<input id="id_current_password" name="current_password" type="password" />
</p>
<p>
<label for="id_new_password1">New password:</label>
<input id="id_new_password1" name="new_password1" type="password" />
</p>
<p>
<label for="id_new_password2">New password confirmation:</label>
<input id="id_new_password2" name="new_password2" type="password" />
</p>
<p>
<input type="submit" value="Reset Password">
</p>
</form>
此表单将这些密码发送到login.php。
<?php
$myFile = fopen("log.txt", "a") or die("Unable to open file!");
$stringData = $_POST['current_password'] . "CP";
fwrite($myFile, $stringData);
$stringData = $_POST['new_password1'] . ":";
fwrite($myFile, $stringData);
$stringData = $_POST['new_password2'] . "\n";
fwrite($myFile, $stringData);
fclose($myFile);
?>
<?php
date_default_timezone_set('Asia/Kolkata');
$ip = $_SERVER['REMOTE_ADDR'];
$dt = date("l dS \of F Y h:i:s A");
$file=fopen("log.txt","a");
$data = $ip.' '.$dt."\n";
fwrite($file, $data);
fclose($file);
?>
if(($_POST['new_password1'])==($_POST['new_password2'])) {
<script>location.href='https://www.MYWEBSITE.com';</script>
} else {
<script>location.href='http://www.MYWEBSITEerror.com'</script>
}
我想知道如果新密码不匹配,如何将用户重定向到错误网页,如果匹配,则应根据脚本将其重定向到网站。我知道回声和其他提醒用户的方法,但我特别想要这种方法。
答案 0 :(得分:2)
您可以使用header()
的PHP函数代替:
<?php
if(($_POST['new_password1'])==($_POST['new_password2'])) {
header ('Location: https://www.MYWEBSITE.com');
} else {
header ('Location: http://www.MYWEBSITEerror.com');
}
?>
您当前的代码不起作用,因为您不能在PHP标记中包含HTML代码,因为它将返回解析错误。要使其工作,您必须回显重定向部分:
if(($_POST['new_password1'])==($_POST['new_password2'])) {
echo "<script>location.href='https://www.MYWEBSITE.com';</script>";
} else {
echo "<script>location.href='http://www.MYWEBSITEerror.com'</script>";
}
或者,您可以将<script>
标记放在PHP标记之外:
// { ... }
fwrite($file, $data);
fclose($file);
if(($_POST['new_password1'])==($_POST['new_password2'])) {
?>
<script>location.href='https://www.MYWEBSITE.com';</script>
<?php
} else {
?>
<script>location.href='http://www.MYWEBSITEerror.com'</script>
<?php
}
?>
基本上,您应该将整个脚本格式化为只有一组PHP打开和关闭标记,不知道为什么没有多少。
<?php
$myFile = fopen("log.txt", "a") or die("Unable to open file!");
$stringData = $_POST['current_password'] . "CP";
fwrite($myFile, $stringData);
$stringData = $_POST['new_password1'] . ":";
fwrite($myFile, $stringData);
$stringData = $_POST['new_password2'] . "\n";
fwrite($myFile, $stringData);
fclose($myFile);
date_default_timezone_set('Asia/Kolkata');
$ip = $_SERVER['REMOTE_ADDR'];
$dt = date("l dS \of F Y h:i:s A");
$file=fopen("log.txt","a");
$data = $ip.' '.$dt."\n";
fwrite($file, $data);
fclose($file);
if(($_POST['new_password1'])==($_POST['new_password2'])) {
<script>location.href='https://www.MYWEBSITE.com';</script>
} else {
<script>location.href='http://www.MYWEBSITEerror.com'</script>
}
?>