Jump to content

redirect


ultratek

Recommended Posts

i cannot get my register page to redirect to my thankyou page using the header function:

<?php
$page_title = 'Register';
include ('./includes/header.html');

if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php');
$errors = array();
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = escape_data($_POST['email']);
}
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = 'Your password did not match the confirmed password.';
} else {
$p = escape_data($_POST['password1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) {
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW() )";
$result = @mysql_query ($query);
if ($result) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0 ,-1);
}
$url .= '/thanks.php';
header("location: $url");
exit();
} else {
echo '<h1 id="mainhead">System Error</h1>
<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>';
include ('./includes/footer.html');
exit();
}
} else {
echo '<h1 id="mainhead">Error!</h1><p class="error">The email address has already been registered.</p>';
}
} else {
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
}
mysql_close();
}
?>
<h2>Register</h2>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) 
echo $_POST['first_name'];
?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" maxlength="30" value="<?php if (isset($_POST['last_name'])) 
echo $_POST['last_name'];
?>" /></p>
<p>Email: <input type="text" name="email" size="30" maxlength="40" value="<?php if (isset($_POST['email'])) 
echo $_POST['email'];
?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

 

what the book told me to enter in the above:

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0 ,-1);
}
$url .= '/thanks.php';
header("location: $url");
exit();

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/
Share on other sites

it checks for a trailing slash...which i am learning about...

 

well with this script the book had me write ...  all i get is a blank page

i was wondering if there might have been some implied thought of code from the book i need to write to wrap up the header function in this script.

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/#findComment-697203
Share on other sites

it checks for a trailing slash...which i am learning about...

 

well with this script the book had me write ...  all i get is a blank page

i was wondering if there might have been some implied thought of code from the book i need to write to wrap up the header function in this script.

 

Check the URL bar. Does it contain the URL that you wish to redirect to? If so, and IF you have error reporting turned off, this could because you have a syntax error on the page that you are redirecting to. Just make sure that you aren't redirecting.

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/#findComment-697210
Share on other sites

<?php
$page_title = 'Register';
include ('./includes/header.html');

if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php');
$errors = array();
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = escape_data($_POST['email']);
}
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = 'Your password did not match the confirmed password.';
} else {
$p = escape_data($_POST['password1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) {
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
	$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
	VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW() )";
	$result = @mysql_query ($query);
	if ($result) {
		$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
		if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
		$url = substr ($url, 0 ,-1);
		}
		$url .= '/thanks.php';
		header("location: $url");
		exit();
	} else {
	echo '<h1 id="mainhead">System Error</h1>
	<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
	echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>';
	include ('./includes/footer.html');
	exit();
	}
} else {
echo '<h1 id="mainhead">Error!</h1><p class="error">The email address has already been registered.</p>';
}
} else {
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
}
mysql_close();
}
?>
<h2>Register</h2>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) 
echo $_POST['first_name'];
?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" maxlength="30" value="<?php if (isset($_POST['last_name'])) 
echo $_POST['last_name'];
?>" /></p>
<p>Email: <input type="text" name="email" size="30" maxlength="40" value="<?php if (isset($_POST['email'])) 
echo $_POST['email'];
?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/#findComment-697229
Share on other sites

More like:

 

<?php
$page_title = 'Register';
include ('./includes/header.html');

if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php');
$errors = array();
if (empty($_POST['first_name'])) {
	$errors[] = 'You forgot to enter your first name.';
} else {
	$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
	$errors[] = 'You forgot to enter your last name.';
} else {
	$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['email'])) {
	$errors[] = 'You forgot to enter your email address.';
} else {
	$e = escape_data($_POST['email']);
}
if (!empty($_POST['password1'])) {
	if ($_POST['password1'] != $_POST['password2']) {
		$errors[] = 'Your password did not match the confirmed password.';
	} else {
		$p = escape_data($_POST['password1']);
	}
} else {
	$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) {
	$query = "SELECT user_id FROM users WHERE email='$e'";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0) {
		$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
		VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW() )";
		$result = @mysql_query ($query);
		if ($result) {
			$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
			if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
				$url = substr ($url, 0 ,-1);
			}
			$url .= '/thanks.php';
			header("location: $url");
			exit();
		} else {
			echo '<h1 id="mainhead">System Error</h1>
			<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
			echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>';
			include ('./includes/footer.html');
			exit();
		}
	} else {
		echo '<h1 id="mainhead">Error!</h1><p class="error">The email address has already been registered.</p>';
	}
} else {
	echo '<h1 id="mainhead">Error!</h1>
	<p class="error">The following error(s) occurred:<br />';
	foreach ($errors as $msg) {
		echo " - $msg<br />\n";
	}
	echo '</p><p>Please try again.</p><p><br /></p>';
}
mysql_close();
}
?>
<h2>Register</h2>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) 
echo $_POST['first_name'];
?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" maxlength="30" value="<?php if (isset($_POST['last_name'])) 
echo $_POST['last_name'];
?>" /></p>
<p>Email: <input type="text" name="email" size="30" maxlength="40" value="<?php if (isset($_POST['email'])) 
echo $_POST['email'];
?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

 

But it still looks kinda messy.

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/#findComment-697230
Share on other sites

ok now i relized i was sending the page title and the include to the browser before the header function...

so i am carefully reviewing my script against the book and i am getting this error when i test it:

<?php
if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php');
$errors = array();
if (empty($_POST['first_name'])) {
	$errors[] = 'You forgot to enter your first name.';
} else {
$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
	$errors[] = 'You forgot to enter your last name.';
} else {
$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['email'])) {
	$errors[] = 'You forgot to enter your email address.';
} else {
$e = escape_data($_POST['email']);
}
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
	$errors[] = 'Your password did not match the confirmed password.';
} else {
$p = escape_data($_POST['password1']);
}
} else {
	$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) {
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
	$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
	VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW() )";
	$result = @mysql_query ($query);
	if ($result) {
		$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
		if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
		$url = substr ($url, 0 ,-1);
		}
		$url .= '/thanks.php';
		header("location: $url");
		exit();
	} else {
	$errors[] = 'You could not be registered due to a system error. We apologize for any inconvenience.';
	$errors[] = mysql_error() . '<br /><br />Query: ' . $query;
	}
} else {
	$errors[] = 'The email address has already been registered.';
}
mysql_close();
} else {
$errors = NULL;
}
$page_title = 'Register';
include ('./includes/header.html');
if (!empty($errors)) {
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
?>
<h2>Register</h2>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) 
echo $_POST['first_name'];
?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" maxlength="30" value="<?php if (isset($_POST['last_name'])) 
echo $_POST['last_name'];
?>" /></p>
<p>Email: <input type="text" name="email" size="30" maxlength="40" value="<?php if (isset($_POST['email'])) 
echo $_POST['email'];
?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

 

error:

Parse error: syntax error, unexpected $end in \\boswinfs02\home\users\web\b1623\ez.ultratek\practice\register.php on line 84

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/#findComment-697243
Share on other sites

You've missed out on a curly bracket somewhere.

 

Try:

<?php
if (isset($_POST['submitted'])) 
{
require_once ('mysql_connect.php');
$errors = array();
if (empty($_POST['first_name'])) 
{
	$errors[] = 'You forgot to enter your first name.';
} 
else 
{
	$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) 
{
	$errors[] = 'You forgot to enter your last name.';
} 
else 
{
	$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['email'])) {
	$errors[] = 'You forgot to enter your email address.';
} 
else 
{
	$e = escape_data($_POST['email']);
}
if (!empty($_POST['password1'])) 
{
	if ($_POST['password1'] != $_POST['password2']) 
	{
		$errors[] = 'Your password did not match the confirmed password.';
	} 
	else 
	{
	$p = escape_data($_POST['password1']);
	}
} 	
else 
{
	$errors[] = 'You forgot to enter your password.';
}
}

if (empty($errors)) 
{
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) 
{
	$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
	VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW() )";
	$result = @mysql_query ($query);
	if ($result) 
	{
		$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
		if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) 
		{
		$url = substr ($url, 0 ,-1);
		}
		$url .= '/thanks.php';
		header("location: $url");
		exit();
	} 
	else 
	{
		$errors[] = 'You could not be registered due to a system error. We apologize for any inconvenience.';
		$errors[] = mysql_error() . '<br /><br />Query: ' . $query;
	}
} 
else 
{
	$errors[] = 'The email address has already been registered.';
}
mysql_close();
} 
else {
$errors = NULL;
}
$page_title = 'Register';
include ('./includes/header.html');
if (!empty($errors)) 
{
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) 
{
	echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
?>
<h2>Register</h2>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) 
echo $_POST['first_name'];
?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" maxlength="30" value="<?php if (isset($_POST['last_name'])) 
echo $_POST['last_name'];
?>" /></p>
<p>Email: <input type="text" name="email" size="30" maxlength="40" value="<?php if (isset($_POST['email'])) 
echo $_POST['email'];
?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/133936-redirect/#findComment-697276
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.