Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6669301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:08:01+00:00 2026-05-26T03:08:01+00:00

This question has more to do with how I am setting up my server

  • 0

This question has more to do with how I am setting up my server side code for a simple login script. I’m interested in the best way to achieve my goal, which is of course to verify a users username and password against a database and present them with either a successful login, a registration page, or a username or password found, but the alternative is wrong.

Right now, I have it set up where my sql query scans the database for both the user and pass:

SELECT * FROM test WHERE userName='" + userName + "' AND pass='" + password + "'"

Problem with this approach is it either returns a true or false…I cannot tell if one of the inputs was correct and the other wasn’t. It either finds the record, or it doesn’t.

So I could query based on the username alone, and if found check the record for the correct password before passing the user onto a successful login. That way I know if the password is wrong, but I have no idea if the password is right and the user simply types the wrong username.

Alternatively, I could extend on that, and if the user isn’t found, requery the database based on the password and determine if I can find a record but the username doesn’t match. It seems like a lot of back and forth with the database, which is fine. But i’d like to hear from some experts on whether or not this is a proper approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:08:02+00:00Added an answer on May 26, 2026 at 3:08 am

    You don’t want to disclose too many information to people with bad intents trying to probe your system for available usernames (or even – god forbid – passwords that are in use).

    When a login attempt failed, simply display a message stating:

    Username and/or password mismatch.

    As an aside, use prepared statements, rather than string concatenation when working with your database; it protects you from SQL injection attacks.
    Plus – although it’s not entirely clear from your code snippet – don’t store plain passwords or plain password hashes. Rely on one of the many available and well tested encryption/hashing libraries e.g. PHP’s crypt function (make sure you select a proper hashing function such as SHA512).

    Your code in the most simplest form would then look like this:

    // coming from your login page
    $dbh = new PDO(…);
    $sth = $dbh->prepare('SELECT `digest` FROM `users` WHERE `name` = :name LIMIT 1');
    $sth->prepare(array( ':name' => $_POST['username'] ));
    $result = $sth->fetch();
    
    if($result !== FALSE && crypt($_POST['password'], $result['digest']) === $result['digest']) {
      printf('You logged in successfully as %s', htmlspecialchars($_POST['username']));
    } else {
      echo 'Sorry, username and/or password did not match! Please try again.';
      sleep(1);
      exit;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been retitled/retagged so that others may more easily find the solution
This is more of a question about best pattern and practice than asking about
This question has been asked before ( link ) but I have slightly different
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),
This question has spawned out of this one. Working with lists of structs in
This question has been asked in a C++ context but I'm curious about Java.
This question has been puzzling me for a long time now. I come from
This question has in the back of my mind for some time, sorry if
This question has been asked in various forms in a number of different forums,
This question has been bugging me for some time. I always picture launching my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.