Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session_start not working??

Status
Not open for further replies.

martinb7

Programmer
Joined
Jan 5, 2003
Messages
235
Location
GB
Hi, i have this login script and when i try and login in from login.php it comes up with this error:

Code:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/hph/public_html/b/mjb3000/forum/login2.php:9) in /home/hph/public_html/b/mjb3000/forum/login2.php on line 18

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/hph/public_html/b/mjb3000/forum/login2.php:9) in /home/hph/public_html/b/mjb3000/forum/login2.php on line 18


here is my code:

<?php

$DB = mysql_connect(&quot;host&quot;, &quot;user&quot;, &quot;pass&quot;);
mysql_select_db(&quot;db&quot;, $DB);

$query = mysql_query(&quot;SELECT * FROM forumMembers WHERE username = '&quot;.$_POST['username'].&quot;' and password = '&quot;.$_POST['password'].&quot;'&quot;);

$result = mysql_fetch_row($query);

if($result){
session_start();
$_SESSION['username'] = $_POST['username'];

echo(&quot;<a href=index2.php>Return</a>&quot;);
}
else{
echo(&quot;Your username or password didn't match any records - Try again<br><br>
LOGIN!<br>
<form method=post action=login2.php>
<input type=text name=username><br>
<input type=password name=password><br>
<input type=submit value=Login!>
</form>&quot;);
}

?>

Any ideas?

Thanx

Martin

 
Code:
session_start();
should be the very first line of your script in order to avoid this type of issue.

//Daniel
 
could you rewrite the code then so i see what you mean??

Thanx

Martin
 
You have no explicit output lines in that code, so the invocation of session_start() should have worked where you put it. You likely have something appearing in your script before your &quot;<?php&quot; line. Maybe a blank line or something.

However, danielhozac has it right -- as a general rule, session_start() is best invoked at the beginning of a script. If the browser has sent a session token, invoking session_start() at the beginning of the file can't hurt anything. If the browser has not sent a session token, PHP will need to send a cookie to set one.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top