Jump to content

PHP while lookp taking too long to execute


phgstudy

Recommended Posts

Hello!

I'm working on a login form, but the while loop takes longer than 120 seconds to run, and then an expiry error is thrown.

Here's the code:-

 

$res=mysqli_query($con,"select username,password from users");
  
         $row=mysqli_fetch_array($res);
         while($row)
         {
                if($username==$row["username"] && $password==$row["password"])
                {
                       echo "LOGIN SUCCESSFUL!";
                }
                else
                {
                       echo "LOGIN Failed!";
                } 
Link to comment
Share on other sites

There really isn't much more to say than that. The query is retrieving every single user in the database. That's slow. If you used a WHERE to only retrieve the user with the matching username then it would happen in less than a second (probably).

 

Well, one more thing. Are you hashing passwords? It doesn't look like it. You need to.

  • Like 1
Link to comment
Share on other sites

your loop is also looping forever (until the php time limit is reached), since the condition being tested by the loop is a fixed value.

 

you don't even need a loop for what you are doing.

 

your sql query should be trying to match one row, based on the username. if the username/one row is found, you would fetch that one row, then verify if the hashed version of the entered password matches the hashed password from when the user registered. see php's password_hash() and password_verify() functions for this.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.