php Newbie having problem with $_SESSION

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
Bike Racer
Forum Newbie
Posts: 3
Joined: Mon Apr 21, 2008 10:31 am

php Newbie having problem with $_SESSION

Post 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??
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php Newbie having problem with $_SESSION

Post by aceconcepts »

Code: Select all

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

Code: Select all

<?PHP
Bike Racer
Forum Newbie
Posts: 3
Joined: Mon Apr 21, 2008 10:31 am

Re: php Newbie having problem with $_SESSION

Post by Bike Racer »

Anything else I could try?
lbfb
Forum Newbie
Posts: 2
Joined: Mon Apr 21, 2008 11:53 pm

Re: php Newbie having problem with $_SESSION

Post 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
Bike Racer
Forum Newbie
Posts: 3
Joined: Mon Apr 21, 2008 10:31 am

Re: php Newbie having problem with $_SESSION

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