Page 1 of 1

Need Help !! Getting Error in sending headers

Posted: Thu Oct 23, 2003 1:44 pm
by sree78
Hi I am creating a quiz module where every new user have to register before they get into the quiz page. Their Information will be stored in a database and as well as the results of the quiz. My code is as below:-


<?php


$first_name = pg_escape_string($first_name);
$last_name = pg_escape_string($last_name);
$email = pg_escape_string($email);
$birthdate = $month.$day;

$sql = "SELECT *
FROM person
WHERE first_name='$first_name' AND last_name='$last_name'
AND birthdate = '$birthdate'";

$result = pg_query($sql);
$row = pg_fetch_row($result);
if ($row == ""):

//Add
$sql = "SELECT org_id
FROM org
WHERE name = '$org'";
$result = pg_query($sql);
$row = pg_fetch_row($result);
$org_id = $row[0];
$sql = "INSERT INTO person
(first_name, last_name, email, birthdate, status, org_id)
VALUES ('$first_name', '$last_name', '$email', '$birthdate', '$status', '$org_id')";

print $sql;
$result= pg_query($sql);



header ("Location:$redirect");
setcookie ("reg_id","$person_id", 0, "/");

else:
//User exists. Send 'em back
$errstr = 'Only one person can be registered with the same name and ';
$errstr .= 'birthday. Someone has already logged in under your name. ';
$errstr .= 'If you have registered before, go to the short registration ';
$errstr .= 'form. If this is your first time, type your nickname or add ';
$errstr .= 'a middle initial in the "First Name" box.';
endif;
?>

If you can see the code in BOLD.. I cannot get the page to redirect once the user registers but instead I am getting an error like this below:-

Warning: Cannot modify header information - headers already sent by


At the end of my html page I have this piece of code which tells the page to redirect:-

Code: Select all

<input type=hidden name="redirect" value="quiz.php">
<table cellpadding="10">
  <tr><td align=center><input type=button value="Register" onClick="largeReg(document.reg);"></td></tr>
</table>
</form>
Any help would be highly appreciated!!!
?>

Posted: Thu Oct 23, 2003 1:46 pm
by brewmiser
Is there any HTML code above this?

Posted: Thu Oct 23, 2003 1:48 pm
by qads

Posted: Thu Oct 23, 2003 1:49 pm
by Kriek
Allow me to elaborate: That error is caused by because you are trying to send header information AFTER you have sent output to the browser. Usually this is a simple case of reordering a statement or removing white space before and/or after the PHP start and end tags, however you can also use output buffering with ob_start() and ob_end_flush() which will allow you to place headers and content in any order you want to. Note output buffering was originally designed to solve HTTP header errors. Although ob_end_flush() isn't needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start().

Code: Select all

<?php
    ob_start();
    if(!get_magic_quotes_gpc()) &#123;
        $first_name = pg_escape_string($first_name);
        $last_name = pg_escape_string($last_name);
        $email = pg_escape_string($email);
    &#125;
    $birthdate = $month . $day;
    $sql = "SELECT * FROM person WHERE first_name='$first_name' AND last_name='$last_name' AND birthdate='$birthdate'";
    $result = pg_query($sql) or die(pg_last_error());
    $row = pg_fetch_row($result) or die(pg_last_error());
    if(empty($row)) &#123;
        $sql = "SELECT org_id FROM org WHERE name='$org'";
        $result = pg_query($sql) or die(pg_last_error());
        $row = pg_fetch_row($result) or die(pg_last_error());
        $org_id = $row&#1111;0];
        $sql = "INSERT INTO person(first_name, last_name, email, birthdate, status, org_id) VALUES('" . $first_name . "', '" . $last_name . "', '" . $email . "', '" . $birthdate . "', '" . $status . "', '" . $org_id . "')";
        $result = pg_query($sql) or die(pg_last_error());
        setcookie('reg_id', $person_id, 0, '/');
        if(!headers_sent()) &#123;
            header ('Location: ' . $redirect);
            exit();
        &#125;
    &#125; else &#123;
        $errstr = 'Only one person can be registered with the same name and ';
        $errstr .= 'birthday. Someone has already logged in under your name. ';
        $errstr .= 'If you have registered before, go to the short registration ';
        $errstr .= 'form. If this is your first time, type your nickname or add ';
        $errstr .= 'a middle initial in the "First Name" box.';
    &#125;
?>
<INPUT TYPE="hidden" NAME="redirect" VALUE="quiz.php">
<TABLE cellpadding="10">
  <TR>
    <TD ALIGN=center><INPUT TYPE=button VALUE="Register" onClick="largeReg(document.reg);"></TD>
  </TR>
</TABLE></FORM>
<?php
    ob_end_flush();
?>

Posted: Fri Oct 24, 2003 2:13 pm
by sree78
Hi.. Kriek..

Thank you so much for your quick reply.. I have been out of the office and I have just tried your method. The page does not produce any errors but I am still not getting to the redirect page. The browser is blank white when I hit the register button.

Any help is highly appreciated...

Once again thanks

Posted: Fri Oct 24, 2003 2:38 pm
by sree78
I have managed to solve the problem... it was just a few editing... once again thanks a lot Kriek...

Posted: Sat Oct 25, 2003 11:19 am
by Kriek
No problem, glad I could be of assistance to you.