Problem with login sessions... empty session variable (weird...)

Thread Solved

Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training

Problem with login sessions... empty session variable (weird...)

 
0
  #1
Oct 29th, 2009
Hi I'm making a login panel which sends data to the same page and I use a class made by me and called 'user' that stored user information and autentification. When I refresh the page I check if any session was created before (it was created at first successfull login containing the email and encoded password).
The problem is when I output the session variables, email contains nothing, password is ok. I don't know why. I rechecked the user object and my script, I cannot find what's going on. Please help.
Here is the code for login:
  1. <?php
  2. session_start();
  3.  
  4. include("connect.php");
  5. include("user_class.php");
  6.  
  7. $user = new user();
  8. /* check session variables if ok - email contains nothing */
  9. echo $_SESSION["email"];
  10. echo " * ".$_SESSION["password"];
  11.  
  12. if(!isset($_SESSION["email"]) && !isset($_SESSION["password"])){//daca sesiunea nu exista
  13. echo"1";
  14.  
  15. if($user->auth($_POST["email"],sha1($_POST["password"])))
  16. {
  17. echo $user->getAuthMsg()."<br>";
  18. $_SESSION["email"]=$user->getEmail();
  19. $_SESSION["password"]=$user->getPassword();
  20. /* check session variables if ok - all OK. This means user object works fine, no? */
  21. echo $_SESSION["email"];
  22. echo " * ".$_SESSION["password"];
  23. }
  24.  
  25. else
  26. {
  27. echo $user->getAuthMsg();
  28. session_destroy();
  29. }
  30. }
  31. else if(isset($_SESSION["email"]) && isset($_SESSION["password"])){//daca sesiunea exista ne autentificam pentru acele date
  32. echo"2";
  33. /************* I could never enter here ******************/
  34. if($user->auth($_SESSION["email"],$_SESSION["password"]))
  35. { echo"2da";
  36. echo $user->getAuthMsg()."<br>";
  37. $_SESSION["email"]=$user->getEmail();
  38. $_SESSION["password"]=$user->getPassword();
  39. }
  40.  
  41. else
  42. {echo"2nu";
  43. echo $user->getAuthMsg();
  44. session_destroy();
  45. }
  46. }
  47. else
  48. {
  49. /************* I always enter here cause $_SESSION["email"] is allways empty :( ******************/
  50. }
  51.  
  52.  
  53. ?>
  54.  
  55.  
  56.  
  57. <?php if(!$user->isAuth){ ?>
  58. <table border="1">
  59. <form name="login_member" id="login_member" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
  60. <tr>
  61. <td>Email:</td><td><input type="text" name="email" id="email" /></td>
  62. </tr>
  63. <tr>
  64. <td>Password:</td><td><input type="password" name="password" id="password" /></td>
  65. </tr>
  66. <td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="Login"></td>
  67. </tr>
  68. </form>
  69. </table>
  70. <?php exit;}else{?>
  71.  
  72. <a href="overall.php">Overall view</a> :: <a href="sections.php">Sections</a> :: <a href="members.php">Members</a> ::
  73. <a href="requests.php">Requests</a> :: <a href="visitors.php">Visitors</a> :: <a href="account.php">Account Settings</a>
  74.  
  75.  
  76. <?php } ?>

Here is user class script:
  1. <?php
  2. class user{
  3. // DATE MEMBRU
  4. var $email;
  5. var $password;
  6. var $ip;
  7. var $banned;
  8. var $allowEditContent;
  9. var $allowViewStatistics;
  10. var $allowAddArticle;
  11. var $allowDELETE;
  12.  
  13. var $authMsg;
  14. var $isAuth=false;
  15.  
  16. var $isAlive=false;
  17. // CONSTRUCTOR
  18. /*
  19. function user($mail,$pass,$ip_address,$banned_ip,$allowEditC,$allowViewS,$allowAddA,$allowDEL){
  20. $this->email=$mail;
  21. $this->password=$pass;
  22. $this->ip=$ip_address;
  23. $this->banned=$banned_ip;
  24. $this->allowEditContent=$allowEditC;
  25. $this->allowViewStatistics=$allowViewS;
  26. $this->allowAddArticle=$allowAddA;
  27. $this->allowDELETE=$allowDEL;
  28. }*/
  29. // functii SET
  30. function user(){
  31. $this->alive=true;
  32.  
  33. $this->email="";
  34. $this->password="";
  35. $this->ip="";
  36. $this->banned=true;
  37. $this->allowEditContent=false;
  38. $this->allowViewStatistics=false;
  39. $this->allowAddArticle=false;
  40. $this->allowDELETE=false;
  41.  
  42. $authMsg="";
  43. }
  44.  
  45. function setEmail($mail){
  46. echo "Setam email:".$this->email=$mail;
  47. }
  48.  
  49. function setPassword($pass){
  50. echo "Setam parola:".$this->password=$pass;
  51. }
  52.  
  53. function setIp($ip_address){
  54. $this->ip=$ip_address;
  55. }
  56.  
  57. function setBanned($banned_ip){
  58. $this->banned=$banned_ip;
  59. }
  60.  
  61. function setAllowEditContent($allowEditC){
  62. $this->allowEditContent=$allowEditC;
  63. }
  64.  
  65. function setAllowViewStatistics($allowViewS){
  66. $this->allowViewStatistics=$allowViewS;
  67. }
  68.  
  69. function setAllowAddArticle($allowAddA){
  70. $this->allowAddArticle=$allowAddA;
  71. }
  72.  
  73. function setAllowDELETE($allowDEL){
  74. $this->allowDELETE=$allowDEL;
  75. }
  76.  
  77. //functii GET
  78. function getEmail(){
  79. return $this->email=$mail;
  80. }
  81.  
  82. function getPassword(){
  83. return $this->password;
  84. }
  85.  
  86. function getIp(){
  87. return $this->ip;
  88. }
  89.  
  90. function getBanned(){
  91. return $this->banned;
  92. }
  93.  
  94. function getAllowEditContent(){
  95. return $this->allowEditContent;
  96. }
  97.  
  98. function getAllowViewStatistics(){
  99. return $this->allowViewStatistics;
  100. }
  101.  
  102. function getAllowAddArticle(){
  103. return $this->allowAddArticle;
  104. }
  105.  
  106. function getAllowDELETE(){
  107. return $this->allowDELETE;
  108. }
  109.  
  110.  
  111. //functii speciale
  112. function isAlive()
  113. { return $this->alive; }
  114.  
  115. function getAuthMsg(){
  116. return $this->authMsg;
  117. }
  118.  
  119. function auth($mail,$pass){
  120. // authentification with mysql database
  121.  
  122. $this->setEmail($mail);
  123. $this->setPassword($pass);
  124.  
  125. $mail=trim(htmlspecialchars($mail));
  126. $pass=trim(htmlspecialchars($pass));
  127. if($mail!="" && $pass!="")
  128. {
  129.  
  130. $q="SELECT * FROM members WHERE email='".$mail."' AND password='".$pass."'";
  131. $res=mysql_query($q);
  132. if(!$res)
  133. {
  134. $this->authMsg="Eroare trimitere date:".mysql_error();
  135. return false;
  136. }
  137. else
  138. { $numrows=mysql_num_rows($res);
  139. if($numrows==1)
  140. {
  141. $this->authMsg="Autentificat!";
  142. $this->isAuth=true;
  143. return isAuth;
  144. }
  145. else
  146. {
  147. $this->authMsg="Nume sau parola gresite!";
  148. $this->isAuth=false;
  149. return $this->isAuth;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. $this->authMsg="Va rugam completati corespunzator formularul!";
  156. $this->isAuth=false;
  157. return $this->isAuth;
  158. }
  159. }
  160.  
  161. };
  162. ?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #2
Oct 29th, 2009
I FOUND IT!
  1. function getEmail(){
  2. return $this->email=$mail;//incorrect. must be "return $this->email;"
  3. }
Sorry I am very tired, I couldn't see it for hours. Anyone can do a mistake like that when is very tired . You just CAN'T see it. Thanks all for reading. Thread solved.
Last edited by Clawsy; Oct 29th, 2009 at 2:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: darius_#2 is an unknown quantity at this point 
Solved Threads: 0
darius_#2 darius_#2 is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
sall
Reply With Quote Quick reply to this message  
Reply

Tags
login, object, php, session, user

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC