PHP + Ajax without refreshing new logon and logout

Source: Internet
Author: User
Provides a variety of official and user-released code examples. For code reference, you are welcome to exchange and learn this article uses Ajax to skip New logon and exit, thus improving the user experience.

Refreshing LogonDemo: http://www.sucaihuo.com/js/54.html $ (". Btn"). live ('click', function (){
Var user = $ ("# user"). val ();
Var pass = $ ("# pass"). val ();
If (user = ""){
$ ('

'User.html ("user name cannot be blank! "). AppendTo ('. sub'). fadeOut (2000 );
$ ("# User"). focus ();
Return false;
}
If (pass = ""){
$ ('

'0000.html ("the password cannot be blank! "). AppendTo ('. sub'). fadeOut (2000 );
$ ("# Pass"). focus ();
Return false;
}
$. Ajax ({
Type: "POST ",
Url: "ajax. php? Action = login ",
DataType: "json ",
Data: {"user": user, "pass": pass },
BeforeSend: function (){
$ ('

'). AddClass ("loading" Login .html ("logging on..." Login .css ("color", "#999 ")
. AppendTo ('. sub ');
},
Success: function (json ){
If (json. success = 1 ){
$ ("# Login_form"). remove ();
Var p ="

"+ Json. user +"Congratulations!


You have logged on to this site for "+ json. login_counts +.


The last time I log on to this site was: "+ json. login_time +"


[Exit]

";
$ ("# Login"). append (p );
} Else {
$ ("# Msg"). remove ();
$ ('

'0000.html(json.msg0000.css ("color", "#999"). appendTo ('. sub ')
. FadeOut (2000 );
Return false;
}
}
});
});
Program code: Session_start ();
Require_once ('connect. php ');

$ Action = $ _ GET ['action'];
If ($ action = 'login') {// log on
$ User = stripslashes (trim ($ _ POST ['user']);
$ Pass = stripslashes (trim ($ _ POST ['pass']);
If (empty ($ user )){
Echo 'user name cannot be blank ';
Exit;
}
If (empty ($ pass )){
Echo 'password cannot be blank ';
Exit;
}
$ Md5pass = md5 ($ pass); // The password is encrypted using md5.
$ Query = mysql_query ("select * from user where username = '$ user '");

$ Us = is_array ($ row = mysql_fetch_array ($ query ));

$ Ps = $ us? $ Md5pass = $ row ['Password']: FALSE;
If ($ ps ){
$ Counts = $ row ['login _ counts'] + 1;
$ _ SESSION ['user'] = $ row ['username'];
$ _ SESSION ['login _ time'] = $ row ['login _ time'];
$ _ SESSION ['login _ counts'] = $ counts;
$ Ip = get_client_ip (); // obtain the logon IP Address
$ Logintime = mktime ();
$ Rs = mysql_query ("update user set login_time = '$ logintime', login_ip =' $ ip ',
Login_counts = '$ counts '");
If ($ rs ){
$ Arr ['success'] = 1;
$ Arr ['msg '] =' login successful! ';
$ Arr ['user'] =_ _ SESSION ['user'];
$ Arr ['login _ time'] = date ('Y-m-d H: I: s', $ _ SESSION ['login _ time']);
$ Arr ['login _ counts'] = $ _ SESSION ['login _ counts'];
} Else {
$ Arr ['success'] = 0;
$ Arr ['msg '] = 'logon failed ';
}
} Else {
$ Arr ['success'] = 0;
$ Arr ['msg '] = 'user name or Password error! ';
}
Echo json_encode ($ arr); // output json data
}
Elseif ($ action = 'logout') {// exit
Unset ($ _ SESSION );
Session_destroy ();
Echo '1 ';
}
This article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.