473,497 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

User Login Script with Session

12 New Member
Hi

I am using the following code to get users to enter their username and password, it is then checked and a new page is displayed with their username. My question is, how can I display the rest of their details from that session, I have googled and read many articles, but I am still not any futher forward, any help would be much appreciated.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $errorMessage = '';
  4. if (isset($_POST['txtUsername']) && isset($_POST['txtPassword'])) {
  5. include 'includes/config.php';
  6. include 'includes/opendb.php';
  7.  
  8. $username = $_POST['txtUsername']; 
  9. $password = md5($_POST['txtPassword']);
  10.  
  11. // check if the user id and password combination exist in database
  12. $sql = sprintf("SELECT username 
  13. FROM phpbb_users
  14. WHERE username = '%s' 
  15. AND user_password = '%s'",$username,$password);
  16.  
  17. $result = mysql_query($sql) 
  18. or die('Query failed. ' . mysql_error()); 
  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. $_SESSION['username'] = $username;
  24. // after login we move to the main page
  25. header('Location:main.php');
  26. exit;
  27. } else {
  28. $errorMessage = 'Sorry, wrong user id / password';
  29. }
  30.  
  31. include 'includes/closedb.php';
  32. }
  33. ?>
  34.  
  35. <html>
  36. <head>
  37. <title>Basic Login</title>
  38. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  39. </head> 
  40. <body>
  41. <?php
  42. include 'includes/header.php';
  43. include 'includes/navbar.tpl';
  44. if ($errorMessage != '')
  45. ?>
  46. <p> <strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
  47.  
  48. <form method="post" name="frmLogin" id="frmLogin">
  49. <table width="400" border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#000000">
  50. <tr>
  51. <td width="150">User Name</td>
  52. <td><input name="txtUsername" type="text" id="txtUsername"></td>
  53. </tr>
  54. <tr>
  55. <td width="150">Password</td>
  56. <td><input name="txtPassword" type="password" id="txtPassword"></td>
  57. </tr>
  58. <tr>
  59. <td width="150">&nbsp;</td>
  60. <td>
  61. <input type="submit" name="btnLogin" value="Login">
  62. </div></td></tr>
  63. </table>
  64.  
  65. </form>
  66. </body>
  67. </html>
  68.  
Make use of the available formatting tags in the forum Use CODE tags around your code as appropriate:
  • [code]..code goes here..[ /code]
  • [code=php] ..php code goes here.. [ /code]
  • [code=c] ..C like code goes here.. [ /code]
  • [code=html] ..html code goes here.. [ /html]
Jun 30 '07 #1
9 3545
ditch
12 New Member
Here is what is coded for the main page
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. include 'includes/header.php'; 
  4. include 'includes/navbarout.tpl';
  5.  
  6. // is the one accessing this page logged in or not?
  7. if (!isset($_SESSION['db_is_logged_in']) 
  8. || $_SESSION['db_is_logged_in'] !== true) {
  9.  
  10. // not logged in, move to login page
  11. header('Location: login.php');
  12. exit;
  13. }
  14.  
  15. echo sprintf("Welcome to the Revenge's members area, you are logged in as %s",$_SESSION['username']);
  16.  
  17. ?>
  18.  
Jun 30 '07 #2
ak1dnar
1,584 Recognized Expert Top Contributor
Thread Title changed : Ajaxrand
From >>> Help for a n00b, whilst using sessions
To >>> User Login Script with Session
Jun 30 '07 #3
ak1dnar
1,584 Recognized Expert Top Contributor
You have created two session Variables so far,Here.
Expand|Select|Wrap|Line Numbers
  1. // set the session
  2. $_SESSION['db_is_logged_in'] = true;
  3. $_SESSION['username'] = $username;
  4.  
You Can use this $_SESSION['username'] to get the other records for the logged user.

Other_page.php
Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. $Logged_in_user = $_SESSION['username'];
  3.  
Now the the $Logged_in_user is here in this variable.
re-use it for any data manipulation,If this user name is a Qnique One


Thanks !
Jun 30 '07 #4
ditch
12 New Member
Thank you for the reply, I understand, what you are saying, however, how can extract the data from multiple tables with that one variable
Jun 30 '07 #5
kovik
1,044 Recognized Expert Top Contributor
Thank you for the reply, I understand, what you are saying, however, how can extract the data from multiple tables with that one variable
Depends on how the tables are related. Typically, all tables dealing with a user are somehow related by the user id.

Expand|Select|Wrap|Line Numbers
  1. SELECT `users`.`username`, `profiles`.`dateJoined` FROM `users` LEFT JOIN (`profiles`) ON (`profiles`.`userid` = `users`.`id`) WHERE `users`.`username` = $foo;
Jun 30 '07 #6
ditch
12 New Member
Understand now, however the query I used looks like the :

[PHP]$query = "SELECT phpbb_users.username, phpbb_users.user_from, phpbb_users.user_email, phpbb_ranks.rank_image
FROM phpbb_users
LEFT JOIN (phpbb_ranks)
ON (phpbb_users.user_rank = phpbb_ranks.rank_id)
WHERE phpbb_users.username = $Logged_in_user";[/PHP]

When I run it, this error appears, which I believe means that it's not connecting to the DB:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

If I change the variable $Logged_in_user to phpbb_users, everybodies details appear, so any ideas whats wrong with the query?
Jul 1 '07 #7
ditch
12 New Member
HOOAH, done it thank you for all your help, I understand now, I chnaged the query to

[PHP]$query = sprintf("SELECT phpbb_users.username, phpbb_users.user_from, phpbb_users.user_email, phpbb_ranks.rank_image
FROM phpbb_users
LEFT JOIN (phpbb_ranks) ON (phpbb_users.user_rank = phpbb_ranks.rank_id)
WHERE phpbb_users.username = '%s'",$Logged_in_user);[/PHP]
Jul 1 '07 #8
ak1dnar
1,584 Recognized Expert Top Contributor
ditch,

Since you have asked unrelated Question within the same thread, regarding PHP based calender I have to Split the thread.
Please Read Why
New Thraed Location:

Thanks ,
.
Jul 2 '07 #9
ak1dnar
1,584 Recognized Expert Top Contributor
Copied Contents from Splitted thread -Ajaxrand

By the way, the only problem with the first query was that you forgot the single quotes around the value.
Jul 2 '07 #10

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

Similar topics

7
17256
by: fr? | last post by:
Hi, i have a website , on wich users have to log in credentials are checked against mysql db some session vars are set during login for use somewhere else in the code. Is there a way to...
3
2833
by: koolyio | last post by:
Hey, could you please tell me what is wrong with my login script. I just started learning php. CODE: login.php <? session_start(); header("Cache-Control: private"); ?>
18
2443
by: | last post by:
Please help. After a number of wrong turns and experiments I need advice on login management system to secure our web pages without inconveniencing our visitors or our internal staff. What I...
3
3548
by: Tom | last post by:
Hi I have a web application using asp.net and c#. User has to login to the application with his username and pwd. However, I do not allow other user uses the same username and pwd to login, i.e....
2
2022
by: Tom | last post by:
I hope someone can help me figure out what's going on here. I've re-read the section on sessions at php.net and Googled this high and low but I haven't found anything that quite explains my...
2
299
by: tshad | last post by:
I created a User Control that just does some VB code and doesn't have any HTML in it. Mainly it is an initialization piece that I want to put on all my pages. It just has this code: ...
1
1191
by: nasirmajor | last post by:
dear all, a simple quetion from still a bigginer in asp.net i have a simple webpage with username and password which is linked with database. now when user gives his username and password and if...
2
1826
by: underground | last post by:
Hi, everyone I've been trying to figure out a way for a user to update there information. I'm using sections to identify the specific user..Here is the form <? include("include/session.php");...
3
6207
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
9
6254
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
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...
1
6878
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...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
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...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
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...

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.