User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 373,375 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,813 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1031 | Replies: 55
Reply
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 165
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Help Can't create sessions

  #1  
Feb 26th, 2008
admin.php
  1. <?php
  2. // connection to MySQL server
  3. mysql_connect('localhost','root','');
  4. mysql_select_db('administration');
  5.  
  6.  
  7. if (isset($_POST['username'])) {
  8. $loginUsername=$_POST['username'];
  9. $loginPassword=$_POST['password'];
  10. $MM_redirectLoginSuccess = "validated.php";
  11. $MM_redirectLoginFailed = "admin.php";
  12. $MM_redirecttoReferrer = true;
  13.  
  14. $errors = array();
  15.  
  16. if(empty($_POST['username'])) {
  17. $errors[] = 'You think whom going to fill up the USERNAME for you?';
  18. }
  19. if(empty($_POST['password'])) {
  20. $errors[] = 'You think whom going to fill up the PASSWORD for you?';
  21. }
  22. if (empty($errors)) {
  23.  
  24. $loginUsername = get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername);
  25. $password = get_magic_quotes_gpc() ? $password : addslashes($password);
  26. $LoginRS_query = "SELECT username, password FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  27. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  28. $loginFoundUser = mysql_num_rows($LoginRS);
  29.  
  30. if ($loginFoundUser) {
  31.  
  32. session_name ('YourVisitID');
  33. session_start();
  34. $_SESSION['user_id'] = $loginFoundUser[0];
  35. $_SESSION['first_name'] = $loginFoundUser[1];
  36. $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
  37.  
  38. echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
  39. exit();
  40. }
  41. else {
  42.  
  43. echo "<script type='text/javascript'>location.href='$MM_redirectLoginFailed';</script>";
  44. }
  45. }
  46. mysql_close();
  47. }
  48.  
  49. ?>

validated.php
  1. <?php
  2.  
  3. session_name ('YourVisitID');
  4. session_start(); // Start the session.
  5. $MM_redirectLoginFailed = "admin.php";
  6. $MM_redirecttoReferrer = true;
  7.  
  8. // If no session value is present, redirect the user.
  9. if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) {
  10.  
  11. echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
  12. exit(); // Quit the script.
  13. }
  14.  
  15. ?>

I've two pages as displayed above. I wanted to enable sessions so that the user can not go to validated.php straight without any validation. But the the code above does not create any sessions at all. The codes in validated.php is working fine and the codes in admin.php is working fine without the line 32 - 36. This line did not create/store any sessions when executed. Each time, after I log in using the correct username and password, it will redirect me bak to the index.php. Please help.
"I might not be the BEST but I'm not like the REST!"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Can't create sessions

  #2  
Feb 26th, 2008
in validated.php page, print out the session variables. print_r($_SESSION);
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 165
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Re: Can't create sessions

  #3  
Feb 26th, 2008
Where should I place this code, before/after session start? thx
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Can't create sessions

  #4  
Feb 26th, 2008
After session_start();
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 165
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Re: Can't create sessions

  #5  
Feb 26th, 2008
When I paste the code as directed, the page validated.php was blinking, similar to an infinity loop. Like the page try to load, but can't. Finally, I stopped that page. Advise pls.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Can't create sessions

  #6  
Feb 26th, 2008
Have 2 pages.
  1. <?php //page1.php
  2. session_start();
  3. $_SESSION['name']="test";
  4. header("location: page2.php");
  5. ?>
In page2.php, write this.
  1. <?php
  2. session_start();
  3. print_r($_SESSION);
  4. ?>
See if it prints test in page2.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 165
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Re: Can't create sessions

  #7  
Feb 26th, 2008
I get output like this:

Array ( [name] => test )

Then.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Can't create sessions

  #8  
Feb 26th, 2008
So, session exists.

Redirect to a dummy page, for eg, page2.php and have print_r($_SESSION) in it. Check if session is being passed !
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 165
Reputation: lordx78 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Re: Can't create sessions

  #9  
Feb 26th, 2008
I get output like this:

Array ( )

It can't create the the session rite? But why. All my syntax was correct rite?
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Can't create sessions

  #10  
Feb 26th, 2008
Strange! Are you sure, $loginFoundUser[0], $loginFoundUser[1], md5($_SERVER['HTTP_USER_AGENT']) have values ? I think nothing is assigned to session variables.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC