PHP Registration and Login "3" User login and exit

Source: Internet
Author: User

Login Page

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

1 <fieldset>2 <legend>User Login</legend>3 <formname= "LoginForm"Method= "POST"Action= "login.php"OnSubmit= "return Inputcheck (This)">4 <P>5 <label for= "username"class= "Label">User name:</label>6 <inputID= "username"name= "username"type= "text"class= "Input" />7 <P/>8 <P>9 <label for= "Password"class= "Label">Password</label>Ten <inputID= "Password"name= "Password"type= "Password"class= "Input" /> One <P/> A <P> - <inputtype= "Submit"name= "Submit"value= "OK"class= "Left" /> - </P> the </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.

1 //Login2 if(!isset($_post[' Submit '])){3     Exit(' Illegal access! '));4 }5 $username=Htmlspecialchars($_post[' username ']);6 $password=MD5($_post[' Password ']);7 8 //include database connection file9 include(' conn.php ');Ten //Check that the username and password are correct One $check _query=mysql_query("Select UID from user where username= '$username' and password= '$password' Limit 1 '); A if($result=Mysql_fetch_array($check _query)){ -     //Login Successful -     $_session[' username '] =$username; the     $_session[' userid '] =$result[' UID ']; -     Echo $username, ' welcome you! 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.

1 Session_Start();2 3 //Log off login4 if($_get[' action '] = = "Logout"){5     unset($_session[' UserID ']);6     unset($_session[' username ']);7     Echo' Logout login succeeded! Click here <a href= "login.html" > Login </a> ';8     Exit;9}

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.

1<?PHP2 Session_Start();3 4 //check whether to log in, and then turn to the login screen if not logged in5 if(!isset($_session[' UserID '])){6     Header("Location:login.html");7     Exit();8 }9 //include database connection fileTen include(' conn.php '); One $userid=$_session[' UserID ']; A $username=$_session[' username ']; - $user _query=mysql_query("SELECT * from user where uid=$useridLimit 1 "); - $row=Mysql_fetch_array($user _query); the Echo' User information: <br/> '; - Echo' User id: ',$userid, ' <br/> '; - Echo' User name: ',$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.

PHP Registration and Login "3" 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.