Page 1 of 1

Spot the code error?

Posted: Sat Oct 12, 2002 2:08 pm
by Rob_Beard
Hey all,
I'm trying to make a member system using sessions;
I've got a login page, posting to a validation script, redirecting to the members area. The code is below; Could someone please tell me why i get these errors?

"Warning: Cannot send session cookie - headers already sent by (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 36

Warning: Cannot send session cache limiter - headers already sent (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 36

Warning: Cannot add header information - headers already sent by (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 38"

Validate Page:

Code: Select all

<html>
<head>
<title>validate page</title>
</head>
<body>
<?php
#------------------------------------------------------------------------------
#*** CHANGE THESE PARAMETERS TO USE YOUR OWN DATABASE AND USERNAME/PASSWORD ***

	$Databasename = '**********';
	$DBUsername =	'**********';
	$DBPassword = '**********';
	$Hostname = 'localhost';

#------------------------------------------------------------------------------
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the database
	$connection = mysql_connect($Hostname, $DBUsername, $DBPassword) or die(mysql_error());
	mysql_select_db($Databasename, $connection) or die(mysql_error());

	//set up the query
	$query = "SELECT * FROM Users
			WHERE user_name='$user_name' AND password='$password'";
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);

	//if there's exactly one result, the user is validated. 
	// Otherwise, he's invalid
	if($affected_rows == 1) &#123;
	$redirect_uri = "http://www.mysite.com/00826-37286/loggedin.php"; 
	//add the user to our session variables
	session_start();
		$_SESSION&#1111;'username'] = $user_name;
	Header ("Location: $redirect_uri");  
	&#125;
	
else &#123;
		echo '<font face="Verdana" color=Red size=2>Password Not Valid<br><br><br><a 

href="http://www.MYSITE.com/00826-37286/home.php?id=9">Click Here To Return To The Login 

Screen</a></font>';
	&#125;
?>


</body>
</html>
Logged In Page gives these errors when loaded:
"Warning: Cannot send session cookie - headers already sent by (output started at /home/sites/site226/web/00826-37286/loggedin.php:2) in /home/sites/site226/web/00826-37286/loggedin.php on line 3

Warning: Cannot send session cache limiter - headers already sent (output started at /home/sites/site226/web/00826-37286/loggedin.php:2) in /home/sites/site226/web/00826-37286/loggedin.php on line 3"

Code: Select all

<html>
<? 
session_start();
	if(empty($_SESSION&#1111;'username'])) &#123;
		die('An error has ocurred. It may be that you have not logged in, or that your session has expired.
			Please try <a href="http://www.MYSITE.com/00826-37286/home.php?id=9">logging in</a> again or contact the 
			<a href="mailto:Admin@MYSITE.com">system administrator</a>');
	&#125;

extract($HTTP_GET_VARS);  

   if (!$id)  $id = 1;
    
    switch($id) &#123;
    
    case 1:
    include('member/topframe.php');
    include('member/main.php');
    include('member/botframe.php');
    break;

    case 2:
    include('member/topframe.php');
    include('member/cp.php');
    include('member/botframe.php');
    break;
    
    case 3:
    include('member/topframe.php');
    include('member/logout.php');
    include('member/botframe.php');
    break;


    default:
    include('member/topframe.php');
    include('member/main.php');
    include('member/botframe.php');
&#125;
?>
</html>
Help would be very much appreciated!

Thanks

Rob

Posted: Sat Oct 12, 2002 2:14 pm
by hob_goblin
try putting

<? ob_start(); ?>

at the top of every page

and

<? ob_end_flush(); ?>

at the bottom of every page.

Posted: Sat Oct 12, 2002 2:40 pm
by Rob_Beard
nope, didnt work. still stuck on validate.php.

Posted: Sat Oct 12, 2002 2:43 pm
by volka
tracking this kind of error is really easy
Warning: Cannot send session cache limiter - headers already sent (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 36
open /home/sites/site226/web/00826-37286/validate.php and try to find out why line 6 produces output before line 36 (where something http-header related happens) is processed.

and read Sticky: Before Post Read: Warning: Cannot add header information ;)

Posted: Sat Oct 12, 2002 3:02 pm
by Rob_Beard
I've cleaned it up a bit;

Code: Select all

<html>
<?
ob_start();
session_start();
?> 
<?php
//#------------------------------------------------------------------------------
//#*** CHANGE THESE PARAMETERS TO USE YOUR OWN DATABASE AND USERNAME/PASSWORD ***
	$Databasename = '******;
	$DBUsername =	'******';
	$DBPassword = '******';
	$HostName = 'localhost';
//#------------------------------------------------------------------------------
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];
	//connect to the DB and select the database
	$connection = mysql_connect($HostName, $DBUsername, $DBPassword) or die(mysql_error());
	mysql_select_db($Databasename, $connection) or die(mysql_error());
	//set up the query
	$query = "SELECT * FROM Users
			WHERE user_name='$user_name' AND password='$password'";
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
   //if there's exactly one result, the user is validated. 
   // Otherwise, he's invalid 
   if($affected_rows == 1) &#123; 
   $redirect_uri = "http://www.mysite.com/00826-37286/loggedin.php"; 
   //add the user to our session variables 
    $_SESSION&#1111;'username'] = $user_name; 
	echo 'Successful';
   &#125; 
else &#123; 
      echo '<font face="Verdana" color=Red size=2>Password Not Valid<br><br><br><a 
href="http://www.MYSITE.com/00826-37286/home.php?id=9">Click Here To Return To The Login 
Screen</a></font>'; 
   &#125; 
?>
<body>
</body>
</html>
<? ob_end_flush(); ?>
but, I still get errors:
"Warning: Cannot send session cookie - headers already sent by (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 36

Warning: Cannot send session cache limiter - headers already sent (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 36

Warning: Cannot add header information - headers already sent by (output started at /home/sites/site226/web/00826-37286/validate.php:6) in /home/sites/site226/web/00826-37286/validate.php on line 38"

and all that is located on line 6 is: "<?php"....

Can anyone see what i'm doing wrong?

Posted: Sat Oct 12, 2002 3:23 pm
by volka
please do read Sticky: Before Post Read: Warning: Cannot add header information
It's all in there
p.s.: session handling is done with cookies by default and cookies are header-informations.

Posted: Sat Oct 12, 2002 3:35 pm
by Takuma
Read the Sticky before posting!

Code: Select all

<?php
ob_start(); 
session_start();  
?>
<html>
<?php
//#------------------------------------------------------------------------------ 
//#*** CHANGE THESE PARAMETERS TO USE YOUR OWN DATABASE AND USERNAME/PASSWORD *** 
   $Databasename = '******; 
   $DBUsername =   '******'; 
   $DBPassword = '******'; 
   $HostName = 'localhost'; 
//#------------------------------------------------------------------------------ 
   $user_name = $_POST&#1111;'user_name']; 
   $password = $_POST&#1111;'password']; 
   //connect to the DB and select the database 
   $connection = mysql_connect($HostName, $DBUsername, $DBPassword) or die(mysql_error()); 
   mysql_select_db($Databasename, $connection) or die(mysql_error()); 
   //set up the query 
   $query = "SELECT * FROM Users 
         WHERE user_name='$user_name' AND password='$password'"; 
   //run the query and get the number of affected rows 
   $result = mysql_query($query, $connection) or die('error making query'); 
   $affected_rows = mysql_num_rows($result); 
   //if there's exactly one result, the user is validated. 
   // Otherwise, he's invalid 
   if($affected_rows == 1) &#123; 
   $redirect_uri = "http://www.mysite.com/00826-37286/loggedin.php"; 
   //add the user to our session variables 
    $_SESSION&#1111;'username'] = $user_name; 
   echo 'Successful'; 
   &#125; 
else &#123; 
      echo '<font face="Verdana" color=Red size=2>Password Not Valid<br><br><br><a 
href="http://www.MYSITE.com/00826-37286/home.php?id=9">Click Here To Return To The Login 
Screen</a></font>'; 
   &#125; 
?> 
<body> 
</body> 
</html> 
<? ob_end_flush(); ?>

Posted: Sat Oct 12, 2002 3:48 pm
by Coco
yeah as the sticky says, if you have a header("Location:") you cant have ANY output before it

that inclues the <html> tag

Posted: Sat Oct 12, 2002 5:11 pm
by Rob_Beard
sorry :( , didn't see the sticky, i was on 1600x1200 res, i could barly see my own text.

Thanks for your help anyway. :)

Posted: Sun Oct 13, 2002 2:57 am
by Takuma
Change it then or make the text bigger by going to View->Text Size