Php 成功注册时无法重定向
我的剧本到底出了什么问题。我对它进行了测试,但每次成功注册似乎都不会将我重定向到register_success.php。它与这个register.phpPhp 成功注册时无法重定向,php,Php,我的剧本到底出了什么问题。我对它进行了测试,但每次成功注册似乎都不会将我重定向到register_success.php。它与这个register.phpheader('Location:./register_success.php')放在同一个文件夹下是我的位置问题吗?我尝试了header('Location:register_success.php')也不起作用 <!doctype html> <html> <head> <meta charset=
header('Location:./register_success.php')放在同一个文件夹下代码>是我的位置问题吗?我尝试了header('Location:register_success.php')代码>也不起作用
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
include_once 'db_connect.php';
include_once 'psl-config.php';
$error_msg = "";
if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
// Sanitize and validate the data passed in
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Not a valid email
$error_msg .= '<p class="error">The email address you entered is not valid</p>';
}
$password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
if (strlen($password) != 128) {
// The hashed pwd should be 128 characters long.
// If it's not, something really odd has happened
$error_msg .= '<p class="error">Invalid password configuration.</p>';
}
// Username validity and password validity have been checked client side.
// This should should be adequate as nobody gains any advantage from
// breaking these rules.
//
$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);
// check existing email
if ($stmt) {
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1) {
// A user with this email address already exists
$error_msg .= '<p class="error">A user with this email address already exists.</p>';
$stmt->close();
}
$stmt->close();
} else {
$error_msg .= '<p class="error">Database error Line 39</p>';
$stmt->close();
}
// check existing username
$prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt) {
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1) {
// A user with this username already exists
$error_msg .= '<p class="error">A user with this username already exists</p>';
$stmt->close();
}
$stmt->close();
} else {
$error_msg .= '<p class="error">Database error line 55</p>';
$stmt->close();
}
// TODO:
// We'll also have to account for the situation where the user doesn't have
// rights to do registration, by checking what type of user is attempting to
// perform the operation.
if (empty($error_msg)) {
// Create a random salt
//$random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); // Did not work
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
// Create salted password
$password = hash('sha512', $password . $random_salt);
// Insert the new user into the database
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.
if (! $insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
}
}
header('Location: ./register_success.php');
}
}
?>
</body>
</html>
无标题文件
开始发送HTTP正文后,无法发送HTTP标头
代码的第一行是内容(doctype),因此在第一行触发HTTP主体
你应该看看
警告:无法修改标题信息-标题已发送
如果要重定向,则不要先发送文档
另一方面,尽管浏览器擅长从错误中恢复,HTTP规范要求位置标头具有绝对URI,而不是相对URI。在成功时尝试重定向(使用。/
)
也可以使用exit代码>在标题之后(“位置:”)代码>如果您的展位将文件保存在同一文件夹中,则可以使用此代码
<?php
header('Location:register_success.php');
?>
我已经对它进行了测试,它对我来说运行正常发现位于psl-config.php
中的错误不应该存在
<?php
/**
* These are the database login details
*/
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "xxxxx"); // The database username.
define("PASSWORD", "xxxxx"); // The database password.
define("DATABASE", "xxxx"); // The database name.
define("CAN_REGISTER", "any");
define("DEFAULT_ROLE", "member");
define("SECURE", FALSE); // FOR DEVELOPMENT ONLY!!!!
?>
</body>
</html>
um。。我没有读过你的代码,但是,有什么错误吗?调试提示,尝试使用分治方法。注释大部分代码,并尝试在未注释的代码之后回显“something”
。如果它输出某个东西
,则没有问题。如果它没有输出something
错误在上面。你确定你的代码到达了执行重定向的行吗?你已经将html输出到客户端,重定向发生得太晚了。如果(!$insert\U stmt->execute()){header,请尝试在else中重定向此语句('Location:../error.php?err=Registration failure:INSERT');}
@BrianCoolidge页面上显示的无错误显示为空白,在代码中添加了error\u reporting
。他说它与register.php放在同一文件夹下。
<?php
/**
* These are the database login details
*/
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "xxxxx"); // The database username.
define("PASSWORD", "xxxxx"); // The database password.
define("DATABASE", "xxxx"); // The database name.
define("CAN_REGISTER", "any");
define("DEFAULT_ROLE", "member");
define("SECURE", FALSE); // FOR DEVELOPMENT ONLY!!!!
?>
</body>
</html>