PHP example code to implement user login cases

Source: Internet
Author: User
Tags php example
This article mainly introduces the PHP implementation of user login case code, small series feel very good, now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

This article describes the case code for PHP to implement user login. Share to everyone for your reference, as follows:

Requirements Analysis:

In Web application development, it is often necessary to implement the function of user login. Suppose a user named username, when the user enters the homepage of the site, if not logged in, the page will prompt to log in, the user entered the login information to verify, verify by entering the user center, otherwise display user name or password error, re-login. After successful login, the user can also click "Logout" to return to the login page.

Programming Flowchart:

Login.html


<! DOCUMENT HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd "> 


login.php


<?php header ("Content-type:text/html;charset=utf-8");  Session_Start ();   if (isset ($_post[' login ')) {$username = Trim ($_post[' username '));   $password = Trim ($_post[' password '); if ($username = = ") | |   ($password = = "))    {Header (' refresh:3;url=login.html ');    echo "User name or password can not be empty, 3 seconds after jumping to the login page";   Exit } else if ($username! = ' username ') | |   ($password! = ' password '))    {//Username or password error header (' refresh:3;url=login.html ');    echo "User name or password error, 3 seconds after the jump to the login page";   Exit } else if (($username = = ' username ') && ($password = = ' password ')) {//login successfully saved information to SESSION $_session[' username ']    = $username;    $_session[' IsLogin ']=1;     If the tick is automatically saved within 7 days, save it to the cookie if ($_post[' Remember ']== "yes") {Setcookie ("username", $username, Time () +7*24*60*60);    Setcookie ("Code", MD5 ($username. MD5 ($password)), Time () +7*24*60*60);     } else {Setcookie ("username", "", Time ()-1);    Setcookie ("Code", ", Time ()-1);   }//Jump to the user Homepage header (' refresh:3;url=index.php '); }}?>


index.php


<?php  Header ("Content-type:text/html;charset=utf-8");  Session_Start ();  First determine if the COOKIE has the remember user information  if (isset ($_cookie[' username '))  {   $_session[' username ']=$_cookie[' username '] ;   $_session[' IsLogin ']=1;  }  if (Isset ($_session[' IsLogin '))  {   //already signed in   to echo $_session[' username ']. ": Hello, Welcome to the personal center! <br/> ";   echo "<a href= ' logout.php ' > Logout </a>";  }  else  {//For login   echo "You are not yet logged in, please <a href= ' login.html ' > Login </a>";  }?>


logout.php


<?php  Header ("Content-type:text/html;charset=utf-8");  Session_Start ();  Clear SESSION  $username =$_session[' username '];  $_session=array ();  Session_destroy ();  Clear Cookie  Setcookie ("username", "', Time ()-1);  Setcookie ("Code", ", Time ()-1);  echo "$username, welcome to the next visit";  echo "re-<a href= ' login.html ' > Login </a>";?>


Login interface:

The above is a small series to introduce you to the PHP implementation of user login case code, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of PHP Chinese network!


Articles you may be interested in:

thinkphp example of a method that prevents duplicate submissions of a form to parse a PHP instance

PHP tips for using the PDO abstraction layer to get query results

PHP page Display method analysis "with PHP General page-type PHP tips


Related Article

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.