Log in

View Full Version : PHP Error: Cannot modify header information



devil_vin
09-07-2007, 04:43 AM
I am new to PHP.I have a login page having an error "Cannot modify header information" .After successful login,the page will redirect to homepage however myscreen still remain in login page. Thanks...


login.php


<?php
include ('dbconn.cfg');
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
if (isset($_POST['submit']))
{//1st if

$tbl_name = "member";
$sql = "SELECT * FROM $tbl_name WHERE email = \"" . $_REQUEST['email'] . "\" ";

//echo $sql;
$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);
if ($numrow != 0)
{//2nd if

$sql = "SELECT name,telephone,address FROM $tbl_name
WHERE email = '" . $_REQUEST['email'] . "' AND password = '" .$_REQUEST['password'] . "' ";

$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");
$numrow = mysql_num_rows($result);
if ($numrow != 0)
{//3rd if

$row_array = mysql_fetch_row($result);
$_SESSION['gmembername'] = $row_array[0];
$_SESSION['gmembertel'] = $row_array[1];
$_SESSION['gmemberaddr'] = $row_array[2];
$_SESSION['gmemberid'] = $_REQUEST['email'];

//$relative_url = "index.php";
header("Location: http://localhost/www/index.php?frame=main.htm");
//echo "Welcome $row_array[0] <br>";

}//3rd if
else
{
$status = "Wrong Password!";
}
}//2nd if
else
{
$status = "Member Id not found!";
}
}//1st if

?>

<html>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>

<body bgcolor="#6F7A9E" text="#0000FF" vlink="#FF0000" alink="#FFFF00" >
<p align="center"> <font size="3" face="Times New Roman, Times, serif"><b>WELCOME
TO GOLDEN TIME CINEMA!</b></font></p>
<p align="center"><b><font size="3" face="Times New Roman, Times, serif">.........
THE MOST COMPREHENSIVE ONLINE </font></b></p>
<p align="center"><b><font size="3" face="Times New Roman, Times, serif">CINEMA
TICKET RESERVATION SERVICE.</font></b></p>
<p>&nbsp;</p>
<p align="center"><b><font size="3" face="Times New Roman, Times, serif">You are
required to login before proceeding, </font></b></p>
<p align="center"><b><font size="3" face="Times New Roman, Times, serif">if you
do not have an account, you can sign up <a href="index.php?frame=signup">HERE</a></font></b></p>
<? if (isset($status))
{
echo "<center><font color=\"red\">$status</font></center>";
} ?>
<form name="form1" method="post" action="http://localhost/www/index.php?frame=login">
<table border=0 align=center>
<tr><td>Email Address: </td>
<td><input type="text" name="email" value=<? if (isset($status))
{
echo $_REQUEST['email'];
} ?>></td></tr>
<tr><td>Password: </td>
<td><input type="password" name="password"></td></tr>
<tr><td colspan="2" align=center><input name="submit" type="submit" onClick="MM_validateForm('email','','R','password','','R');return document.MM_returnValue" value="Submit"></td></tr>
</table>
</form>
</body>
</html>

codeexploiter
09-07-2007, 05:25 AM
PHP's header function generates information to include in the header and so it must be called before other information is sent out from your PHP code. This is because PHP assumes as soon as it sees any output via print, echo or of regular HTML that the header information is complete and can be sent.

Instead of using the PHP header() you can try another method that works in the client-side part using JavaScript


$redirectUrl = "http://localhost/www/index.php?frame=main.htm";
print "<script type=\"text/javascript\">";
print "window.location.href = '$redirectUrl'";
print "</script>";

Twey
09-07-2007, 05:43 AM
That's a terrible solution!

codeexploiter
09-07-2007, 06:21 AM
That's a terrible solution!
That was just an alternative. If a PHP based solution they needed then they have to alter their PHP code according to that.

Twey
09-07-2007, 06:37 AM
I suspect it's as simple as whitespace or HTML before the <?php tag.

devil_vin
09-07-2007, 06:54 AM
PHP's header function generates information to include in the header and so it must be called before other information is sent out from your PHP code. This is because PHP assumes as soon as it sees any output via print, echo or of regular HTML that the header information is complete and can be sent.

Instead of using the PHP header() you can try another method that works in the client-side part using JavaScript


$redirectUrl = "http://localhost/www/index.php?frame=main.htm";
print "<script type=\"text/javascript\">";
print "window.location.href = '$redirectUrl'";
print "</script>";

Thanks a lot..this is what i want...:) :)

Twey
09-07-2007, 12:53 PM
Don't use that -- it's an ugly solution that uses a hackish Javascript redirect rather than a proper HTTP one. Simply don't output anything before calling header(), and you'll be fine.