PDA

View Full Version : Redirecting not working




MythicFrost
Aug 19, 2009, 07:59 PM
Hi, I'm trying to redirect my page after I'm done, this is at the very end of my code: header('Location: signupcomplete.php');

It doesn't do anything though.

Anyone know why?

Kind Regards



angelwatt
Aug 19, 2009, 08:00 PM
The header can only be used when no other content has been outputted (http://us2.php.net/manual/en/function.header.php) to the screen. Don't know what the rest of your code entails so can't comment if you're violating that rule. Also need full URL.

MythicFrost
Aug 19, 2009, 08:15 PM
I think I'm not doing that, this is my code, I think you can see what I'm trying to do.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sign Up</title>
</head>

<body>
<script type="text/javascript">

</script>
<?php require_once('menu.php'); ?> //this has my banner/home/signup buttons on it
<?php
//use $_SESSION to store error information
$username = mysql_real_escape_string($_POST["tfUsername"]);
$password = mysql_real_escape_string($_POST["tfPassword"]);
$passwordConfirm = mysql_real_escape_string($_POST["tfPasswordConfirm"]);
$email = mysql_real_escape_string($_POST["tfEmail"]);
$emailConfirm = mysql_real_escape_string($_POST["tfEmailConfirm"]);
$country = mysql_real_escape_string($_POST["sCountry"]);
$bUsernameTaken=false;
$bPwNotPwCon=($password == $passwordConfirm);
$bEmNotEmCon=($email == $emailConfirm);

$mysqlCon = mysql_connect("localhost", "priv", "priv");
if (!$mysqlCon)
{
die("Unable to connect, ".mysql_error());
}
mysql_select_db("main", $mysqlCon);

$query = sprintf("SELECT * FROM Users WHERE username = '%s'", mysql_real_escape_string($username));
$result = mysql_query($query, $mysqlCon);

$iNumRows = mysql_num_rows($result);
if ($iNumRows > 0)
{
$bUsernameTaken=true;
}
mysql_close($mysqlCon);
$bContinue = !$bUsernameTaken && !$bPwNotPwCon && !$bEmNotEmCon;
?>
<form id="form1" name="form1" method="post" action="signup.php">
<table width="600" border="0" align="center">
<tr>
<td height="114" colspan="3">&nbsp; <hr /></td>
</tr>
<tr>
<td width="89">Username</td>
<td width="269" valign="middle"><input name="tfUsername" type="text" id="tfUsername" value="username" maxlength="30" />
</td>
</tr>
<tr>
<td></td>
<td valign="middle">
</td>
</tr>
<tr>
<td>Password</td>
<td colspan="2"><input name="tfPassword" type="password" id="tfPassword" value="password" maxlength="20" /></td>
</tr>
<tr>
<td height="20">Confirm</td>
<td height="20" colspan="2"><input name="tfPasswordConfirm" type="password" id="tfPasswordConfirm" value="passwordconfirm" maxlength="20" /></td>
</tr>
<tr>
<td height="20">&nbsp;</td>
<td valign="middle">
</td>
</tr>
<tr>
<td height="20">E-mail</td>
<td height="20" colspan="2"><input name="tfEmail" type="text" id="tfEmail" value="email" maxlength="90" /></td>
</tr>
<tr>
<td height="20">Confirm</td>
<td height="20" colspan="2"><input name="tfEmailConfirm" type="text" id="tfEmailConfirm" value="emailconfirm" maxlength="90" /></td>
</tr>
<tr>
<td height="20">&nbsp;</td>
<td valign="middle">
</td>
</tr>
<tr>
<td height="20">Country</td>
<td height="20" colspan="2" valign="middle"><select name="sCountry" id="sCountry">
<option>Select an option</option>
<option>Australia</option>
<option>France</option>
<option>Japan</option>
<option>America</option>
</select></td>
</tr>
<tr>
<td colspan="3" align="right"><label><!-- type=submit -->
</label>
<hr /></td>
</tr>
</table>
<table width="600" border="0" align="center">
<tr>
<td width="192"></td>
<td width="252"><div align="right">
<input type="submit" name="bSignup" id="bSignup" value="Signup" />
</div></td>
</tr>
</table>
</form>
<?php
if ($bContinue)
{
$mysqlCon = mysql_connect("localhost", "root", "root");
if (!$mysqlCon)
{
die("Unable to connect, ".mysql_error());
}
mysql_select_db("main", $mysqlCon);

$sqlQuery = "INSERT INTO Users (id,username, password, email, country) VALUES ('0','$username','$password','$email', '$country')";
$queryResult = mysql_query($sqlQuery, $mysqlCon);
if (!$queryResult)
{
die("Error, ".mysql_error());
}
header('Location: signupcomplete.php'); //this doesn't seem to work
mysql_close($mysqlCon);
}
?>
</body>
</html>

Any idea?

Kind Regards

angelwatt
Aug 19, 2009, 08:18 PM
Yup, as I said, you're using it after you outputted things to the screen. All that HTML at the top, that output. Can't do it that way.

MythicFrost
Aug 19, 2009, 08:20 PM
How could I do what I want then? do you know?

Kind Regards

angelwatt
Aug 19, 2009, 08:38 PM
How could I do what I want then? do you know?

Kind Regards

Simply put, do the PHP part before you start outputting anything.

MythicFrost
Aug 19, 2009, 09:01 PM
Alright, I'm doing that, no success, is "Location: signupcomplete.php" correct, will it know to look in the root folder for it?

This is at the VERY top of my code, all HTML is below it.

<?php
//use $_SESSION to store error information
$username = mysql_real_escape_string($_POST["tfUsername"]);
$password = mysql_real_escape_string($_POST["tfPassword"]);
$passwordConfirm = mysql_real_escape_string($_POST["tfPasswordConfirm"]);
$email = mysql_real_escape_string($_POST["tfEmail"]);
$emailConfirm = mysql_real_escape_string($_POST["tfEmailConfirm"]);
$country = mysql_real_escape_string($_POST["sCountry"]);
$bBlank=false;
$bUsernameTaken=false;
if ($username == "")
{
$bBlank=true;
}
$bPwNotPwCon=!($password == $passwordConfirm);
$bEmNotEmCon=!($email == $emailConfirm);
//Styles
/*function getStyle()
{
$styleFont="arial";
$styleColor="red";
$styleFontSize="11";
$s = "<p style=\"font-family:".$styleFont."; color:".$styleColor."; font-size:".$styleFontSize."px\">";
return $s;
}
$sUserTaken = getStyle()."That username is already taken</p>";
$sPwNotPwCon = getStyle()."Your password and confirm password do not match</p>";
$sEmNotEmCon = getStyle()."Your email and confirm email do not match</p>";*/
//End Styles
$mysqlCon = mysql_connect("localhost", "root", "root");
if (!$mysqlCon)
{
die("Unable to connect, ".mysql_error());
}
mysql_select_db("main", $mysqlCon);

$query = sprintf("SELECT * FROM Users WHERE username = '%s'", mysql_real_escape_string($username));
$result = mysql_query($query, $mysqlCon);

$iNumRows = mysql_num_rows($result);
if ($iNumRows > 0)
{
$bUsernameTaken=true;
}
$bContinue = !$bUsernameTaken and !$bPwNotPwCon and !$bEmNotEmCon and !bBlank;
//echo (sprintf("bCon %b, bUser %b, bPwNotPwon %b, bEmNotEmCon %b, bBlank %b", $bContinue, !$bUsernameTaken, !$bPwNotPwCon, !$bEmNotEmCon, !$bBlank));
if ($bContinue)
{
$sqlQuery = sprintf("INSERT INTO Users (id,username, password, email, country) VALUES ('%s','%s','%s','%s', '%s')", 0, mysql_real_escape_string($username), mysql_real_escape_string($password), mysql_real_escape_string($email), mysql_real_escape_string($country));
$queryResult = mysql_query($sqlQuery, $mysqlCon);
if (!$queryResult)
{
die("Error, ".mysql_error());
}
header('Location: signupcomplete.php');
}
mysql_close($mysqlCon);
?>

I can't see any output there

Kind Regards

angelwatt
Aug 20, 2009, 08:04 AM
Alright, I'm doing that, no success, is "Location: signupcomplete.php" correct, will it know to look in the root folder for it?

No, it'll need the full URL

header('Location: '. $_SERVER['DOCUMENT_ROOT'] .'/signupcomplete.php');