944,199 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3568
  • PHP RSS
Jun 16th, 2006
0

Session Login Problem

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <?PHP
  2. session_start();
  3. mysql_connect("localhost", "*******", "*******")or die(mysql_error());
  4. mysql_select_db("eluxian");
  5.  
  6. $email = $_POST['email'];
  7. $pass1 = $_POST['password'];
  8.  
  9. $pass = md5($pass1);
  10.  
  11. $check = mysql_query("SELECT password FROM members WHERE email='$email'")
  12. or die("Email not found in database");
  13. $fetch = mysql_fetch_array($check) or die(mysql_error());
  14.  
  15. $password = $fetch['password'];
  16.  
  17. if ($password != $pass){
  18. echo "You entered a bad password";
  19. }else{
  20. $_SESSION['email'] = $email;
  21. echo "<meta http-equiv='Refresh' content='0;url=news.php'>";
  22. echo "Correct password. Forwarding now...";
  23. }
  24.  
  25. ?>
Last edited by Ries; Jun 16th, 2006 at 6:32 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ries is offline Offline
20 posts
since Apr 2006
Jun 17th, 2006
0

Re: Session Login Problem

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jun 17th, 2006
0

Re: Session Login Problem

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jun 18th, 2006
0

Re: Session Login Problem

Dude you forgot to register the session, follow this code

$_SESSION['email'] = $email
session_register("email");
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP "hello" in different languages ???
Next Thread in PHP Forum Timeline: Warning: session_start():





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC