我刚刚开始使用PHP进行编码,并且被重定向卡住了。我接受了提交表单中的$ _POST信息并将它们分配给变量。我收到2个密码变量:$ password和$ c_password。我的目标是检查这两个密码是否匹配,如果他们想要散列它们。如果没有,我想发回用户(重定向)到register.php页面。问题是如果我阻止了JavaScript重定向下的所有内容,它就会起作用。但是如果我在js重定向之后不阻止脚本,用户就会在数据库中注册并重定向到login.php页面。下面是我到目前为止编码:重定向脚本,用表格哈希处理后的PHP
<?php
// Open database connection
include 'db_connect.php';
// check existence and accept form data
if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['c_password'])) {
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$c_password = $_POST['c_password'];
}
// Hash the password if password and c_password match
if(strcmp($password, $c_password) == 0) {
$password = hash('sha512',$_POST['password']);
}
else { ?>
<script type="text/javascript">
document.location = './register.php?pass_nomatch=1';
</script>
<?php
}
// Create a random salt
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
// Create salted password (Careful not to over season)
$password = hash('sha512', $password.$random_salt);
// Add your insert to database script here.
// Make sure you use prepared statements!
if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);
// Execute the prepared query.
$insert_stmt->execute();
// Close MYSQL database connection
mysqli_close($mysqli);
// Redirect user to login page
header('Location: ./login.php');
}
?>
如果任何人有回答我的问题请尽快回复:)
重定向是什么意思?你的意思是,重定向用户到另一个页面? – samayo 2013-05-11 14:00:16
我不完全确定你的问题是什么,但我会建议使用标题重定向(标题(“位置:网址/”);)像你最后做的而不是JavaScript,就像你在中间做的一样。 – Matt 2013-05-11 14:02:13
@Matt我尝试过头('register.php?pass_nomatch = 1');但是如果在header()之后有任何代码,则它不起作用。 – Dribba 2013-05-11 14:05:46