Results 1 to 5 of 5

Thread: What is Wrong with This Script?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    What is Wrong with This Script?

    PHP Code:
    <?
    $hostname="<HIDDEN>";
    $username="damasterjo";
    $password="<HIDDEN>";
    $database="damasterjo";

    mysql_connect($hostname,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM Customers";
    $result=mysql_query($query);
    $num=mysql_numrows($result);

    //Checks If The Username and Password Exists
    //Declaring Variables
    $usernameisvalid=0;
    $passwordisvalid=0;
    $passwordexists=0;
    $usernameexists=0;
    //loop through all the entrys in the database to see if any match...
        while ($usernameisvalid < $num) {
            //set the username they want to a variable
            $checkuser=$_POST['Username'];
            //Set the database user name into a variable
            $existing=mysql_result($result,$usernameisvalid,"Username");
            //Check whether or not they are the same

                if  ($checkuser == $existing)
                {
                    $usernameisvalid=$num;
                    $usernameexists=1;
                }
                    $usernameisvalid++;
                }

        while ($passwordisvalid < $num) {
            //Set Password They Want Into A Variable
            $checkpassword=$_POST['Password'];
            //Set the database password into a variable
            $existingpass=mysql_result($result,$passwordisvalid,"Password");
            //Check whether or not they are the same
            
                if ($checkpassword == $existingpass)
                {
                    $passwordisvalid=$num;
                    $passwordexists=1;
                }
                    $passwordisvalid++;
                }
        
        if ($passwordexists == 0 and $usernameexists == 0)
        {
            echo "This Username or Password is Invalid!";
        }
        if ($usernameexists == 1 and $passwordexists == 1)
        {
            echo "This Username and Password Exists!";
        }
    ?>
    For some reason, no matter what Username or Password I enter, it keeps saying that it is valid.

    Btw, just to let everyone know, this is for a joined project between me and Damasterjo.



  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: What is Wrong with This Script?

    Here is your problem..

    PHP Code:
    $num=mysql_numrows($result); 


    //Should be

    $num=mysql_num_rows($result); 

  3. #3
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: What is Wrong with This Script?

    why dont you just do this all in an SQL query
    PHP Code:
    $query "SELECT username, password from Users WHERE username = '" $_POST['username'] . "' AND password = '" $_POST['password'] ."' LIMIT 1"
    Note: You should not store the actual password in your database, instead store the hash of it. Use the md5() function for this.


    Lintz mysql_num_rows and mysql_numrows will both work, for the moment, as they havent removed the later from PHP. If the problem was with the function beig removed then it would have thrown an error saying that the function could not be found.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: What is Wrong with This Script?

    Here is the gist of my login function. It's inside a user class which explains the use of the this keyword.

    $username is the posted username. $passwordHash is the md5 hash of the password posted.

    PHP Code:
    // find the user
    $users mysql_query(
      
    'SELECT DISTINCT * FROM `'.USERS_TABLE.'` WHERE `username` = \''.$username.'\''
    );

    if (
    is_resource($users) && (bool)mysql_num_rows($users)) {
      
    $user mysql_fetch_assoc($users);
      
      
    // check password
      
    if ($user['password_md5'] == $passwordHash) {
        
    $this->LoggedIn true;
        
        
    $this->userID $user['id'];
        
    $this->username $username;
        
    $this->userGroups $user['user_groups'];
        
        
    $_SESSION['user_object'] = $this;
      }
      else {
        return 
    AUTH_WRONG_PASSWORD;
      }
    }
    else {
      return 
    AUTH_NO_USER;

    AUTH_WRONG_PASSWORD and AUTH_NO_USER are just constants.

  5. #5
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: What is Wrong with This Script?

    Ok well i fixed it for him, all that was wrong he was using the wrong post variable, its caps sensitive, so I fixed it...
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width