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 361,626 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 2,207 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: 978 | Replies: 55
Reply
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
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

  #11  
Feb 26th, 2008
  1. $_SESSION['user_id'] = $loginFoundUser[0];
  2. $_SESSION['first_name'] = $loginFoundUser[1];
  3. $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);

I took this code from a php book and modified it. The content of my table in my database is,

user_id
first_name
last_name
email
contact
username
password

So I thought, since user_id in the first row and the first_name in the second row, I define it [0] and [1]. I've actually no idea why the book used $_SESSION['agent']. Kindly, please advise me on how to modify the codes to make the session works.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
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

  #12  
Feb 26th, 2008
Just to say that, I wont be available for the next 7-8 hours. I will back for your reply. Please don't ignore this thread till it marks unsolved. This is my kind request. Thanks a lot.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 205
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: Can't create sessions

  #13  
Feb 26th, 2008
Ah! i see. Thats wrong. You aren't fetching any record from the table. You are trying to assign null value to the session, because, there will be nothing in $loginFoundUser[0] and $loginFoundUser[1]. Use this instead of your query.
  1. $LoginRS_query = "SELECT userid, firstname, username, password FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'"; // Here, userid and firstname are the columns of the table. Change it to whatever columnnames you have in your table.
  2. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  3. $loginFoundUser = mysql_num_rows($LoginRS);
  4. $record_row = mysql_fetch_array($LoginRS);
  5. $_SESSION['user_id'] = $record_row[0];
  6. $_SESSION['first_name'] = $record_row[1];
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: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 205
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: Can't create sessions

  #14  
Feb 26th, 2008
And, you don't need $_SERVER['HTTP_USER_AGENT'], since its just used to get the browser information.
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: 160
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

  #15  
Feb 26th, 2008
  1. $LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  2. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  3. $loginFoundUser = mysql_num_rows($LoginRS);
  4. $record_row = mysql_fetch_array($LoginRS);
  5.  
  6. if ($loginFoundUser) {
  7.  
  8. session_name ('YourVisitID');
  9. session_start();
  10. $_SESSION['user_id'] = $record_row[0];
  11. $_SESSION['first_name'] = $record_row[1];

Just wanted to confirm with. Is this the way you want me to do?
or

  1. $LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  2. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  3. $loginFoundUser = mysql_num_rows($LoginRS);
  4. $record_row = mysql_fetch_array($LoginRS);
  5.  
  6. if ($record_row>0) {
  7.  
  8. session_name ('YourVisitID');
  9. session_start();
  10. $_SESSION['user_id'] = $record_row[0];
  11. $_SESSION['first_name'] = $record_row[1];


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

So what kind of session validation should I place here. Please advise.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 205
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: Can't create sessions

  #16  
Feb 26th, 2008
  1. $LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  2. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  3. $loginFoundUser = mysql_num_rows($LoginRS);
  4. $record_row = mysql_fetch_array($LoginRS);
  5.  
  6. if ($loginFoundUser) {
  7.  
  8. session_name ('YourVisitID');
  9. session_start();
  10. $_SESSION['user_id'] = $record_row[0];
  11. $_SESSION['first_name'] = $record_row[1];
This is correct. And yes, in validated.php, you just can check if the session variables is set.
So, instead of if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) { , you can check,
  1. if(! isset($_SESSION['user_id']) || ! isset($_SESSION['first_name'])) { // sesssion not set, redirect to error page
  2. }
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: 160
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

  #17  
Feb 26th, 2008
I've tried the both methods above with the dummy page to print out the sessions, not working.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
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

  #18  
Feb 26th, 2008
Still get an empty array.
"I might not be the BEST but I'm not like the REST!"
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 205
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: Can't create sessions

  #19  
Feb 26th, 2008
Do one thing. Before assigning it to the session, print out its value. ie.,
  1. $LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  2. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  3. $loginFoundUser = mysql_num_rows($LoginRS);
  4. $record_row = mysql_fetch_array($LoginRS);
  5. $user_id = $record_row['user_id'];
  6. $first_name = $record_row['first_name'];
  7. print "User id is :". $user_id;
  8. print "First name is :". $first_name;
See what it prints ! Then assign $user_id and $first_name to the 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: 160
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

  #20  
Feb 26th, 2008
When i pasted the code above, I've temporarily disabled the sessions loop. After I've inputted the correct username and password, at the top left of admin.php I get a msg like this,

User id is :c13964First name is :Lord

and this matches my id and first name in the table. But why can't the session work?
Last edited by lordx78 : Feb 26th, 2008 at 11:18 pm.
"I might not be the BEST but I'm not like the REST!"
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 6:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC