- Check if the user is logged in
- function Checklogin () {
- if (Emptyempty ($_session[' User_info ')) {//check to see if the SESSION is empty
- if (Emptyempty ($_cookie[' username ')) | | emptyempty ($_cookie[' password ')) {//If the session is empty and the user does not choose to log the login form
- Header ("location:login.php?req_url=". $_server[' Request_uri '); Go to the login page, log the URL of the request, log in and jump past, and the user experience is good.
- }else{//user selected Remember login status
- $user = GetUserInfo ($_cookie[' username '],$_cookie[' password '); To fetch the user's personal data
- if (Emptyempty ($user)) {//username password not to fetch information, go to login page
- Header ("location:login.php?req_url=". $_server[' Request_uri ');
- }else{
- $_session[' user_info '] = $user; Username and password Right, put the user's personal data into the session
- }
- }
- }
- }
Copy CodeNote: When accessing each page in the background, you must first check the above Second, the user submits the login information when the user fills in the user name and the password, submits here.
/**
- Detection of user-submitted login information
- link:http://bbs.it-home.org
- */
- $username = Trim ($_post[' username ');
- $password = MD5 (Trim ($_post[' password '));
- $validatecode = $_post[' Validatecode ');
- $ref _url = $_get[' Req_url ');
- $remember = $_post[' remember ');
$err _msg = ";
- if ($validatecode!=$_session[' checksum ') {
- $err _msg = "Incorrect verification code";
- }elseif ($username = = "| | $password = =") {
- $err _msg = "User name and password cannot be empty";
- }else{
- $row = GetUserInfo ($username, $password);
if (Emptyempty ($row)) {
- $err _msg = "username and password are not correct";
- }else{
- $_session[' user_info '] = $row;
- if (!emptyempty ($remember)) {//If the user chooses, log the login status and put the username and secret password into the cookie
- Setcookie ("username", $username, Time () +3600*24*365);
- Setcookie ("Password", $password, Time () +3600*24*365);
- }
- if (Strpos ($ref _url, "login.php") = = = = False) {
- Header ("Location:". $ref _url);
- }else{
- Header ("location:main_user.php");
- }
- }
- }
- $username = Trim ($_post[' username ');
- $password = MD5 (Trim ($_post[' password '));
- $validatecode = $_post[' Validatecode ');
- $ref _url = $_get[' Req_url ');
- $remember = $_post[' remember ');
$err _msg = ";
- if ($validatecode!=$_session[' checksum ') {
- $err _msg = "Incorrect verification code";
- }elseif ($username = = "| | $password = =") {
- $err _msg = "User name and password cannot be empty";
- }else{
- $row = GetUserInfo ($username, $password);
if (empty ($row)) {
- $err _msg = "username and password are not correct";
- }else{
- $_session[' user_info '] = $row;
- if (!empty ($remember)) {//If the user chooses, log the login status and put the username and secret password into the cookie
- Setcookie ("username", $username, Time () +3600*24*365);
- Setcookie ("Password", $password, Time () +3600*24*365);
- }
- if (Strpos ($ref _url, "login.php") = = = = False) {
- Header ("Location:". $ref _url);
- }else{
- Header ("location:main_user.php");
- }
- }
- }
Copy CodeAbout $ref_url Explanation: if: User a access to b.php, but a user is not logged in, jump to the login page login.php, after the login page completed user and password, determined to jump to b.php this page, instead of jumping to a default page Main_ user.php. The user experience is better because b.php is the page that user a wants to visit. Third, when the user exits, clear record login status when the user points out, be sure to clear the current login status, so that malicious people use your login information to destroy OH.
- Sign Out
- function logout () {
- unset ($_session[' user_info ');
- if (!emptyempty ($_cookie[' username ')) | | emptyempty ($_cookie[' password ')) {
- Setcookie ("username", NULL, Time () -3600*24*365);
- Setcookie ("password", NULL, Time () -3600*24*365);
- }
- }
- ?>
Copy CodeOK, about using PHP to remember password, automatic login method introduced, I hope you have something to gain. Scripting Academy, focus on you. |