943,929 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3155
  • PHP RSS
Oct 29th, 2009
0

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

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 29th, 2009
0
Re: Problem with login sessions... empty session variable (weird...)
I FOUND IT!
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Nov 17th, 2009
0
Re: Problem with login sessions... empty session variable (weird...)
sall
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darius_#2 is offline Offline
1 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: starting a website in PHP and My SQL
Next Thread in PHP Forum Timeline: Automatic Email to selected persons for alerts





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


Follow us on Twitter


© 2011 DaniWeb® LLC