FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Header problems





guitarcrazy087
Ok, so I'm making a registration form. It works by checking if my content variable is set, and if set, does certain things depending on the value. The whole registration process is supposed to take place in one file.

Here's the code for register.php :

Code:

<?php

$connection = mysql_connect('localhost', 'henry', '') or die();

mysql_select_db('joehenry', $connection) or die();

include ($_SERVER["DOCUMENT_ROOT"] . "/header.php");

// the following are variables retrieved from the form further down.

$content     = $_POST['content'];
$firstname   = $_POST['firstname'];
$lastname    = $_POST['lastname'];
$displayname = $_POST['displayname'];
$email       = $_POST['email'];
$username    = $_POST['username'];
$password    = $_POST['password'];
$confirmpw   = $_POST['confirmpw'];


// this displays the registration form the user will see after loading the page for the first time.
// I also display the form if the last try at filling it out failed.
if ($content == NULL || $content == "badregistration") {

?>

// javascript file for validating the form before submission. I can provide this if needed.
<script src="registercheck.js"></script>

<h2>Register</h2>

// just my registration form

<form name="register" method="post" action="register.php" onMouseMove="checkFirstName(); checkLastName(); checkDisplayName(); checkEmail(); checkUserName(); checkPass();">

<div class="halfleft"><small>*First Name (will not be displayed):</small></div>
<div class="halfright"><table><tr><td><input type="text" id="firstname" name="firstname" <?php if ($firstname !== NULL) {echo "value=\"$firstname\" ";} ?> onKeyUp="checkFirstName()" /></td>
<td id="displayfirstname"></td></tr></table></div><br /><br />

<div class="halfleft"><small>*Last Name (will not be displayed):</small></div>
<div class="halfright"><table><tr><td><input type="text" id="lastname" name="lastname" <?php if ($lastname !== NULL) {echo "value=\"$lastname\" ";} ?> onKeyUp="checkLastName()" /></td>
<td id="displaylastname"></td></tr></table></div><br /><br />

<div class="halfleft"><small>*Display Name (will be displayed):</small></div>
<div class="halfright"><table><tr><td><input type="text" id="displayname" name="displayname" <?php if ($displayname !== NULL) {echo "value=\"$displayname\" ";} ?> onKeyUp="checkDisplayName()" /></td>
<td id="displaydisplayname"></td></tr></table></div><br /><br />

<div class="halfleft"><small>*Email Address (will not be displayed):</small></div>
<div class="halfright"><table><tr><td><input type="text" id ="email" name="email" <?php if ($email !== NULL) {echo "value=\"$email\" ";} ?> onKeyUp="checkEmail()" /></td>
<td id="displayemail"></td></tr></table></div><br /><br />

<div class="halfleft"><small>*Username (used only for logging in):</small></div>
<div class="halfright"><table><tr><td><input type="text" id="username" name="username" <?php if ($username !== NULL) {echo "value=\"$username\" ";} ?> onKeyUp="checkUserName()" /></td>
<td id="displayusername"></td></tr></table></div><br /><br />

<div class="halfleft"><small>*Password :</small></div>
<div class="halfright"><table><tr><td><input type="password" id="password" name="password" <?php if ($password !== NULL) {echo "value=\"$password\" ";} ?> onKeyUp="checkPass()" /></td>
<td id="displaypw"></td></tr></table></div><br /><br />

<div class="halfleft"><small>*Confirm password:</small></div>
<div class="halfright"><table><tr><td><input type="password" id="confirmpw" name="confirmpw" <?php if ($confirmpw !== NULL) {echo "value=\"$confirmpw\" ";} ?> onKeyUp="checkPass()" /></td>
<td id="displaypwconfirm"></td></tr></table></div><br /><br /><br />

// here i set the content variable to register

<input type="hidden" name="content" value="register" />

<div class="textright"><input type="submit" value="register" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="start over" onMouseOut="checkFirstName(); checkLastName(); checkDisplayName(); checkEmail(); checkUserName(); checkPass();" /></div>

</form>

<?php

}

// and on submission the page loads the following.

else if ($content == "register") {

echo "<form method=\"post\" action=\"register.php\">";

$content = "goodregistration";

// i start by setting the content variable to goodregistration.
// if there are any errors in the form i change that.

$firstname = trim($firstname);
$fnl = strlen($firstname);
if ($fnl < 2) {
   $content = "badregistration";
   $fne = "The firs name you entered is too short...";
   echo "<input type=\"hidden\" name=\"firstname\" value=\"$firstname\" />";
}
else if ($fnl > 20) {
   $content = "badregistration";
   $fne = "The first name you entered is too long...";
   echo "<input type=\"hidden\" name=\"firstname\" value=\"$firstname\" />";
}
else {
   $firstname = addslashes($firstname);
}

$lastname = trim($lastname);
$lnl = strlen($lastname);
if ($lnl < 2) {
   $content = "badregistration";
   $lne = "The last name you entered is too short...";
   echo "<input type=\"hidden\" name=\"lastname\" value=\"$lastname\" />";
}
else if ($lnl > 20) {
   $content = "badregistration";
   $lne = "The last name you entered is too long...";
   echo "<input type=\"hidden\" name=\"lastname\" value=\"$lastname\" />";
}
else if ($content == "badregistration") {
   echo "<input type=\"hidden\" name=\"lastname\" value=\"$lastname\" />";
}
else {
   $lastname = addslashes($lastname);
}

$displayname = trim($displayname);
$dnl = strlen($displayname);
if ($dnl < 2) {
   $content = "badregistration";
   $dne = "The displayname you entered is too short...";
   echo "<input type=\"hidden\" name=\"displayname\" value=\"$displayname\" />";
}
else if ($dnl > 20) {
   $content = "badregistration";
   $dne = "The display name you entered is too long...";
   echo "<input type=\"hidden\" name=\"displayname\" value=\"$displayname\" />";
}
else if ($content == "badregistration") {
   echo "<input type=\"hidden\" name=\"displayname\" value=\"$displayname\" />";
}
else {
   $displayname = addslashes($displayname);
}



$username = trim($username);
$unl = strlen($username);
$query = mysql_query("SELECT username FROM users WHERE username = \"$username\"");
if (mysql_num_rows($query) !== 0) {
   $content = "badregistration";
   $une1 = "The username you entered is alreay taken...";
   echo "<input type=\"hidden\" name=\"username\" value=\"$username\" />";
}

if ($unl < 6) {
   $content = "badregistration";
   $une2 = "The username you entered is too short...";
   echo "<input type=\"hidden\" name=\"username\" value=\"$username\" />";
}
else if ($unl > 20) {
   $content = "badregistration";
   $une2 = "The username you entered is too long...";
   echo "<input type=\"hidden\" name=\"username\" value=\"$username\" />";
}
else if ($content == "badregistration") {
   echo "<input type=\"hidden\" name=\"username\" value=\"$username\" />";
}
else {
   $username = addslashes($username);
}

if ($password !== $confirmpw) {
   $content = "badregistration";
   $pwe1 = "The passwords you entered did not match...";
}

if (strlen($password) < 6) {
   $content = "badregistration";
   $pwe2 = "The password you entered was too short...";
}
else if (strlen($password) > 20) {
   $content = "badregistration";
   $pwe2 = "The password you entered was too long...";
}
else {
   $password = addslashes($password);
}

$date = date(YYYY-MM-DD);

if ($content == "goodregister") {
   echo "everything passed!";
   $register = mysql_query("INSERT INTO users (firstname, lastname, username, password, displayname, email, registerdate) VALUES ($firstname, $lastname, $username, PASSWORD($password), $displayname, $email, $date)");
   if ($register) {
      echo "woot your registered";
   }
   else {
      echo "database problems";
   }
}
else {

// PROBLEM HERE WITH THE HEADER!!!

   echo "<input type=\"hidden\" name=\"content\" value=\"badregistration\" />";
   echo "</form>";
   $header = "http://" . $_SERVER['SERVER_NAME'] . "/register.php";
   header("Location: $header");
}

}

// if all fields passed the check...

else if ($content == "goodregistration") {
   echo "Thank you very much for registering!";
}
else {
   echo "<div class=\"warning\">ERROR: Opps!  Something went wrong.  If you were trying to register please try again.</div>";
}

include ($_SERVER["DOCUMENT_ROOT"] . "/footer.php");

?>


Now for my problem with the header. All the other code works. I've tested that by using an echo command instead of the header if the form doesn't validate. Anyway, here's the php error i get:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/xampp/xamppfiles/htdocs/header.php:10) in /Applications/xampp/xamppfiles/htdocs/register.php on line 190

Here's a link to the php manual on headers.

Any help is appreciated, and thank you!

p.s. I know that's a ton of code up there and anyone who bothers to read it is very dedicated. I will reward whoever finds the answer to my problem with a generous donation of my almost $300 frihost dollars =]
rvec
Quote:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.


Quote:
Code:
echo "<input type=\"hidden\" name=\"content\" value=\"badregistration\" />";
   echo "</form>";
   $header = "http://" . $_SERVER['SERVER_NAME'] . "/register.php";
   header("Location: $header");


That's the problem Wink
imagefree
You have correctly identified the problem. Its here:


Code:
   echo "<input type=\"hidden\" name=\"content\" value=\"badregistration\" />";
   echo "</form>";
   $header = "http://" . $_SERVER['SERVER_NAME'] . "/register.php";
   header("Location: $header");


The problem is that the code is outputting HTML before the header function functions Very Happy

Let me explain:
when server sends page back to browser, it first sends few header information to the browser to provide browser useful information about the page, and tells the browser how to treat the page. The problem with your code is that you are outputting html far before the use of header(). What is wrong with this is that header("Location: STRING") tells the browser to redirect to a new page, but when you have already sent HTML to the browser, it fails to redirect the browser to a new page. (infact it fails to send header information to the browser and shows the error).

The same type of error occures when you output some HTML (even a space), and set a cookie, or send custom header information to browser.

Need more help>?
rvec
the problem isn't that no headers are send to the user, the problem is that the headers already are send.

When using the header function you try to edit the headers before they are send to the user. The server can make perfectly valid headers itself, and will do so automaticly. If you use the header function you edit the headers before you send them, and when sending output to the user the server will send the headers (a client needs headers to open content). In your case, you are trying to edit headers which are already send to the browser.
guitarcrazy087
Ah, ok, I think I understand now. Thanks, but I still need a way to refresh the page and send the values back to the form if they don't validate... Could I use a session for this instead of the inputs I'm using? Or would that still not work because I have HTML earlier in the page (the big form)?

Thanks for the help so far guys!
rvec
sessions aren't put in the header, you can always use those. But to forward you can use output buffering to make sure the output isn't send to the browser (so you can modify headers) or redirect with html/javascript.

For the html and javascript way:
http://www.pageresource.com/jscript/jredir.htm

for the output buffering:
http://php.net/ob_start
guitarcrazy087
Ok, well, I revised my code, made it a little more clear, less chunky, and came up with this:

Code:
<?php

$connection = mysql_connect('localhost', 'henry', '') or die("<br /><div class=\"warning\">SYSTEM ERROR: Cannot connect to the server. It may be temprarilly down. Please refresh the page and try again...</div>");

mysql_select_db('joehenry', $connection) or die("<br /><div class=\"warning\">SYSTEM ERROR: Cannot connect to the selected database. It may be temprarilly down. Please refresh the page and try again...</div>");

$name = "Register";

include ($_SERVER["DOCUMENT_ROOT"] . "/header.php");

include ($_SERVER["DOCUMENT_ROOT"] . "/left.php");

$content     = $_POST['content'];
$firstname   = $_POST['firstname'];
$lastname    = $_POST['lastname'];
$displayname = $_POST['displayname'];
$email       = $_POST['email'];
$username    = $_POST['username'];
$password    = $_POST['password'];
$confirmpw   = $_POST['confirmpw'];

$load = $_POST['load'];
$content = "goodregistration";


$firstname = trim($firstname);
$fnl = strlen($firstname);
if ($fnl < 2) {
   $content = "badregistration";
   $fne = "The firs name you entered is too short...";
}
else if ($fnl > 20) {
   $content = "badregistration";
   $fne = "The first name you entered is too long...";
}
else {
   $firstname = addslashes($firstname);
}


$lastname = trim($lastname);
$lnl = strlen($lastname);
if ($lnl < 2) {
   $content = "badregistration";
   $lne = "The last name you entered is too short...";
}
else if ($lnl > 20) {
   $content = "badregistration";
   $lne = "The last name you entered is too long...";
}
else {
   $lastname = addslashes($lastname);
}


$displayname = trim($displayname);
$dnl = strlen($displayname);
if ($dnl < 2) {
   $content = "badregistration";
   $dne = "The displayname you entered is too short...";
}
else if ($dnl > 20) {
   $content = "badregistration";
   $dne = "The display name you entered is too long...";
}
else {
   $displayname = addslashes($displayname);
}


$username = trim($username);
$unl = strlen($username);
$query = mysql_query("SELECT username FROM users WHERE username = \"$username\"");
if (mysql_num_rows($query) !== 0) {
   $content = "badregistration";
   $une1 = "The username you entered is alreay taken...";
}

if ($unl < 6) {
   $content = "badregistration";
   $une2 = "The username you entered is too short...";
}
else if ($unl > 20) {
   $content = "badregistration";
   $une2 = "The username you entered is too long...";
}
else {
   $username = addslashes($username);
}

if ($password !== $confirmpw) {
   $content = "badregistration";
   $pwe1 = "The passwords you entered did not match...";
}

if (strlen($password) < 6) {
   $content = "badregistration";
   $pwe2 = "The password you entered is too short...";
}
else if (strlen($password) > 20) {
   $content = "badregistration";
   $pwe2 = "The password you entered is too long...";
}
else {
   $password = addslashes($password);
}

if (strlen($confirmpw) < 6) {
   $content = "badregistration";
   $cpwe = "The confirmation password you entered is too short...";
}
else if (strlen($confirmpw) > 20) {
   $content = "badregistration";
   $cpwe = "The confirmation password you entered is too long...";
}

$date = date("Y-m-d H:i:s");

if ($content == "goodregistration") {
   $register = mysql_query("INSERT INTO users VALUES ('$firstname', '$lastname', '$username', PASSWORD('$password'), '$displayname', '$email', '$date'");
   if ($register) {
      echo "Your registered!";
   }
   else {
      echo "<div class=\"warning\">ERROR: Could not connect to database... Please re-submit the form.</div>";
      include ("registerform.php");
   }
}
else {
   if ($load == 1) {
      if (isset($fne)) {
         echo $fne . "<br />\n";
      }
      if (isset($lne)) {
         echo $lne . "<br />\n";
      }
      if (isset($dne)) {
         echo $dne . "<br />\n";
      }
      if (isset($une1)) {
         echo $une1 . "<br />\n";
      }
      if (isset($une2)) {
         echo $une2 . "<br />\n";
      }
      if (isset($pwe1)) {
         echo $pwe1 . "<br />\n";
      }
      if (isset($pwe2)) {
         echo $pwe2 . "<br />\n";
      }
      if (isset($cpwe)) {
         echo $cpwe . "<br />\n";
      }
   }
   include ("registerform.php");
}

include ($_SERVER["DOCUMENT_ROOT"] . "/right.php");

include ($_SERVER["DOCUMENT_ROOT"] . "/bottomnav.php");

include ($_SERVER["DOCUMENT_ROOT"] . "/footer.php");

?>


It works fine except that for some reason I can't connect to my database. My server is running fine and everything... I'm just completely lost here... Thanks for you help guys.

Because you both helped but I didn't necessarily use your ideas, I'll just split the cash between you. Thanks again!
guitarcrazy087
Ok, I got it. I had some problems with my insert statement.

Thanks for all the help guys!
imagefree
Code:
"INSERT INTO users ( FIELD1 , FIELD2 , FIELD3 , FIELD4 , FIELD5 ,  FIELD6 , FIELD7 ) VALUES ( '$firstname', '$lastname', '$username', ".PASSWORD('$password')." , '$displayname', '$email', '$date'"

where FIELD1, FIELD2 represent table field names.
guitarcrazy087
Yes, I've got it now. =]

Thanks for your help guys!
Related topics
HELP: setcookie() and header problems
[ java scripts ] email service
Facing Many Problems
New forum area (computer problems)
[TUTOR] A Short Intro to GD
CANT UPLOAD .htaccess, .htpasswd, .header, .footer
GD-library header probs
Okay, I'm done trying to use CSS for layout.
[tutor] How to protect images without htaccess using PHP
phpbb problem....
Problems With Index Page
div problems
problems with phpBB2 forum
session_destroy problems
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.