Remember the password implementation for PHP login

  • 2020-06-01 08:54:27
  • OfStack

form page
 
<form action="login.php" method="post"> 
 The user name <input type="text" name="username"><br> 
 password <input type="text" name="password"><br> 
 Remember the password <input type="checkbox" name="jizhu" value="1"><br> 
<input type="submit"> 
</form> 
<a href="userinfo.php"> The user information </a> 
<?php 
session_start(); 
function p($arr){ 
echo "<pre>"; 
print_r($arr); 
echo "</pre>"; 
} 
p($_SESSION); 
p($_COOKIE); 
?> 

The login page
 
<?php 
function p($arr){ 
echo "<pre>"; 
print_r($arr); 
echo "</pre>"; 
} 
session_start(); 
//p($_POST); 
$_SESSION['username'] = $_POST['username']; 
$_SESSION['password'] = $_POST['password']; 
if($_POST['jizhu']){ 
setcookie('username',$_POST['username'],time()+60); 
setcookie('password',$_POST['password'],time()+60); 
} 
header("location:form.php"); 

User information page
 
<?php 
session_start(); 
echo " Close the browser first before accessing it form.php  Try it, and then   Click push in to close the browser "; 
p($_SESSION); 
p($_COOKIE); 
if(empty($_SESSION['username'])){ 
if(empty($_COOKIE['username'])){ 
echo "<a href='form.php'> Please login first </a>";exit; 
}else{ 
$_SESSION['username'] = $_COOKIE['username']; 
} 
} 
function p($arr){ 
echo "<pre>"; 
print_r($arr); 
echo "</pre>"; 
} 
?> 
2131231231 
<a href="updateuserinfo.php"> Modify the information </a> 
<a href="logout.php"> launch </a> 

Introduction page
 
<?php 
session_start(); 
unset($_SESSION['username']); 
unset($_SESSION['password']); 
setcookie('username','',0); 
setcookie('password','',0); 
header("location:form.php"); 
?> 

Modify the user information page
 
<?php 
session_start(); 
if(empty($_SESSION['username'])){ 
echo " Please login first "; 
}else{ 
echo " Here is the information "; 
} 
?> 

Related articles: