Session_start() doesn't work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nataly333
Forum Newbie
Posts: 17
Joined: Thu Oct 23, 2003 4:08 pm

Session_start() doesn't work

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
nataly333
Forum Newbie
Posts: 17
Joined: Thu Oct 23, 2003 4:08 pm

Post 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 :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
nataly333
Forum Newbie
Posts: 17
Joined: Thu Oct 23, 2003 4:08 pm

Post 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?
Post Reply