PHP + jQuery + Ajax enables user login and exit, jqueryajax. PHP + jQuery + Ajax enables user logon and exit. the jqueryajax user logon and exit functions are used in many places. in some projects, we need to use Ajax to log on, login to PHP + jQuery + Ajax to achieve user login and exit, jqueryajax
The user logon and exit functions are used in many places. in some projects, we need to use Ajax to log on. after successful logon, we only refresh the page to improve the user experience. This article uses PHP and jQuery to implement the login and exit functions.
Prepare database
In this example, we use the Mysql database to create a user table with the following table structure:
Create table 'user' ('id' int (11) not null auto_increment, 'username' varchar (30) not null comment 'username', 'password' varchar (32) not null comment 'password', 'login _ time' int (10) default NULL comment' logon time', 'login _ IP' varchar (32) default null comment 'logon IP', 'login _ counts' int (10) not null default '0' comment' logon Times ', primary key ('id ')) ENGINE = MyISAM default charset = utf8;
Insert a piece of user information data into the user table:
INSERT INTO `user` (`id`, `username`, `password`, `login_time`, `login_ip`, `login_counts`) VALUES(1, 'demo', 'fe01ce2a7fbac8fafaed7c982a04e229', '', '', 0);
Index. php
After entering the user name and password, the user is prompted that the user has successfully logged on, and relevant logon information is displayed. if you click "exit", the user logon page is displayed.
Go to index. php. if the user is logged on, the logon information is displayed. if the user is not logged on, the logon box is displayed, asking the user to log on.
User logon <? Php if (isset ($ _ SESSION ['user']) {?>
<? Php echo $ _ SESSION ['user'];?>Congratulations!
You are the first <? Php echo $ _ SESSION ['login _ counts'];?> Login site.
The last time I log on to this site was: <? Php echo date ('Y-m-d H: I: S', $ _ SESSION ['login _ time']);?>
[Exit]
<? Php} else {?>
User name:
Password:
<? Php }?>
Note that in index. the PHP file header should be added with the statement session_start. at the same time, the jquery library and global. js, you can also write a beautiful CSS style for the login box, of course, this example has slightly written a simple style, please refer to the source code.