login session not working

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
danieln
Forum Newbie
Posts: 4
Joined: Sat Apr 12, 2008 4:05 am

login session not working

Post by danieln »

hi,

i have this header.php file included at the top of every of my page.

when i enter my username & password from the login page, it works well and directs me to the mail.php page. but when i click on o move to the next page, i was then kicked out from the logged in session as if i was not logged on.

here is my code:

<?php
include_once('connect.php');
session_start();
$uid = isset($_POST['user']) ? $_POST['user'] : $_SESSION['user'];
$pwd = isset($_POST['pass']) ? $_POST['pass'] : $_SESSION['pass'];

echo "display : " . $uid . " " . $pwd ;

if(!isset($uid)) {
?>
Please login
<?php
exit();
}

$_SESSION['username'] = $uid;
$_SESSION['password'] = $pwd;

$query = "SELECT * FROM webture_staff " .
"WHERE username = '$uid' AND password = '$pwd' ";
$result = mysql_query($query)
or die(mysql_error());
if (!$result) {
unset($_SESSION['username']);
unset($_SESSION['password']);
echo "please login 2 " ;
}
?>

please advice where did it went wrong?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: login session not working

Post by aceconcepts »

Alwaus keep session_start() at the top of the page:

Code: Select all

 
<?PHP
session_start();
 
danieln
Forum Newbie
Posts: 4
Joined: Sat Apr 12, 2008 4:05 am

Re: login session not working

Post by danieln »

thanks!

i made the change and i noticed something very wrong.

i just need to press inter on the login page, and regarless of what is entered, i am able to view the main.php page and the problem is still there--> i can't view other pages :(
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: login session not working

Post by aceconcepts »

It looks like you need to revise your logic.
JustPHP
Forum Newbie
Posts: 2
Joined: Thu Apr 24, 2008 3:22 pm

Re: login session not working

Post by JustPHP »

check $_SESSION['user'] and you are setting $_SESSION['username']. You are using 2 different session variables. same for keys pass and password.
Post Reply