ultratek Posted November 23, 2008 Share Posted November 23, 2008 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(); Quote Link to comment Share on other sites More sharing options...
laPistola Posted November 23, 2008 Share Posted November 23, 2008 i might not be the best to answer but why would you need to substr() the url with a -1 to me that tells me that string is telling it to show everything but the last character???? Quote Link to comment Share on other sites More sharing options...
ultratek Posted November 23, 2008 Author Share Posted November 23, 2008 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. Quote Link to comment Share on other sites More sharing options...
waynew Posted November 23, 2008 Share Posted November 23, 2008 Try: header('Location: '.$url); exit; Quote Link to comment Share on other sites More sharing options...
waynew Posted November 23, 2008 Share Posted November 23, 2008 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. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 23, 2008 Share Posted November 23, 2008 Your header() call is inside a conditional statement. If the page doesn't redirect, it could mean $result is returning false. Quote Link to comment Share on other sites More sharing options...
ultratek Posted November 23, 2008 Author Share Posted November 23, 2008 no it stays on register.php just goes blank keeping my header.html intact...but it does add the user to the data base... Quote Link to comment Share on other sites More sharing options...
waynew Posted November 23, 2008 Share Posted November 23, 2008 Could you format your code a little bit better? There are IF ELSE statements all over the place and it might help both you and us if it is presented in a more logical way. IF increment out IF increment out ELSE ELSE Quote Link to comment Share on other sites More sharing options...
ultratek Posted November 23, 2008 Author Share Posted November 23, 2008 <?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'); ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 23, 2008 Share Posted November 23, 2008 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. Quote Link to comment Share on other sites More sharing options...
laPistola Posted November 23, 2008 Share Posted November 23, 2008 could it be that this if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { the substr($url, -1) is missing a number try if ((substr($url,0,-1) == '/') OR (substr($url,0,-1) == '\\') ) { Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 23, 2008 Share Posted November 23, 2008 No, substr($url, -1) returns the last char of $url. But the last bit should be corrected to OR (substr($url, -2) == '\\') Quote Link to comment Share on other sites More sharing options...
ultratek Posted November 23, 2008 Author Share Posted November 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
waynew Posted November 23, 2008 Share Posted November 23, 2008 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'); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.