(花了整整一天的时间,部分是因为PHP,Eclipse和网络编程一般不是我的专长。也许有人可以提供帮助。)
我register.php
中的代码不会重定向到verify.php
。这是代码:
header ( "Location: verify.php" );
die ( "Redirecting to activation" );
它只是在浏览器中打印Redirecting to activation
并保持在register.php
。
我已经尝试过的事情:
header
行。<?php
阻止之前没有任何html或空格。ob_start()
似乎没有任何改变。<?php
块没有任何echo
或print
。根据要求,这是整个register.php
:
<?php
require 'config.php';
function isValid() {
try {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array (
'secret' => 'asdf4234234kljsd32902341',
'response' => $_POST ['g-recaptcha-response'],
'remoteip' => $_SERVER ['REMOTE_ADDR']
);
$options = array (
'http' => array (
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query ( $data )
)
);
$context = stream_context_create ( $options );
$result = file_get_contents ( $url, false, $context );
return json_decode ( $result )->success;
} catch ( Exception $e ) {
return $e->getMessage ();
}
}
function RunScalarQuery($db, $q, $params) {
try {
$stmt = $db->prepare ( $q );
$result = $stmt->execute ( $params );
} catch ( PDOException $ex ) {
die ( "Failed to run query: " . $ex->getMessage () );
}
$row = $stmt->fetch ();
if ($row)
return $row [0];
else
return null;
}
function SendMail($smtpServer, $username, $pwd, $port, $from, $fromName, $to, $toName, $cc, $bcc, $subject, $body, $altBody) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer ();
$mail->isSMTP (); // Set mailer to use SMTP
$mail->Host = $smtpServer; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $pwd; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $port; // TCP port to connect to
$mail->setFrom ( $from, $fromName );
$mail->addAddress ( $to, $toName ); // Add a recipient
if (! empty ( $cc ))
$mail->addCC ( $cc );
if (! empty ( $bcc ))
$mail->addBCC ( $bcc );
$mail->isHTML ( true ); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $altBody;
if ($mail->send ()) {
return true;
} else {
return $mail->ErrorInfo;
}
}
ob_start();
if (! empty ( $_POST )) {
// Ensure that the user fills out fields
if (empty ( $_POST ['username'] )) {
die ( "Please enter a username." );
}
if (empty ( $_POST ['inputPassword'] )) {
die ( "Please enter a password." );
}
if (! filter_var ( $_POST ['inputEmail'], FILTER_VALIDATE_EMAIL )) {
die ( "Invalid E-Mail Address" );
}
$Res = isValid ();
if (! $Res) {
die ( $Res );
}
// Check if the username is already taken
$UsernameExists = RunScalarQuery ( $db, "SELECT 1 FROM `users` WHERE username = :username", array (
':username' => $_POST ['username']
) );
if ($UsernameExists != null) {
die ( "This username is already in use" );
}
$EmailExists = RunScalarQuery ( $db, "SELECT 1 FROM `users` WHERE email = :email", array (
':email' => $_POST ['email']
) );
if ($EmailExists != null) {
die ( "This email address is already registered" );
}
// Add row to database
$query = "
INSERT INTO users (username, password, salt, email, token, ActivationCode)
VALUES (:username, :password, :salt, :email, :token, :code)";
// Security measures
$salt = dechex ( mt_rand ( 0, 2147483647 ) ) . dechex ( mt_rand ( 0, 2147483647 ) );
$password = hash ( 'sha256', $_POST ['inputPassword'] . $salt );
for($round = 0; $round < 65536; $round ++) {
$password = hash ( 'sha256', $password . $salt );
}
$token = dechex ( mt_rand ( 0, 2147483647 ) ) . dechex ( mt_rand ( 0, 2147483647 ) );
$token = hash ( 'sha256', $token );
for($round = 0; $round < 256; $round ++) {
$token = hash ( 'sha256', $token . $salt );
}
// activation code
$act_code = mt_rand ( 10000000, 99999999 );
$query_params = array (
':username' => $_POST ['username'],
':password' => $password,
':salt' => $salt,
':email' => $_POST ['inputEmail'],
':token' => $token,
':code' => $act_code
);
try {
$stmt = $db->prepare ( $query );
$result = $stmt->execute ( $query_params );
} catch ( PDOException $ex ) {
die ( "Failed to run query: " . $ex->getMessage () );
}
$_SESSION ['registered_email'] = $_POST ['inputEmail'];
$mailContent = file_get_contents ( 'VerificationEmail.html' );
$mailContent = str_replace ( "[UserName]", $_POST ['username'], $mailContent );
$mailContent = str_replace ( "[Email]", $_POST ['inputEmail'], $mailContent );
$mailContent = str_replace ( "[Code]", $act_code, $mailContent );
$mailContent = str_replace ( "[EncodedEmail]", urlencode ( $_POST ['inputEmail'] ), $mailContent );
// SendMail(...); //localhost version
SendMail ( ...); //online version
header ( "Location: verify.php" );
die ( "Redirecting to activation" );
exit();
}
$head_content = '<link href="Content/full.css" rel="stylesheet">
<link href="Content/signin.css" rel="stylesheet">
<link href="Content/validetta.css" rel="stylesheet" type="text/css" media="screen">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>';
$body_class = 'class="full"';
$menu_content = '';
$body_content = 'register_body.php';
$script_content = 'register_script.php';
include ('master.php');
ob_flush();
?>
答案 0 :(得分:0)
因此,对于任何未来的读者,确保您的脚本在发出重定向之前确实没有随意吐出任何内容是非常重要的。在我的例子中,脚本在一开始就有一个不可见的,零宽度字符;就在开始<?php
标记之前。我尝试引入ob_start()
等修补程序并删除echo
和print
调用失败,直到我意识到问题实际发生的位置。