PHP user login code session, cookie automatic memory function_PHP tutorial
WBOYOriginal
2016-07-13 16:59:471182browse
When users log in, there are many basic user logins like my example. Running to the specified page is not safe. If the user knows your address, there is no need to log in. Example 2 uses session, which is also more commonly used in operations. The page has added session verification, but cannot remember the next login. Example 3 uses session and cookie to log in simultaneously and can automatically record the next automatic login function.
if ($name && $passowrd){
$sql = "SELECT * FROM liuyanban WHERE name = '$name' and password='$passowrd'";
$res = mysql_query($sql);
$rows=mysql_num_rows($res);
if($rows){
header("refresh:0;url=a.htm");//Jump to the page, pay attention to the path
exit;
}
echo "";
}else {
echo "";
}
?>
There is another one written by me who just learned PHP
代码如下
复制代码
This is a rendering of the login page, other login aliases and passwords
if(!preg_match("/^w+$/",$myname) || strlen($myname)<3 || strlen($myname)>15 ){
alert('The entered username information is incorrect! The username must be composed of numbers, underscores, and English letters, and the length is 3-15 characters!','');
}
if(!preg_match("/^w+$/",$mypass) || strlen($mypass)<6 || strlen($mypass)>15 ){
alert('Enter the user password! The password must be composed of numbers and underscores, English letters, and the length is 6-15 characters!','');
}
$sql="select * from tbn where admin_name='$myname' and admin_pwd='".md5($mypass)."'";
$result =mysql_query($sql);
if(mysql_num_rows($result) ){
$my =mysql_fetch_array($result);
$_SESSION['uid']=$myname;
//$_SESSION['auth']=return_auth($my['group_id']); //This is because the user group is used to obtain the permissions of the user group
header("location:main.php");
}else{
alert('Tips: The username and password you entered are inconsistent!','');
}
?>
In the above example, I only saved the information to the session. Let’s take a look at using session and cookie to save user login information at the same time
1. Database connection device page: connectvars.php
//Insert relevant information to connect to the database
require_once ""connectvars.php"";
//Open a session
session_start();
$error_msg = "";
//If the user is not logged in, that is, if $_SESSION[""user_id""] is not set, execute the following code
if(!isset($_SESSION[""user_id""])){
If(isset($_POST[""submit""])){//When the user submits the login form, execute the following code
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$user_username = mysqli_real_escape_string($dbc,trim($_POST[""username""]));
$user_password = mysqli_real_escape_string($dbc,trim($_POST[""password""]));
if(!empty($user_username)&&!empty($user_password)){
//The SHA() function in MySql is used to perform one-way encryption of strings
$query = "SELECT user_id, username FROM mismatch_user WHERE username = ""$user_username"" AND "."password = SHA(""$user_password"")";
$data = mysqli_query($dbc, $query);
//Use username and password to query. If the found record is exactly one, set SESSION and COOKIE, and redirect the page at the same time
If(mysqli_num_rows($data)==1){
$row = mysqli_fetch_array($data);
$_SESSION[""user_id""]=$row[""user_id""];
$_SESSION[""username""]=$row[""username""];
setcookie(""user_id"", $row[""user_id""], time()+(60*60*24*30));
setcookie(""username"", $row[""username""], time()+(60*60*24*30));
$home_url = ""loged.php"";
header(""Location: "".$home_url);
}else{//If the found record does not match the error, set the error message
$error_msg = ""Sorry, you must enter a valid username and password to log in."";
}
}else{
$error_msg = ""Sorry, you must enter a valid username and password to log in."";
}
}
}else{//If the user is already logged in, jump directly to the logged in page
$home_url = ""loged.php"";
header(""Location: "".$home_url);
}
?>
Mismatch - Log In
& Lt;!-Through the $ _Sactive ["User_id" "], if the user fails to log in, the login form is displayed so that the user enters the username and the code-& gt;
If(!isset($_SESSION[""user_id""])){
echo ""
"";
echo "" Log Out("".$_SESSION[""username""]."")"";
}
/**In the logged-in page, the user's session can be deceived, such as $_SESSION[""username""],
* $_SESSION[""user_id""] queries the database and can do a lot of work*/
?>
4. Log out the session and cookie page: logOut.php (redirect to lonIn.php after publishing)
/**Publish session and cookie pages at the same time*/
//Even when publishing, you must first start a session to access session variables
session_start();
//Use a session variable to check the login status
if(isset($_SESSION[""user_id""])){
//To clear session variables, set the $_SESSION super global variable to an empty array
$_SESSION = array();
//If a session cookie exists, delete it by setting the expiry time to 1 hour before
If(isset($_COOKIE[session_name()])){
setcookie(session_name(),"""",time()-3600);
}
//Apply the built-in session_destroy() function call to cancel the session
Session_destroy();
}
//At the same time, set the expiration time of each cookie to a time in the past so that they can be deleted by the system. The time is in seconds
setcookie(""user_id"","""",time()-3600);
setcookie(""username"","""", time()-3600);
//The location header redirects the browser to another page
$home_url = ""logIn.php"";
header(""Location:"".$home_url);
?>
User registration and login involves the interaction between user information and the database, so special attention must be paid to the information submitted by the user not being illegal. In this example, the registration part has been restricted using regular expressions, and htmlspecialchars is simply used for the login part. () processing, it can be more stringent in actual application.
This tutorial simply demonstrates the process of user registration and login. Its code is for learning reference only and cannot be directly used for project production.
In this tutorial, session is used to manage the user after successful login. Cookie can also be used to manage, especially for time-limited requirements.
In order to improve the user experience, the user registration part can be combined with AJAX to detect the information entered by the user without waiting for the user to click submit.
http://www.bkjia.com/PHPjc/631288.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631288.htmlTechArticleThere are many kinds of basic user logins like my example. It is unsafe to run to the specified page. If the user knows your address, there is no need to log in. Example 2 uses...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn