Page 1 of 1

USER AUTHENTICATION

Posted: Sun Aug 03, 2008 11:06 pm
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>

Re: USER AUTHENTICATION

Posted: Sun Aug 03, 2008 11:43 pm
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) {

Re: USER AUTHENTICATION

Posted: Mon Aug 04, 2008 1:09 pm
by rooky
Hi Dajawu,
Thanks a lot for your help, is working pretty well now.
Bye!

Re: USER AUTHENTICATION

Posted: Mon Aug 04, 2008 1:32 pm
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.

Re: USER AUTHENTICATION

Posted: Mon Aug 04, 2008 1:38 pm
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!

Re: USER AUTHENTICATION

Posted: Mon Aug 04, 2008 1:52 pm
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).