A blog about PHP, Ajax, JavaScript, CSS and Web 2.0

Ajax login validation system in PHP using jQuery

Last time, I’ve showed you how to check user availability in Ajax using jQuery’s fading effect. But I’ve just shown the example without connecting the database and some people have faced problem with database connecting solution of that problem.In this post, I’ll show you how to use Ajax login system in php using jQuery and some animation as well.

In this example, first of all we’ll validate the user login detail from ajax showing the messages with some animation. If authenticated, the user will be redirected to the secure page “secure.php”. If you’ll try to directly access “secure.php”, you can’t do that.

As you can see above this code is preety much similar to the previous post of checking username availability in ajax and php and you can see the explanation of the above code from that post. But in above code, where the user is validated, he’ll be logged into the “secure.php” using “document.location” in JavaScript.

Well, as you can see above javaScript’s code, when focus is moved away from the password it also call the for sumit action. Basically whenever you hit tab button in password field, it starts validating the user detail using ajax.

As you can see above, the user login detial is validated from the database. If the user login detail doesn’t exists, it just returns the “no” values and if the user login detial does exists the it just return “yes” values with setting username in session variable.

As you can see above the code of “secure.php” is simpleforward. If the user is not authenticated by session then he’ll be redirected to “index.html”. And there is link for “logout” in this page form where user can destroy the seession.

156 thoughts on “Ajax login validation system in PHP using jQuery”

Comment navigation

If you need a complete login script with remember me and admin feautures try visting:http://evolt.org/PHP-Login-System-with-Admin-Features
Though I recomend you are rather good at php and php classes. But its greatly made and I put this script together with the script from the link and it works like a breeze.
I also higly recommend to read and understand this.
Regards

Kasper

July 8, 2009

Once again hi,
Regarding javascript disabled this can actually be solved verry verry easy in making a php switch one case with php validation and error messaging for javascript disabled and one without.

And for making the form work just type in the action of the file pointing to the php validation without java and then have javascript remove it. This way when javascript is disabled the form still submits and gives error messages.

I’ve loaded all the downloaded files as they were into my webserver.
Since my login system does not involve a mysql call (it gets done through API), I’ve removed all the code in the ajax_login.php file, except for testing purposes I kept the echo yes line.

I assumed ANY values entered in the login form would be acceptable that way, however the script still fails to accept the login.
Any ideas?

hi
the code is working fine on my local wamp server! but it is not running in my webserver , when i uploaded in my website…it says ur login details sucks…i just changed my database settings, page layout..everthing is fine and working in my local server but not in webhosting’s server…plz help!! and can u provide me with code where i can enter series of user id and password…to allow multiplle user login???

Thanks Roshan! You’ve got it. I want to show a more “clear code” to any user that right-click on my page and choose “View Source Code” and this should do it. I just didn’t understand why is that code not inside a .js file!

dmid

September 5, 2009

Hi, I love this example, and have got it running on my mac under Mamp.
Just one question though:
How would I modify it so that each user that logs in is taken to a different secure page?

This is great. But as I understand AJAX is for changing sections of a page without having to resubmit the whole page. So instead of redirecting to secure.php, it could stay in the same index.html but on the top right cornor you could display the login information.. kind of like the information in Amazon. When you are redirecting the page I believe the whole point of AJAX is lost IMHO

Andrei S

October 7, 2009

Hi,

Great tutorial. Except there is a little bug. After I fill the boxes with something and hit submit, i receive the normal error message, but if I focus on password filed than on user name filed, the submit button triggers automatically. Hope you can fix that

Great job.

huihui

October 19, 2009

I found your articles is absolutely useful to me. But I wonder how to apply it into codeIgniter code.
It always stop at checking message box there but without post the username value to user_availability.php page. Any idea of that? Would you mind to show me how to do it? Will be appreciate if someone can help me with that. Thank you very very much.

Shaka

October 30, 2009

Can this login code be altered to use a text file to store the username/password instead of a database

edi

November 10, 2009

Hello Roshan,
Thx a lot for this code which would be very helpfull. However, I am unable to run your download code properly. The validation always fails and says that login detail sucks even when I try with the good login and password. Do you know how I can fix it? Thx for your help.

Gagan Shrestha

November 11, 2009

hello roshan,
your code looks great and simple,
i tried but some problem occured that i am unable to figure out, hope to get help from u
My probem is that whe i post the user name and password to ajax_login.php every thing woks fine and it also echoes “yes” but the data variable in the statement if(data==”yes”) in the javascript seems to be blank. when l console.log(“data:”+data) the value of the data in firebug i see that
data: is printed before “yes”
seems that my callback function is executed before the value passed is taken by the function….
some help would be appreciated..

Frank

November 16, 2009

Nice coding but I found a bug so u can try login thousands times with not your own username.
If you fill in an username and a password you can try to hack the account. If you only fill the username you can try hundreds of passwords. If you click on the username the errorbox refreshes everytime! Not a good login for evil hackers!

Yeah, so the only way I can get it to work is if I change line 27 of ‘ajax_login.php’ to:

if(strcmp($row[‘password’],$_POST[‘password’])==0)

Is this somehow risking the security of the site?

Lionel

January 23, 2010

Great Script!

Any help to add the Remember Me checkbox will be welcomed 😀

I tried to add it, but I am unable to even view if the checkbox is checked or not the ajax_login.php.

It is something stupid but I can’t find why I can’t see if it exist or not….

Any help will we be welcomed.

Thanks all and thanks for this great script

sHWETA

February 24, 2010

gr8 tutorial.thanx……………….

shweta

February 24, 2010

values r not posting on page.there is some problem with input field nmaes.can u plz sort it out

Nishant

February 25, 2010

Ur example is superb.. n i m using it in my application.. but for security issue.. i m facing a problem..
i can see the username n password i send in firebug of firefox…. is there any way throw which i can hide it.. or encrypt it.?

Thanks
Nishant

Lars

March 23, 2010

I shamelessly stolen this and included it in my application, saved me surely atleast one hour of work. Thanks man!

Hi. Me again, you can just delete all my comments, i found the problem in some of my PHP code, so had NOTHING to do with your script.

Thanks anyways for a nice script!

DenW

April 3, 2010

Hi,

I found your script searching for a login method using AJAX and tried it.
Everything worked perfectly! Thanks for a great script..

Yesterday however i decided to change jquery.js to an updated version (jquery-1.4.2-min.js).
After uploading the new jquery, the login script isn’t working anymore. It simply does nothing (as far as i can tell) after i leave the password field.

OK I must be doing something wrong. The only thing I’ve done differently with your code is to put the form inside a div. When the user goes to an index page and clicks on a link, i load with form with a jquery command. Form loads fine. However, it seems as though the doc ready jquery stuff is not loading at all in the div – I even put an alert in right after the document.ready command and the alert does not display. If i continue to fill out the form and press submit, i’m just returned to my index page.