Page 1 of 1

Session_start() doesn't work

Posted: Sat Nov 08, 2003 4:48 pm
by nataly333
When i used session_auto_start=1 flag in my php.ini, my site worked OK.
Now i moved it to host (session_auto_start=0, and ,of course, i can not change it). I added session_start() function in my login.php, but sessions do not start.
Any suggestions?
Thanks

Posted: Sat Nov 08, 2003 5:22 pm
by McGruff
Are you getting any error messages?

If anything gets output to the browser before the session_start() call you'll get some "headers already sent" errors.

Posted: Sat Nov 08, 2003 5:52 pm
by nataly333
No errors, no messages, just session variables, registered in login.php, are invisible in the another .php files(members pages).

If i understand correctly, start_session() must be first function in the site? Or first php function in the file?
Sorry, i'm really newbie in php.
Thank you very much for your help :)

Posted: Sat Nov 08, 2003 8:27 pm
by McGruff
session_start() doesn't actually have to be the first call you make - but it must come before any browser output.

Another thing to check would be any session_unset() calls although I suspect that's not the problem.

Sorry I can't really think why it would have stopped working.

Posted: Sat Nov 08, 2003 8:59 pm
by volka
and remember: you have to use session_start() for each request/page you want to use sessions with. Not only at the first page.

Posted: Sun Nov 09, 2003 3:46 am
by nataly333
Thanks ! :)
It was very important : use session_start() for each page(i used it only at the first :( )
But now i have a new problem : only second "Submit" of the Login form starts the session.
This is my code(short version).

Code: Select all

<?
session_start();
// MySQL connection 
?>

<html>
<head><title>Login</title></head>

<body>
<?
if(!isset($usernameU) | !isset($passwordU)) {
?>

<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<table><tr><td>
<table> 
<tr><td Password :</td>
<td><input type=password name=passwordU></td></tr>
<input type=submit value=Submit></td></tr>
</form> 
</td></tr></table>
</td></tr></table>
</body>
</html>
<?
exit();
}


session_register("usernameU");
session_register("passwordU"); 

$sql = mysql_query("SELECT password FROM login WHERE username = '$usernameU'");
$fetch_em = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);

if($numrows != "0" & $passwordU == $fetch_em["password"]) {
$valid_user = 1;
}
else {
$valid_user = 0;
}

if ($valid_user)
{//return to main page
}


if (!($valid_user))
{
session_unset(); // Unset session variables.
session_destroy(); 

?>

<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<table><tr><td>
<table > 
<tr><td>
Incorrect login information, please try again.
</td></tr>

<tr><td>User Name :</td><td>
<input type=text name=usernameU></td></tr> 
<tr><td>Password :</td>
<td><input type=password name=passwordU></td></tr>

<tr><td>
<input type=submit value=Submit></td></tr>
</form> 
</td></tr></table>
</td></tr></table>
</body>
</html>
<?
exit();
}

?>
[mod_edit:

Code: Select all

tag added][/size]

What did i do wrong?