Jump to content

[SOLVED] Login + Headers = Headache


steveangelis

Recommended Posts

 

I know this is going to be a lot of code but I am going to toss it all out first:

 

<?PHP
if (isset($_COOKIE['user']) && isset($_COOKIE['pass'])) {
    $query2 = "SELECT * from member WHERE name='".mysql_real_escape_string($_COOKIE["user"])."' and pass='".mysql_real_escape_string($_COOKIE["pass"])."'" or die(mysql_error);
    $result2 = mysql_query($query2) or die("The information you entered does not match our records.");
    $row2=mysql_fetch_array($result2) or die(header('location:index.php?p=badcookie'));
    if ((isset($_COOKIE['user']) == $row2['name'])&& (isset($_COOKIE['pass']) == md5($row2['pass'])))
    {

       include('modules/console.php');
    }
}
else
{


if (isset($_GET['l'])) {
	if ($_GET['l'] == '1')
	{
		$username2=$_POST['username'];
		$password2=$_POST['password'];

		$query = "SELECT * from member WHERE name='$username2'";
		$result = mysql_query($query) or die("The information you entered does not match our records.");
		$row=mysql_fetch_array($result);
		$dbpassword = $row['pass'];
		$userid = $row['id'];
		//$mdp=$row['password'];


		if ($dbpassword == md5($password2))
		{

			//authenticate user

			$password5=md5($password2);
			setcookie ("id", $userid);
			setcookie ("user", $username2);
			setcookie ("pass", $password5);
			mysql_query("update member set lastlogin='".gmdate("M d Y H:i:s")."' where id=".$userid) or die(" ".mysql_error());
			mysql_close($linkid);
			//$username=$_COOKIE["user"];
			include('modules/console.php');
			//Header ("location: index2.php?p=status");
			//test display authentication

		}
		else
		{
			echo "<center>Login Failed. <br><a href='index.php?p=cp'>Back</a></center>";
		}

	}
	else
	{

	}?>

	<?PHP
}
else
{

//login html code here

}
}
?>

 

Now I know that the headers are susposed to come BEFORE any html code, and for some reason this worked with my WAMP server, but now not on my real one.  This page comes up as part of a template on my index page.  This is a file called cp.php and it is loaded like index.php?p=cp and so on.  Therefore with this method the headers are always loaded with the index page which caused my problem.  Can anyone see a way of fixing my header problem?

 

Link to comment
https://forums.phpfreaks.com/topic/170301-solved-login-headers-headache/
Share on other sites

Now I know that the headers are susposed to come BEFORE any output

Fixed.

 

Is error reporting turned on?  Perhaps an error is being reported in production and causing premature output.

 

I see you calling mysql_query() before making a database connection and using or die() which echoes output.

I cut out some code to save some space.  It connects to the DB perfectly fine and everything.  The problem is I am getting a header error because the headers are being loaded after the page because of how the page loads via the template and I do not know if there is a way to get around it.  The main problem is at this part:

 

setcookie ("id", $userid);

setcookie ("user", $username2);

setcookie ("pass", $password5);

 

This is where the header error is coming up.

 

Warning: Cannot modify header information - headers already sent by (output started at /home/sf9761/public_html/ois/index.php:19) in /home/sf9761/public_html/ois/modules/cp.php on line 46

 

Warning: Cannot modify header information - headers already sent by (output started at /home/sf9761/public_html/ois/index.php:19) in /home/sf9761/public_html/ois/modules/cp.php on line 47

 

Warning: Cannot modify header information - headers already sent by (output started at /home/sf9761/public_html/ois/index.php:19) in /home/sf9761/public_html/ois/modules/cp.php on line 48

 

That is the errors I get which is those three lines of code from the previous post.

ob_start() and echo ob_get_clean() will fix the problem.  Since you are dealing with multiple files, where you put them will make a difference.

 

ob_start() should really be at the very beginning of execution and echo ob_get_clean() should be at the very end.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.