PHP + jQuery + Ajax allows users to log on and exit. PHP + jQuery + Ajax implement user login and exit this article uses Ajax to skip new login and exit, thus improving the user experience. If the user is logged on, PHP + jQuery + Ajax enables the user to log on and exit.
PHP + jQuery + Ajax for user login and exit
This article uses Ajax to enhance the user experience by refreshing new logon and logout. If the user is logged on, the user-related logon information is displayed; otherwise, the logon form is displayed.
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:
?
1 2 3 4 5 6 7 8 9 |
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' login times ', Primary key ('id ') ) ENGINE = MyISAM default charset = utf8; |
Insert a piece of user information data into the user table:
?
1 2 |
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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
User logon If (isset ($ _ SESSION ['user']) { ?> Congratulations! You are the first Login site. The last time I log on to this site is: [Exit] User name: Password: |
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.
?