SESSION登录实战及PDO操作
check.php源码
<?php
session_start();
//echo 'check';die;
//var_dump($_POST);die;
if (isset($_POST['phone']) && isset($_POST['password'])){
$phone=$_POST['phone']?$_POST['phone']:'';
$password=md5($_POST['password']?$_POST['password']:'');
$sql='SELECT `phone`,`pwd`,`name` FROM `user` WHERE `phone`=:phone and pwd=:password LIMIT 1';
$stsm=$pdo->prepare($sql);
$stsm->execute(['phone'=>$phone,'password'=>$password]);
$user=$stsm->fetch(PDO::FETCH_ASSOC);
// var_dump($user);die;
if ($phone===$user['phone'] && $password===$user['pwd']){
// setcookie('name',$user['name'],time()+3600);
$_SESSION['name']=$user['name'];
header('location:index.php');
}else{
echo '<script>alert("验证失败");history.back();</script>';
}
}else{
echo '<script>alert("请求类型错误")</script>';
header('location:index.php');
}
check.php源码
<?php
$db=[
'type'=>'mysql',
'host'=>'localhost',
'dbname'=>'emshop',
'username'=>'root',
'password'=>'123456'
];
$dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
try{
$pdo=new PDO($dsn,$db['username'],$db['password']);
}catch (PDOException $e){
die('连接失败,错误信息是:'.$e->getMessage());
}
//var_dump($pdo);
dispatch源码
<?php
require __DIR__.'/connect.php';
$action=isset($_GET['action'])?$_GET['action']:'login';
$action=htmlentities(strtolower(trim($action)));
switch ($action){
case 'login':
include __DIR__ . '/login.php';
break;
case 'logout':
include __DIR__.'/logout.php';
break;
case 'check':
include __DIR__.'/check.php';
break;
default:
header('location:index.php');
}
index.php源码
<?php
session_start();
if (isset($_SESSION['name'])){
echo '用户名:'.$_SESSION['name'];
echo "<br><a href='dispatch.php?action=logout'>点击退出</a>";
}else{
echo "<a href='dispatch.php?action=login'>用户未登录,点击登陆</a>";
}
login.php源码
<?php
session_start();
//setcookie('name','',time()-1);
if (isset($_SESSION['name']) && !empty($_SESSION['name'])){
echo '<script>alert("用户已登陆")</script>';
header('location:index.php');
die;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h3>用户登录</h3>
<form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
<p>
<label for="phone">手机号:</label>
<input type="phone" name="phone" id="phone">
</p>
<p>
<label for="password">密码:</label>
<input type="password" name="password" id="password">
</p>
<p>
<button>提交</button>
</p>
</form>
<script>
function isEmpty() {
var phone = document.getElementById('phone').value;
var password = document.getElementById('password').value;
if (phone.length=== 0 || password.length===0) {
alert('手机和密码不能为空');
return false;
}
}
</script>
</body>
</html>
logout源码
<?php
session_start();
if(!isset($_SESSION['name'])){
header('location:dispatch.php?action=index');
die;
}
//setcookie('name',NULL,time()-1);
session_destroy();
setcookie('PHPSESSID','',time()-3600,'/');
header('location:dispatch.php?action=index');