PHP Cookie Login Verification Tips _php Tutorial

Source: Internet
Author: User
Tags set cookie setcookie
The purpose of building a website is the integrity of the function. One of these features is the implementation of user login verification. Let's give you a detailed introduction to the

Sample PHP Cookie Login Verification code:

  1. < html>
  2. < head>
  3. < title>Login< /title>
  4. < meta http-equiv="Content-type"
    content="text/html; charset=gb2312 ">
  5. < /head >
  6. < body>
  7. < form name = Form1 " method = Post "
    action = "login.php" ;
  8. < table width = "border" = "0" align="center"
    cellpadding="2" cellspacing="2" >
  9. < tr>
  10. < TD width = "150" > < Div align = "right" span class= "tag" > ;
    user name: < /div > < /td ;
  11. < td width="Max">< input type="text"
    name="username">< /td >
  12. < /tr >
  13. < tr>
  14. < td>< div align ="right"> Password:< /div> < /td>
  15. < td>< input type= "password" name="passcode"> < /td >
  16. < /tr >
  17. < tr>
  18. < TD > < Div align = Right " ; Cookie Save time: < /div > < /td ;
  19. < td>< Select name ="Cookie " id="cookie">
  20. < option value="0" selected> Browser Process < /option>
  21. < option value="1"> save 1 days < /option >
  22. < option value="2"> Save 30 days < /option >
  23. < option value="3"> Save 365 days < /option >
  24. < /select >< /td >
  25. < /tr >
  26. < /table >
  27. < p align="center">
  28. < input type = "Submit" Name = "Submit" value="Submit">
  29. < input type = "reset" name = "Reset" value="Reset">
  30. < / P >
  31. < /form >
  32. < /body >
  33. < /html >
 
 
  1. < ? PHP
  2. @mysql_connect ("localhost", "root", "1981427")
    You need to connect to the database server before selecting a database
  3. Or Die ("Database server Connection Failed");
  4. @mysql_select_db ("test")//Select Database MyDB
  5. Or Die ("database does not exist or is not available");
  6. Get user input
  7. $ username = $_post[' username '];
  8. $ Passcode = $_post[' passcode '];
  9. $ Cookies = $_post[' Cookie '];
  10. Execute SQL statement
  11. $ Query = @mysql_query ("Select Username,
    Userflag from Users "
  12. ." Where username = ' $username ' and
    Passcode = ' $passcode ' ")
  13. Or Die ("SQL statement execution failed");
  14. Determine if the user exists, the password is correct
  15. if ($row = mysql_fetch_array($query))
  16. {
  17. if ($row [' userflag '] = = 1 or $row [' userflag '] = = 0)
    Determine if user rights information is valid
  18. {
  19. Switch ($cookie)//Set cookie save time according to user's choice
  20. {
  21. Case 0://Save cookie for browser process
  22. Setcookie ("username", $row [' username ']);
  23. Break
  24. Case 1://Save 1 days
  25. Setcookie ("username", $row [' username '],
    Time () +24*60*60);
  26. Break
  27. Case 2://Save 30 days
  28. Setcookie ("username", $row [' username '],
    Time () +30*24*60*60);
  29. Break
  30. Case 3://Save 365 Days
  31. Setcookie ("username", $row [' username '],
    Time () +365*24*60*60);
  32. Break
  33. }
  34. Header ("location:main.php");
    Auto Jump to main.php
  35. }
  36. Else
  37. {
  38. echo "User rights information is incorrect";
  39. }
  40. }
  41. Else
  42. {
  43. echo "User name or password error";
  44. }
  45. ?>

PHP Cookie Login Verification code example:

 
 
  1. < ? PHP
  2. Session_Start ();
  3. if (isset ($_cookie[' username '))
  4. {
  5. @mysql_connect ("localhost", "root", "1981427")
  6. You need to connect to the database server before selecting a database
  7. Or Die ("Database server Connection Failed");
  8. @mysql_select_db ("test")//Select Database MyDB
  9. Or Die ("database does not exist or is not available");
  10. Get session
  11. $ username = $_cookie[' username '];
  12. Execute SQL statement to get Userflag value
  13. $ Query = @mysql_query ("Select
    Userflag from Users "
  14. ." Where username = ' $username '")
  15. Or Die ("SQL statement execution failed");
  16. $ Row = Mysql_fetch_array ($query);
  17. Obtaining User rights Information
  18. $ Flag = $row [' Userflag '];
  19. Output a different welcome message based on the value of Userflag
  20. if ($flag = = 1)
  21. echo "Welcome administrator". $_session[' username '
    ." Login System ";
  22. if ($flag = = 0)
  23. echo "Welcome user". $_session[' username '
    ." Login System ";
  24. echo " < a href="logout.php"
    href = "logout.php" > Logout < /A > ";
  25. }
  26. Else
  27. {
  28. echo "You do not have permission to access this page";
  29. }
  30. ?>
  31. < ? PHP
  32. Setcookie ("username");
  33. echo "Logout success";
  34. ?>

The implementation of the above-mentioned timely PHP cookie login verification.


http://www.bkjia.com/PHPjc/445946.html www.bkjia.com true http://www.bkjia.com/PHPjc/445946.html techarticle The purpose of building a website is the integrity of the function. One of these features is the implementation of user login verification. Below we will give you a detailed description of the PHP cookie login verification 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: 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.