473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

PHP login script

12
For some reason its not pulling the id and username from my database and putting the values into the correlated sessions. Can anyone help?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $err = array();
  4.     // Will hold our errors
  5.  
  6.  
  7.     if(!$_POST['username'] || !$_POST['password'])
  8.         $err[] = 'All the fields must be filled in!';
  9.  
  10.     if(!count($err))
  11.     {
  12.         $username = $_POST['username'];
  13.         $password = $_POST['password'];
  14.         //connect to the database here
  15.         $username = mysql_real_escape_string($username);
  16.         $query = "SELECT password, salt
  17.                 FROM users
  18.                 WHERE username = '$username';";
  19.         $result = mysql_query($query);
  20.         if(mysql_num_rows($result) < 1) //no such user exists
  21.         {
  22.             $err[]='Wrong username and/or password!';
  23.         } else {
  24.             $userData = mysql_fetch_array($result, MYSQL_ASSOC);
  25.             $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );        
  26.         }
  27.  
  28.         if($hash != $userData['password']) //incorrect password
  29.         {
  30.             $err[]='Wrong username and/or password!';
  31.         } else {
  32.  
  33.             $row = mysql_fetch_assoc(mysql_query("SELECT id,username FROM users WHERE username = '$username';"));
  34.             if($row['username']) {
  35.                 $_SESSION['usr']= $row['username'];
  36.                 $_SESSION['id'] = $row['id'];
  37.                 $_SESSION['rememberMe'] = $_POST['rememberMe'];
  38.                 setcookie('fiRemember',$_POST['rememberMe']);
  39.                 header("Location: main.php");
  40.                 exit;
  41.             } else {
  42.                 $_SESSION['usr']= "NOT WORKING";
  43.                 header("Location: main.php");
  44.                 exit;                
  45.             }
  46.         }
  47.     }
  48.             if($err)
  49.             $_SESSION['msg']['login-err'] = implode('<br />',$err);
  50.             // Save the error messages in the session
  51.  
  52.             header("Location: index.php");
  53.             exit;
  54.  
  55. ?>
  56.  
May 28 '11 #1
1 1864
johny10151981
1,059 1GB
you did not connect with mysql database in this page.
May 29 '11 #2

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

Similar topics

8
by: Steve Fitzgerald | last post by:
The below login script does work. The form does not seem to be submitting. I keep getting the username and password fields. The only errors I get are notices that email and password and undefined...
3
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"); ?>
2
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
by: dubdave | last post by:
Hi I wrote a very basic login script which acesses a mySQL database, like this:- <?php // compares values entered in login page form with mySQL database, and then directs either to...
2
by: drah | last post by:
Hello not exactly sure if it is a php script problem or a database/table issue i apologize if i'mposting in wrong category. I was asked to put a login script on a site for someone and i made the...
8
tolkienarda
by: tolkienarda | last post by:
hi all i have a login script that is simplified with out any extra stuff. and it doesn't seem to work. i think the problem is something to do with session variables. but i am not sure what it is....
3
by: bull1099 | last post by:
I designed a simple site which has a login for users to access their account page. When i had my files uploaded on a terrible hosting service site, my website I designed was fully functional. I moved...
12
by: lamez | last post by:
I have a login script that use to work, but for some reason it does not work now! You guys can test it here: http://lamezz.com/_NEW/login.php USER: test PASS: test it uses a script that...
1
by: sadanandsuvarna | last post by:
Hi, I hv created following script for rediffmail login, i've saved login name and password and has saved it with .html extension so that when i click on the file it wud directly take me to inbox...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.