PHP User Login and exit

Source: Internet
Author: User

Login Page

Login.html is responsible for collecting user-filled login information.

<fieldset><legend> User Login </legend><form name= "LoginForm" method= "post" action= "login.php" Onsubmit= "return Inputcheck (This)" ><p><label for= "username" class= "label" > User name:</label>< Input id= "username" name= "username" type= "text" class= "input"/><p/><p><label for= "password" class= "Label" > Password: </label><input id= "password" name= "password" type= "password" class= "input"/><p/> <p><input type= "Submit" name= "Submit" value= "  OK  " class= "left"/></p></form></ Fieldset>

JavaScript detection and CSS style can refer to reg.html, this section is omitted, you can directly view the final appendix of the complete code.

Login Processing

Login.php is responsible for handling user login and exit actions.

Login if (!isset ($_post[' submit ')) {    exit (' illegal access! ');} $username = Htmlspecialchars ($_post[' username ')), $password = MD5 ($_post[' password ');//contains database connection file include (' conn.php ' );//detect the user name and password correctly $check_query = mysql_query ("Select uid from user where username= ' $username ' and password= ' $password ' Limit 1 "), if ($result = mysql_fetch_array ($check _query)) {    //Login succeeded    $_session[' username '] = $username;    $_session[' userid '] = $result [' uid '];    echo $username, ' Welcome! Enter <a href= "my.php" > User Center </a><br/> ';    Echo ' Click here <a href= "Login.php?action=logout" > Logout </a> Login! <br/> ';    Exit;} else {    exit (' Login failed! Click here <a href= "Javascript:history.back (-1);" > Return </a> retry ');}

This code first confirms that if the user is logged in, it must be a POST action submission. Then according to the user input information to check whether the database is correct, if correct, register session information, otherwise prompt login failed, the user can retry.

The code needs to enable the Session_Start () function at the beginning of the page, see the Exit Processing code section below.

Exit processing

The code that handles the user exit and the code that handles the login are in login.php.

Session_Start ();//Logout Login if ($_get[' action '] = = "Logout") {    unset ($_session[' userid ']);    unset ($_session[' username ');    Echo ' Logout login succeeded! Click here <a href= "login.html" > Login </a> ";    Exit;}

This code is only allowed to be accessed in a login.php?action=logout manner until the code is processed by the user, and other methods are considered to be detecting user logons. See the complete code of the appendix for specific logic.

User Center

my.php is the User Center, listed in the tutorial as a user login detection reference.

<?phpsession_start ();//Check whether login, if not login to the login interface if (!isset ($_session[' userid ')) {    header ("Location:login.html") ;    Exit ();} Include database connection file include (' conn.php '); $userid = $_session[' userid '); $username = $_session[' username ']; $user _query = Mysql_  Query ("SELECT * from user where uid= $userid limit 1"); $row = mysql_fetch_array ($user _query); Echo ' User information: <br/> '; echo ' User id: ', $userid, ' <br/> '; Echo ' username: ', $username, ' <br/> '; Echo ' email: ', $row < ' email ';, ' <br/> '; Echo ' Registration date: ', Date ("y-m-d", $row [' regdate ']), ' <br/> '; Echo ' <a href= ' login.php?action=logout ' > Logout </a > Login <br/> ';? >
Tips
    1. User Registration login involves the user information and database interaction, so pay special attention to the user submitted information can not be illegal information, in this example, the registration section has used regular expression restrictions, the login part is only simple use of htmlspecialchars () processing, practical application can be more stringent.
    2. This tutorial simply demonstrates the user registration and login process, the code is only for learning reference, not directly for the project production.
    3. In this tutorial, you can use a session to manage a user's login success, or use a cookie to manage it, especially for time-limited requirements.
    4. In order to improve the user experience, the user Registration section can be combined with AJAX to detect user-entered information without having to wait for a click to submit and then detect.
    5. Click here to download the complete source code compression package for this tutorial.

PHP User Login and exit

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.