473,547 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

capturing username

ddtpmyra
333 Contributor
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. ?>
Nov 24 '08 #1
5 1490
Markus
6,050 Recognized Expert Expert
Store the username in a session, and then on any page that requires it, start the session and grab it from the array.
Nov 24 '08 #2
ddtpmyra
333 Contributor
Markus,

can you give me example how to do it?

thanks,
DM
Nov 24 '08 #3
Markus
6,050 Recognized Expert Expert
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.  
Nov 24 '08 #4
ddtpmyra
333 Contributor
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.  
Nov 26 '08 #5
Markus
6,050 Recognized Expert Expert
Line 24 needs to reflect the actual username.

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

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1627
by: ashutosh.bhardwaj1980 | last post by:
Hi, I amdeveloping a web site in ASP and it is a secure site. I would like someone to help me on this: -->i dont want 2 users to accesss my site with the same username parallely. --> Also how can I capture window.close event in my application and then call a asp script in that function.
33
5577
by: Joerg Schuster | last post by:
Hello, Python regular expressions must not have more than 100 capturing groups. The source code responsible for this reads as follows: # XXX: <fl> get rid of this limitation! if p.pattern.groups > 100: raise AssertionError( "sorry, but this version only supports 100 named groups"
14
2521
by: Brent Burkart | last post by:
I am trying to capture the Windows Authenticated username, but I want to be able to capture the login name that exists in IIS, not Windows. In order to enter my company's intranet through the internet, they have to login. I want to be able to capture that login versus their Windows login because I need to know who they are from any computer...
8
3943
by: Raj Thakkar | last post by:
Hi, I am currenty working on a site for intranet. I have a user control in the header of every page that will be displayed only if people with certain username are surfing the site. These lists of usernames is stored on the server side in a xml based file So what I am doing at the moment is inside the user control pageload method, i get...
17
4301
by: Lauren Wilson | last post by:
Does anyone know if it is possible to capture FTP responses to various FTP commands when managing an FTP session from a VBA procedure? For example, if we try to login to an FTP server and the login info is incorrect, FTP replies with a message to that effect. I need to capture those kinds of responses and advise the user of an Access app...
5
10214
by: sachu | last post by:
Hi Everyone, I want develop the intranet site with seamless authentication. Is there any way to capture only the user name who has logged in the windows machine. Iam using PHP 5.0 MySQL and IIS Server. I have tried with $_SERVER but its returning nothing. Please help me out.
7
1709
xxoulmate
by: xxoulmate | last post by:
what script do i use to determine the username currently logged on the computer., tnx is there any way to do it using php or javascript.,
2
1433
xxoulmate
by: xxoulmate | last post by:
how can i captured the username logged on the computer., and print it in the php page. tnx
3
2208
by: mohanprasadgutta | last post by:
Hello, I want to capture the text being printed on console. problem description is as follows. I am calling called.pl program from caller.pl program using backticks. in called.pl program i am giving an external command to be executed in backticks. that external application will through a message to the console if any arguements are missing...
0
7437
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7703
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
748
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.