Validates Username and Passowrd with PHP

Hi Guys, Here I m Writing the code, How to Validates user name and password with the database…

If the username and Password matches with the database values we need to assign a session to that user..

for that here we are giving a simple session to a user.



$server = “localhost”; //In case if u have some other write it here
$username = “Your Server Login Username”;
$password = “Your Server Login Password”;
$db_name = “Your Database Name”;

$db = mysql_connect($server, $username, $password) or die(“Connection to database failed, perhaps the service is down !!”);
mysql_select_db($db_name) or die(“Database name not available !!”);

Those Query Will Connect You with your Database…. Now I m Writing the Query..

$login = mysql_query(“select * from table_name where (username = ‘” . $_POST[‘username’] . “‘) and (password = ‘” . md5($_POST[‘password’]) . “‘)”,$db);

Tip: here my table contains only Two Field user name and password, if ur table contains more that two fields you can also use *. There is no such issue of using (*) Specifically. (*) will select all the values from the database, where as I can also write

$login = mysql_query(“SELECT username, password FROM table_name WHERE (username = ‘” . $_POST[‘username’] . “‘) and (password = ‘” . md5($_POST[‘password’]) . “‘)”,$db);

Note: Here first username you are watching is the name of the column in your database and the value $_POST[‘username’] is the value which we are getting from the form in the previous page. Similarly for password also.

Note2: md5($_POST[‘password’]) is the Password which is stored in the database in the form encrypted value, so that no one can see what the password is!!

$rowcount = mysql_num_rows($login);
if ($rowcount == 1) {
$_SESSION[‘username’] = $_POST[‘username’];
header(“Location: welcomehome.php”);
header(“Location: login.php”);

Note: here we are giving username and password to a variable called $rowcount.

Next if the user name and password matches with the database we are assigning user a session. and If user name and password doesn’t matches with the database we are redirecting the user back to the login page, else we will redirect the user to its home page.


15 thoughts on “Validates Username and Passowrd with PHP

  1. Hasmukh Rathod March 26, 2008 / 10:28 AM

    Hi Puneet Nice Article, It Helps me a Lot, Please Continue to Publish an Article Like this.

    Thanks Once Again for your Great Help

  2. paulraj July 28, 2008 / 7:26 PM

    Hi puneet. Thanks a lot. Ur code works neat. After the validation I want a message window opened which shows “username already exists” or “succesfully created account”. After that I want the control remains in the same page. Could u plz help me. Thanks, Paul

  3. dblackshell August 7, 2008 / 4:38 PM

    ‘) or 1=1#

    ^ sql injection to bypass the login script

    try to use mysql_real_escape_string() to sanitize the input…

  4. nsbaskar January 29, 2009 / 1:51 PM

    which is very useful those who are the beginneers in webdesignig

  5. mani April 22, 2009 / 12:23 AM

    hai puneet please continue an article like this thanks once again

  6. shaheda August 31, 2009 / 5:36 AM

    im shaheda
    a php trainee,
    would u please help me for writing a php code to validate username and password without using php sessions.

    with regards,

  7. Puneet Pandey August 31, 2009 / 5:46 AM

    Hi Shaheda,

    First of all I am shifted to ruby on rails.. I am no more working on PHP.. so I can’t help you with sessions and all.. I am creating a base for myself to get my grip back on PHP. bt I think it will take time.. Meanwhile there are many developers and their well-maintained blogs, I am sure they can guide you well..

    For any references like if you are stuck somewhere I can’t able to give you the exact code bt yaa I will surely help you with the links available.

    Let me know if that sounds fine for you..

    Puneet Pandey

    • shaheda September 1, 2009 / 2:41 AM

      Hi Puneet,

      Thanks for the reply.
      i’ll be greatful to u, if u send me a link of any developer who can guide me well on PHP.


  8. shruthi April 18, 2011 / 5:07 AM

    hi !! am currently learning and working on PHP in an org. could u pls help me in learning it better ???? i need the links for PHP basics and database connections . pls help

    • Puneet Pandey April 18, 2011 / 5:56 PM

      Hi Shruthi,

      I am not working with PHP anymore 😦 but there are lots of forums and blogs you can subscribe, where you can ask your doubts, see what people are talking about and do many things..

      Here are few lists which might help you:

      There are numerous books as well, which you can subscribe to learn PHP. Let me know, if you need couple of more references.

      Thanks for contacting

      • Anitha July 20, 2012 / 1:49 PM

        Hi Puneet
        in the same page of the html design itself we want to give the php code for validation ah plz tell me ……..for login page design

      • Puneet Pandey August 23, 2012 / 9:37 AM

        Hi Anitha,

        Unfortunately, I am not able to help you in that, because I’ve worked upon PHP a long back and not in touch now. There are other folks who can probably help you out in this. Or you can also post your question to


    • d kalyan kumar May 8, 2013 / 2:56 PM

      Hi Shruthi

      Iam also fresher for PHP but i have some exp in action script 3.0
      i will try to send some links when i am doing pratise

      Kalyan Kumar

  9. sumit October 15, 2012 / 12:59 PM

    hello,can you please me suggest anything to validate gmail account username and password in my site without redirecting to google account page.

  10. ruby programming books March 1, 2014 / 8:11 PM

    Hi there, I get pleasure from coding with Ruby. Havent been using it very long but discovered heaps from sites around
    the web. Thatt said, your website has helped me a whole
    lot, also, I say thanks a lot for the crystal clear explanations and really easy to
    follow content on your site.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s