(Have spent the entire day with this, partly because PHP, Eclipse and web programming in general are not my specialty. Maybe someone here could help).
My code in register.php
won't redirect to verify.php
. Here's the code:
header ( "Location: verify.php" );
die ( "Redirecting to activation" );
It simply prints Redirecting to activation
in the browser and remains on register.php
.
Things that I have already tried:
- I have ensured that the code actually performs everything up to just above the
header
line. - My file doesn't have any html or white spaces before the
<?php
block. - Adding
ob_start()
at the top doesn't appear to change anything. - the
<?php
block doesn't have anyecho
orprint
.
Edit
As asked, here's the entire 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();
?>