•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
Reputation:
Rep Power: 1
Solved Threads: 2
php Syntax (Toggle Plain Text)
$_SESSION['user_id'] = $loginFoundUser[0]; $_SESSION['first_name'] = $loginFoundUser[1]; $_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
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!"
•
•
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
Reputation:
Rep Power: 1
Solved Threads: 2
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation:
Rep Power: 7
Solved Threads: 205
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.
php Syntax (Toggle Plain Text)
$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. $LoginRS = mysql_query($LoginRS_query) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); $record_row = mysql_fetch_array($LoginRS); $_SESSION['user_id'] = $record_row[0]; $_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*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation:
Rep Power: 7
Solved Threads: 205
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*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
Reputation:
Rep Power: 1
Solved Threads: 2
php Syntax (Toggle Plain Text)
$LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'"; $LoginRS = mysql_query($LoginRS_query) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); $record_row = mysql_fetch_array($LoginRS); if ($loginFoundUser) { session_name ('YourVisitID'); session_start(); $_SESSION['user_id'] = $record_row[0]; $_SESSION['first_name'] = $record_row[1];
Just wanted to confirm with. Is this the way you want me to do?
or
php Syntax (Toggle Plain Text)
$LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'"; $LoginRS = mysql_query($LoginRS_query) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); $record_row = mysql_fetch_array($LoginRS); if ($record_row>0) { session_name ('YourVisitID'); session_start(); $_SESSION['user_id'] = $record_row[0]; $_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
php Syntax (Toggle Plain Text)
<?php session_name ('YourVisitID'); session_start(); // Start the session. //print_r($_SESSION); $MM_redirectLoginFailed = "admin.php"; $MM_redirecttoReferrer = true; // If no session value is present, redirect the user. if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) { echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>"; exit(); // Quit the script. } ?>
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!"
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation:
Rep Power: 7
Solved Threads: 205
php Syntax (Toggle Plain Text)
$LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'"; $LoginRS = mysql_query($LoginRS_query) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); $record_row = mysql_fetch_array($LoginRS); if ($loginFoundUser) { session_name ('YourVisitID'); session_start(); $_SESSION['user_id'] = $record_row[0]; $_SESSION['first_name'] = $record_row[1];
So, instead of
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) { , you can check, php Syntax (Toggle Plain Text)
if(! isset($_SESSION['user_id']) || ! isset($_SESSION['first_name'])) { // sesssion not set, redirect to error page }
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*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
Reputation:
Rep Power: 1
Solved Threads: 2
•
•
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
Reputation:
Rep Power: 1
Solved Threads: 2
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation:
Rep Power: 7
Solved Threads: 205
Do one thing. Before assigning it to the session, print out its value. ie.,
See what it prints ! Then assign $user_id and $first_name to the session.
php Syntax (Toggle Plain Text)
$LoginRS_query = "SELECT user_id, first_name FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'"; $LoginRS = mysql_query($LoginRS_query) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); $record_row = mysql_fetch_array($LoginRS); $user_id = $record_row['user_id']; $first_name = $record_row['first_name']; print "User id is :". $user_id; print "First name is :". $first_name;
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*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2007
Location: Penang Island, Malaysia
Posts: 160
Reputation:
Rep Power: 1
Solved Threads: 2
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?
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!"
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Similar Threads
- Email addrress in Sessions (PHP)
- Identifing all Active sessions in PHP (PHP)
- previous sessions not visible burning multisession DVDs (Storage)
- Do all programs in Windows XP compactible in Linux? (Getting Started and Choosing a Distro)
- Sharing object instances between applications (VB.NET)
- How do I create own Shout box/or input and display form? (PHP)
- Trying to create a login system (PHP)
- login script using sessions (PHP)
- Create Windows Authentication (VB.NET)
Other Threads in the PHP Forum
- Previous Thread: contains
- Next Thread: OPTION problem



Linear Mode