Phpcookie application instance

Source: Internet
Author: User
HtmlheadtitleLogintitlemetahttp-equivContent-Typecontenttexthtml; charsetgb2312headbodyformnameform1methodpostactionlogin. phptablealigncentercellpadding2cellspacing2trtdwidth

HtmlheadtitleLogin/titlemeta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/headbodyform name = "form1" method = "post" action = "login. php "table width =" 300 "border =" 0 "align =" center "cellpadding =" 2 "cellspacing =" 2 "tr td width ="

<Html> <pead> <title> Login </title> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> </pead> <body> </ptml>

<? Php @ mysql_connect ("localhost", "root", "1981427") // before selecting a database, connect to the database server or die ("database server connection failed "); @ mysql_select_db ("test") // select the database mydbor die ("the database does not exist or is unavailable"); // obtain the user input $ username = $ _ POST ['username']; $ passcode = $ _ POST ['passcode']; $ cookie =$ _ POST ['cooker']; // execute an SQL statement $ query = @ mysql_query ("select username, userflag from users ". "where username = '$ username' and passcode =' $ passcode'") or die ("SQL statement execution failed"); // determine whether the user exists , The password is correct if ($ row = mysql_fetch_array ($ query) {if ($ row ['userflag'] = 1 or $ row ['userflag'] = 0) // determine whether the user permission information is valid {switch ($ cookie) // set the cookie retention time {case 0: // Save the Cookie as the browser process setcookie ("username", $ row ['username']); break; case 1: // save the one-day setcookie ("username ", $ row ['username'], time () + 24*60*60); break; case 2: // save 30-day setcookie ("username ", $ row ['username'], time () + 30*24*60*60); break; case 3: // Save the 365-day setcookie ("use Rname ", $ row ['username'], time () + 365*24*60*60); break;} header (" location: main. php "); // automatically jump to main. php} else {echo "incorrect user permission information" ;}} else {echo "incorrect user name or password" ;}?>

<? Phpsession_start (); if (isset ($ _ COOKIE ['username']) {@ mysql_connect ("localhost", "root", "1981427 ") // before selecting a database, connect to the database server or die ("database server connection failed"); @ mysql_select_db ("test ") // select the database mydbor die ("the database does not exist or is unavailable"); // obtain Session $ username =$ _ COOKIE ['username']; // run the SQL statement to obtain the userflag value $ query = @ mysql_query ("select userflag from users ". "where username = '$ username'") or die ("SQL statement execution failed"); $ row = mysql_fetch_array ($ query); // obtain User Permissions Limited information $ flag = $ row ['userflag']; // output different welcome information based on the value of userflag if ($ flag = 1) echo "Welcome administrator ". $ _ SESSION ['username']. "log on to the system"; if ($ flag = 0) echo "welcome ". $ _ SESSION ['username']. "log on to the system"; echo "log off";} else {echo "You are not authorized to access this page" ;}?>

<? Phpsetcookie ("username"); echo "logout successful";?>

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.