Page 1 of 1

Problem setting headers.

Posted: Tue Nov 04, 2008 4:44 pm
by oscardog
I know, 3rd topic today , lots of problems. This should be the final one!!!

I have the following code:

Code: Select all

<?php
if(isset($_POST['submit'])){
if (!$_POST['username'] | !$_POST['password'])
{
die('You did not complete all the required fields. Please go to the <a href="login.php">login page</a>');
}
 
include("connection.php");
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
 
$password = mysql_real_escape_string($password);
$password = md5($password);
$query = "SELECT * FROM `members` WHERE `password`  = '".$password."' ";
$result1 = mysql_query($query) or die("Can't execute insert query: " . mysql_error());
$row = mysql_fetch_array($result1);
$passwordcheck = $row['password'];
$passwordcheck = stripslashes($passwordcheck);
$sql = "SELECT * FROM members WHERE username='$username' and password='$password'";
$result2 = mysql_query($sql);
 
$count=mysql_num_rows($result2);
if($count == 1 && $password == $passwordcheck) {
$_SESSION['username'] = $username;
session_write_close();
header("location: guild_registration.php");
}
else
{
echo "Wrong Username or Password <br />";
echo "Please try again"; ?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="loginverify.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login</strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" maxlength="20" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" maxlength="20" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
}
}
if(!isset($_POST['submit'])){
die('The server requires you login to access this page. Please go to the <a href=\"login.php\">login page</a>');
}
?>
If i submit the wrong username/password, it returns the error. If i enter the correct details, it loads the following error message:

'Warning: Cannot modify header information - headers already sent by (output started at /www/1stfreehosting.com/a/s/h/ashpea1/htdocs/verifylogin.php:10) in /www/1stfreehosting.com/a/s/h/ashpea1/htdocs/verifylogin.php on line 38'

(ignore the line reference when looking at the code above, i have other code about that. Line 38 is the line where it sets the header.)
Now i have read some FAQs about this, something about removing white space. Well i have done that, to no avail so can anyone poiint out whats wrong? :)

Re: Problem setting headers.

Posted: Tue Nov 04, 2008 4:50 pm
by Jade
Do you have a space/header output in the connection problem? The error message means something is already sending out header information that's messing everything up. That could be any kind of HTML or header() calls.

Also, do you realize if (!$_POST['username'] | !$_POST['password']) should be if (!$_POST['username'] || !$_POST['password'])

Re: Problem setting headers.

Posted: Tue Nov 04, 2008 4:57 pm
by oscardog
Oh yeh thanks, typo DOH.

And connection problem? There is absolutely no header output in the connection.php file, would <title> tags count as setting a header? Guess it would... Remove those?

Re: Problem setting headers.

Posted: Tue Nov 04, 2008 5:00 pm
by Jade
Are you echoing the title tags? If you echo any kind of html before the session_start() you get that problem. Session_start() has to be sent out with the header in order to create the session data/vars on the server. If for some reason header data has already been sent and you call session_start() it will crap out with that error.

Re: Problem setting headers.

Posted: Tue Nov 04, 2008 5:01 pm
by oscardog

Code: Select all

<?php session_start(); ?>
<!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=iso-8859-1" />
<title>DOFUS Guildopedia ::: Free guild pages for all UK/International Servers :: Verifying Login...</title>
</head>
 
<body>
<?php
if(isset($_POST['submit'])){
if (!$_POST['username'] | !$_POST['password'])
{
die('You did not complete all the required fields. Please go to the <a href="login.php">login page</a>');
}
 
include("connection.php");
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
 
$password = mysql_real_escape_string($password);
$password = md5($password);
$query = "SELECT * FROM `members` WHERE `password`  = '".$password."' ";
$result1 = mysql_query($query) or die("Can't execute insert query: " . mysql_error());
$row = mysql_fetch_array($result1);
$passwordcheck = $row['password'];
$passwordcheck = stripslashes($passwordcheck);
$sql = "SELECT * FROM members WHERE username='$username' and password='$password'";
$result2 = mysql_query($sql);
 
$count=mysql_num_rows($result2);
if($count == 1 && $password == $passwordcheck) {
$_SESSION['username'] = $username;
session_write_close();
header("location: guild_registration.php");
}
else
{
echo "Wrong Username or Password <br />";
echo "Please try again"; ?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="loginverify.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login</strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" maxlength="20" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" maxlength="20" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
}
}
if(!isset($_POST['submit'])){
die('The server requires you login to access this page. Please go to the <a href=\"login.php\">login page</a>');
}
?>
</body>
</html>
 
Full code, didnt change the || yet but you get the idea.

Re: Problem setting headers.

Posted: Tue Nov 04, 2008 5:26 pm
by requinix
Let's see here:
oscardog wrote:Warning: Cannot modify header information - headers already sent by (output started at /www/1stfreehosting.com/a/s/h/ashpea1/htdocs/verifylogin.php:10) in /www/1stfreehosting.com/a/s/h/ashpea1/htdocs/verifylogin.php on line 38
So, what's on line 10? Opening PHP tag, had some HTML before it so that was outputted.
Line 38?

Code: Select all

header("location: guild_registration.php");
Oops, too late to do that.

There's some code in there to determine whether the user should be sent to another page instead of the current one.
Move that logic to the top of the file so the user would get redirected before anything else happened.
And header() doesn't stop the script from executing. You need an exit(); after it, otherwise PHP will keep going like normal.

Re: Problem setting headers.

Posted: Wed Nov 05, 2008 3:33 am
by oscardog
Ah i see! Thanks alot, basically i move all of that above the HTML and it should work? :D

Thanks :)

Re: Problem setting headers.

Posted: Wed Nov 05, 2008 7:34 am
by keerthika
when u go for header function don use echo and html tags

Re: Problem setting headers.

Posted: Thu Dec 11, 2008 7:35 pm
by TaAngel1962
Hi

I am having the same problem which I never ever had before, and the script has been up for over a year and then day before yesterday it causes a similiar problem for me, but on the logout.php feature.

I had to take out a flush() line on mine but looking at your code I didn't see a flush() on yours.