Connecting Tech Pros Worldwide Forums | Help | Site Map

capturing username

ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#1: Nov 24 '08
Hi I have a log-in script below that do the log-in validation, my question is how can I capture the username to for my next page reference so I can execute update command according to current log-in name?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. $errorMessage = '';
  5. if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
  6.     include 'library/config.php';
  7.     include 'connect.php';
  8.  
  9.     $userId   = $_POST['txtUserId'];
  10.     $password = $_POST['txtPassword'];
  11.  
  12.     // check if the user id and password combination exist in database
  13.     $sql = "SELECT * FROM members WHERE username='$userId' and password='$password' and user_level=1";
  14.  
  15.  
  16.  
  17.     $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
  18.  
  19.     if (mysql_num_rows($result) == 1) {
  20.         // the user id and password match,
  21.         // set the session
  22.         $_SESSION['db_is_logged_in'] = true;
  23.  
  24.         // after login we move to the main page
  25.         session_register("txtUserId");
  26.         session_register("txtPassword");
  27.         header('Location: approvals_r.php');
  28.         exit;
  29.     } else {
  30.         $errorMessage = 'Sorry, wrong user id / password';
  31.     }
  32.  
  33.     include 'library/closedb.php';
  34. }
  35. ?>

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#2: Nov 24 '08

re: capturing username


Store the username in a session, and then on any page that requires it, start the session and grab it from the array.
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#3: Nov 24 '08

re: capturing username


Markus,

can you give me example how to do it?

thanks,
DM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#4: Nov 24 '08

re: capturing username


Ok, on the page where the user logs in, if the log is successful, do this:

Expand|Select|Wrap|Line Numbers
  1. $_SESSION['username'] = $username; // change this to reflect the database value
  2.  
Then on a page that needs to 'capture' the user name, do this:

Expand|Select|Wrap|Line Numbers
  1. // at very beggining of page:
  2. session_start();
  3.  
  4. // code
  5.  
  6. // get the username
  7. echo $_SESSION['username'];
  8.  
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#5: Nov 26 '08

re: capturing username


Here's my code but didn't work what am I missing here?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // we must never forget to start the session
  3. session_start();
  4.  
  5. $errorMessage = '';
  6. if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
  7.     include 'library/config.php';
  8.     include 'connect.php';
  9.  
  10.     $userId   = $_POST['username'];
  11.     $password = $_POST['txtPassword'];
  12.  
  13.     // check if the user id and password combination exist in database
  14.     $sql = "SELECT * FROM members WHERE username='$username' and password='$txtPassword' and user_level=1";
  15.  
  16.  
  17.  
  18.     $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
  19.  
  20.         if (mysql_num_rows($result) == 1) {
  21.         // the user id and password match,
  22.         // set the session
  23.         $_SESSION['db_is_logged_in'] = true;
  24.         $_SESSION['username'] = $username; // change this to reflect the database value 
  25.  
  26.         // after login  move to the target page
  27.         session_register("username");
  28.         session_register("txtPassword");
  29.         header('Location: approvals_r.php');
  30.         exit;
  31.     } else {
  32.         $errorMessage = 'Sorry, wrong user id / password';        
  33.     }
  34.  
  35.  
  36. }
  37. ?>
and for the target page after log-in

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // start the session
  3. session_start();
  4. echo $_SESSION['username']; 
  5. // is the one accessing this page logged in or not?
  6. if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
  7.     // not logged in, move to login page
  8.     header('Location: login_r.php');
  9.     echo $_SESSION['username']; 
  10.  
  11.  
  12. //more codes here....
  13. ?>
  14.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#6: Nov 26 '08

re: capturing username


Line 24 needs to reflect the actual username.

Expand|Select|Wrap|Line Numbers
  1.         $_SESSION['username'] = $userId;
  2.  
Also remove lines 27 & 28.
Reply