Page 1 of 1

php Newbie having problem with $_SESSION

Posted: Mon Apr 21, 2008 10:44 am
by Bike Racer
Trying to put together a login screen-
<?php
require_once("Connections/testserver.php");

session_start();

//Catch Field Data
$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password) {
/////////////////////////////////////////////
$query = sprintf("SELECT * FROM night where user_name='$userid' and user_password = '$password'");
$result = mysql_query($query);
$rowAccount = mysql_fetch_array($result);
/////////////////////////////////////////////
}
if ($rowAccount) {

$_SESSION['id'] = $rowAccount['user_id'];

header("location:success.php");
exit;


}elseif($submitted){

echo "You don't exist on our database";
}

?>


I echoed $_SESSION['id} on this page and got a result.

on my welcome page - success.php - I get kicked back to my login page.

<?php
require_once("Connections/testserver.php");

session_start;

if (!isset($_SESSION['id'])){

header("location: mylogin.php");
exit;

}

$id = $_SESSION['id'];

////////////////////////////
$query =sprintf("SELECT * FROM night where user_id='$id'");
$result =mysql_query($query);
$rowAccount =mysql_fetch_array($result);
/////////////////////////////
?>

I echoed $_SESSION['id'] on this page and got a blank.

Am I missing something??

Re: php Newbie having problem with $_SESSION

Posted: Mon Apr 21, 2008 10:49 am
by aceconcepts

Code: Select all

session_start();
should always be declared first, immediately after

Code: Select all

<?PHP

Re: php Newbie having problem with $_SESSION

Posted: Mon Apr 21, 2008 11:29 am
by Bike Racer
Anything else I could try?

Re: php Newbie having problem with $_SESSION

Posted: Tue Apr 22, 2008 12:06 am
by lbfb
Bike Racer

I am new to php myself, so don't be annoyed if I am completely off track here. But looking at the code you posted, I wonder is this exactly what you used on your pages?
If so, then in the second page you are missing the brackets () after session_start.

Also, in the first page where you set $_SESSION['id'] = $rowAccount['user_id']. I am guessing a bit here, but I would have thought that $rowAccount identifier would have to be the field name from your table. And that would be the same name as that you used when selecting from the table contents two or three lines earlier. And in your case they don't match. However, like I said, I am not so sure of myself here as I am new to PHP and mySql also. And if you got a result when echoing your session variable, I am probably wrong about it.

But I know I tried your code, and with the brackets added on to session_start in the second page it worked ok for me.

cheers - hope this helps

Re: php Newbie having problem with $_SESSION

Posted: Tue Apr 22, 2008 5:59 am
by Bike Racer
Thanks for the reply - I was trying different ideas and left the () out by mistake. With them it still doesn't work for me. Thanks for trying the code - at least I know it's not something that I had done wrong. I'm having my web host people looking into the problem.