Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,176 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,176 people online right now. Registration is fast and FREE... Join Now!




Sayin incorrect password when it's the right one

 
Reply to this topicStart new topic

Sayin incorrect password when it's the right one, Login page

Decypher
29 Aug, 2008 - 04:21 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

Ok basically I've got it working kinda, It recognises the username and I've checked this by typing something random in and it tells me that the user does not exist...but when i put the correct password in, it tells me its wrong eventhough I know it's right

CODE
$username = $_COOKIE['Username'];
$pass = $_COOKIE['Password'];
$check = mysql_query("SELECT * FROM user WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: members.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM user WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');


I believe the problem is in there somewhere.

Thanks for any help in advance
User is offlineProfile CardPM
+Quote Post

jonesa01
RE: Sayin Incorrect Password When It's The Right One
29 Aug, 2008 - 04:57 AM
Post #2

New D.I.C Head
Group Icon

Joined: 18 Feb, 2007
Posts: 39


Dream Kudos: 25
My Contributions
I spotted the problem instantly, but I have re-written it to how I think it should be:

CODE

$username = $_COOKIE["Username"];
$pass = $_COOKIE["Password"];
// Connect to the database
mysql_connect("server","username","password") or die("Error: ".mysql_error());
// Select database table
mysql_select_db("database");
// Check if user exists
// Suggestion: Don't go through this if there is no username/password
if($username != "" && $password != ""){
             $check = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'") or die("Error: ".mysql_error());
             While ($info = mysql_fetch_array($check)){
                   if($pass != $info["password"]){
                            // Invalid Password
                   }else{
                         header("Location: members.php");
                   }
             }
}else{

      // If form is submitted
      if (isset($_POST["submit"])){
         if ($_POST["username"] == "" || $_POST["pass"] == ""){
          die("You did not fill in a required field.');
          // Suggestion: Just for clarity put the rest of the page in the else statement
         }else{
               if(!get_magic_quotes_gbc()){
                                           $_POST["email"] = addslashes($_POST["email"]);
               }
               // Suggestion: Don't put post in mysql feed it into a variable
               $user = $_POST["username"];
               $check = mysql_query("SELECT * FROM `user` WHERE `username` = '$user'") or die("Error: ".mysql_error());
               $check2 = mysql_num_rows($check);
               if($check2 == "0"){
                    die("That user does not exist in our database. <a href=\"add.php\">Click Here to Register</a>");
               }else{
                     // Suggestion: Put loop in else statement
                     While ($info = mysql_fetch_array($check)){
                           // Suggestion: Cut straight to the chase, just md5
                           $_POST["pass"] = MD5($_POST["Pass"]);
                           if($_POST["pass"] != $info["password"]){
                                       die("Incorrect password, please try again");
                           }else{
                                      // Valid login
                           }
                     }
               }
         }
      }
}


I hope I have helped. If you need anything else I am happy to help!

This post has been edited by jonesa01: 29 Aug, 2008 - 04:59 AM
User is offlineProfile CardPM
+Quote Post

Decypher
RE: Sayin Incorrect Password When It's The Right One
29 Aug, 2008 - 05:06 AM
Post #3

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

ty it recognises the password but i gives me a syntax error...going to have a look first but will come back if i can't find a solution...

Cheers jonesa
User is offlineProfile CardPM
+Quote Post

Decypher
RE: Sayin Incorrect Password When It's The Right One
29 Aug, 2008 - 05:12 AM
Post #4

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

hmmm I get this:

Fatal error: Call to undefined function get_magic_quotes_gbc() in C:\Program Files\Abyss Web Server\htdocs\MafiaGame\login.php on line 29

I'm guessin it has something to do with my php config right?
User is offlineProfile CardPM
+Quote Post

jonesa01
RE: Sayin Incorrect Password When It's The Right One
29 Aug, 2008 - 05:15 AM
Post #5

New D.I.C Head
Group Icon

Joined: 18 Feb, 2007
Posts: 39


Dream Kudos: 25
My Contributions
Well, it is not your php config, it is:

get_magic_quotes_gpc()

not

get_magic_quotes_gbc()

This post has been edited by jonesa01: 29 Aug, 2008 - 05:15 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:38AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month