Implement login authentication based on pear auth

  • 2020-03-31 20:25:30
  • OfStack

Create a page called yz.php
 
<?php 
require_once('Auth.php'); 
function show_login_form () { 
echo '<form method="post" action="yz.php"> 
<p>username:<input type="text" name="username"></p> 
<p>password:<input type="password" name="password"></p> 
<p><input type="submit" value=" submit "></p> 
' ; 
} 
$options=array('dsn'=>'mysql://root:1@localhost/zs', 
'table'=>'users', 
'usernamecol'=>'user_name', 
'passwordcol'=>'user_passwd', 
'cryptType'=>'', 
'db_fields'=>'*', 
); 
$auth=new Auth('DB',$options,'show_login_form'); 
?> 
<html> 
<head> 
</head> 
<body> 
<?php 
$auth->start(); 
if($auth->checkAuth()){ 
echo "ok"; 
echo "<br>"; 
echo "<a href='yzh.php'>add</a>"; 
}else { 
echo "error"; 
} 
//$auth->setSessionName($auth->getAuthData('user_name')); 
?> 
</body> 
</html> 

Login after the session passes the page yzh. PHP
 
<?php 
function back(){ 
echo '<a href="yz.php">back</a>'; 
} 
require_once('Auth.php'); 
$auth=new Auth('DB',$option,'back'); 
$auth->start(); 
if($auth->checkAuth()) 
{ 
echo "this is a session page,welcome "; 
echo "{$auth->getUserName()}"; 
} 
?> 

Related articles: