PHP + jQuery + Ajax for user login and exit. jqueryajax_PHP tutorial

Source: Internet
Author: User
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']);?>


<? Php} else {?>

User name:


<? 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.

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: 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.