954,184 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Session Login Problem

This is my code im using for my login, but when it re-directs to the next page the $_SESSION['email'] array doesnt contain any data, what is wrong with my coding that causes this(btw to check it had no data I echoed it and nothing came onto my screen)

<?PHP
session_start();
mysql_connect("localhost", "*******", "*******")or die(mysql_error());
mysql_select_db("eluxian");

$email = $_POST['email'];
$pass1 = $_POST['password'];

$pass = md5($pass1);

$check = mysql_query("SELECT password FROM members WHERE email='$email'")
or die("Email not found in database");
$fetch = mysql_fetch_array($check) or die(mysql_error());

$password = $fetch['password'];

if ($password != $pass){
echo "You entered a bad password";
}else{
$_SESSION['email'] = $email;
echo "<meta http-equiv='Refresh' content='0;url=news.php'>";
echo "Correct password. Forwarding now...";
}

?>
Ries
Newbie Poster
20 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

Where are you echoing the session variable?
You should try echoing it right at the top.

Maybe you're overwriting with $_SESSION['email'] = $email;
Where $email will be empty on the second load of the page.

Try something like...


[php]
if ($_SESSION['email']) {
// session is set, check if valid

} else {
// session not set, ask for login

}
[/php]

Something to note:
sessions are available on the second load, after you set it.
(thats the reason we have it, continuity in a stateless protocol, http)
they dont always work, if cookies are not accepted by the browser, and your php ini only allows 'safe' sessions, where it isn't passed in the url, sessions will fail.

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

ps: ref.
http://us2.php.net/manual/en/ref.session.php

You'll notice theres alot of config in php.ini for sessions.
It may not be your code, maybe the config.

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

Dude you forgot to register the session, follow this code

$_SESSION['email'] = $email
session_register("email");

cancer10
Posting Whiz in Training
234 posts since Dec 2004
Reputation Points: 58
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You