在这里,我将这个示例/ tut 放在一起,因为我怀疑您正在查看一个非常古老的教程;
它非常简单易懂,涵盖了很多方面,包括使用 PDO 安全地连接到数据库并对其进行查询、会话控制以及使用简单类和访问其方法。希望能帮助到你。
<?php
session_start();
class simpleLogin{
public $error;
function __construct($dsn, $user=null, $pass=null){
$this->dsn = $dsn;
$this->user = $user;
$this->pass = $pass;
//Connect
$this->connect();
}
function connect(){
try{
$this->db = new PDO($this->dsn, $this->user, $this->pass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
}catch (Exception $e){
die('Cannot connect to databse. Details:'.$e->getMessage());
}
}
//Get all users from db for drop down box
function get_all_users(){
$sql = "SELECT * FROM users";
$statement = $this->db->query($sql);
$statement->execute();
return $statement->fetchAll();
}
/**
* The main check_login method, this method is called
* on each page load to check status of logged in user
* or handle form POST login.
*
* @return bool
*/
function check_login(){
//Logout
if(isset($_GET['logout'])){$this->logout();}
//Already Logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true){return true;}
//User posted login form
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!empty($_POST['myname'])){
/*
CREATE TABLE `lunch_punch`.`users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(255) NULL ,
PRIMARY KEY (`id`) );
*/
$sql = "SELECT 1 FROM users WHERE username=:username";
$statement = $this->db->prepare($sql);
$statement->bindParam(':username', $_POST['myname']);
$statement->execute();
$result = $statement->fetch();
if(!empty($result)){
$_SESSION['logged_in']=true;
return true;
}else{
return false;
}
}else{
$this->error = 'Please select your name!';
}
}
}
/**
* Logout user and then redirect to index
*
*/
function logout(){
session_destroy();
session_regenerate_id(true);
exit(header('Location: index.php'));
}
}
//Start the login class and pass your mysql connection details
$login = new simpleLogin('mysql:host=127.0.0.1;dbname=lunch_punch','root','password');
//Check the login
if($login->check_login() === true){
//Logged In, wOOt do whatever...
echo 'You are logged in... <a href="?logout">Logout</a>';
}else{
//Logged Out, show login form
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Login by Select Box</title>
</head>
<form method="POST" action="">
<p>Please Login by selecting your name.</p>
<p><select size="1" name="myname">
<option value="" selected>-- Select Your Name --</option>
<?php
//Get all users from database and output into the option box
foreach($login->get_all_users() as $user):?>
<option value="<?php echo $user['username'];?>"><?php echo $user['username'];?></option>';
<?php endforeach;?>
</select>
<input type="submit" value="Login">
</p>
<?php echo ((!empty($login->error))?'<span style="color:red;">'.$login->error.'</span>':null);?>
</form>
<body>
</body>
</html>
<?php } ?>