USER AUTHENTICATION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

USER AUTHENTICATION

Post by rooky »

Everah | Code tags are a requirement, not an option. Please use them when posting code in the forums. Also, please do not use all caps when posting. It looks like you are mad at us.


HI AGAIN EVERYBODY,
I HAVE THIS CODE TO AUTHENTICATE SOME USERS AND I'M HAVING PROBLEMS BECAUSE IS TELLING ME THAT THE PASSWORD IS WRONG AND I ALREADY CHECK WITH THE DATABASE AND IS CORRECT. I ALSO WOULD LIKE TO BE REDIRECTED TO ANOTHER PAGE AS SOON AS THE USER IS AUTHENTICATED. CAN SOMEONE PLEASE HELP ME WITH THIS ONE TOO?

Code: Select all

<?PHP
// Configura los datos de tu cuenta
$dbhost='host';
$dbusername='username';
$dbuserpass='password';
$dbname='database';
 
session_start();
 
// Conect to database
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
 
if ($_POST['username']) {
  //Check if username and password was sent
  $username=$_POST['username'];
  $password=$_POST['password'];
  if ($password==NULL) {
    echo "Password was not sent";
  }else{
    $query = mysql_query("SELECT user_email,user_pwd FROM users WHERE user_email = '$username'") or die(mysql_error());
    $data = mysql_fetch_array($query);
    if($data['password'] != $password) {
      echo "Login incorrect";
    }else{
      $query = mysql_query("SELECT user_email,user_pwd FROM users WHERE user_email = '$username'") or die(mysql_error());
      $row = mysql_fetch_array($query);
      $_SESSION["s_username"] = $row['username'];
      echo "You have been log correctly ".$_SESSION['s_username']." and now you can access index.php.";
    }
  }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
<table border="0"> 
<tr><td colspan=2><h1>Login</h1></td></tr> 
<tr><td>Username:</td><td> 
<input type="text" name="username" maxlength="40"> 
</td></tr> 
<tr><td>Password:</td><td> 
<input name="password" type="password" id="password" maxlength="50"> 
</td></tr> 
<tr><td colspan="2" align="right"> 
<input type="submit" name="submit" value="Login"> 
</td></tr> 
</table> 
</form>
Last edited by RobertGonzalez on Mon Aug 04, 2008 1:30 pm, edited 1 time in total.
Reason: For the love of God can you please use [php] tags around your code?
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: USER AUTHENTICATION

Post by dajawu »

The problem is this line:

Code: Select all

if($data['password'] != $password) {
You labeled your password field in your database as user_pwd but you call it up using password which is wrong. Change that line to this:

Code: Select all

if($data['user_pwd'] != $password) {
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: USER AUTHENTICATION

Post by rooky »

Hi Dajawu,
Thanks a lot for your help, is working pretty well now.
Bye!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: USER AUTHENTICATION

Post by RobertGonzalez »

Just an aside to what you are doing...

Calling an array var like

Code: Select all

<?php
if ($_POST['somefield']) {
?>
Will throw errors if that array index is not set. You might not be seeing the errors because of your error display / handling settings, but they are there. You should always check isset() or empty() rather than working on an array member that may or may not be there.
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: USER AUTHENTICATION

Post by rooky »

Hi Everah,
Sorry for the Caps, I will stop using them. Please show me how to use the Code Tags in this forum. I'm a new user.
Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: USER AUTHENTICATION

Post by RobertGonzalez »

Wrap the code you post inside of either [ code ] (without the spaces) tags (you can use the button above the posting text box) or you can use [ code=php ] or even [ php ] (again, without spaces).
Post Reply