Blogger Information
Blog 25
fans 2
comment 0
visits 18573
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP教学-SESSION实战、PDO练习手抄,PDO总结_11.22
果莫个人博客
Original
620 people have browsed it

SESSION实战

index.php

<?phpsession_start();if (isset($_SESSION['phone']) ){    echo '用户'.$_SESSION['phone'].'已登录<br>';    echo '<a href="dispatch.php?a=logout">退出</a>';}else{    echo'<a href="dispatch.php?a=login">请登录</a>';}

login.php

<?phpif (isset($_SESSION['phone'])){    echo '<script>alert("不要重复登录");location.assign("index.php");</script>';}?><!doctype html><html lang="en"><head><meta charset="UTF-8"><title>用户登录</title></head><body><h3>用户登录</h3><form action="dispatch.php?a=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="pwd" id="pwd">    </p>    <p>        <button>提交</button>    </p></form><script>    function isEmpty() {        var phone = document.getElementById('phone').value;        var password = document.getElementById('pwd').value;        if (phone.length=== 0 || password.length===0) {            alert('手机和密码不能为空');            return false;        }    }</script></body></html>

logout.php    

<?php// 必须在用户已经登录的情况下再退出if (isset($_SESSION['phone'])) {    session_destroy();    echo '<script>alert("退出成功");location.assign("index.php");</script>';} else {    // 要求用户先登录    echo '<script>alert("请先登录");location.assign("login.php");</script>';}

connect.php

<?php//这两个方法不再用://    php mysql  5.4以下版本//    php mysqli 5.4以上版本////主流方法://    PDO是php data object 的简写 php数据对象//PDO支持不同类型的数据库。PDO是最方便访问数据库的接口////PDO连接//$pdo= new PDO('mysql:host=localhost;dbname=shujuku','root123','root123');//print_r($pdo);//数据库连接参数$db=[    'type'=>'mysql',    'host'=>'localhost',    'dbname'=>'shujuku',    'username'=>'123456',    'password'=>'123456'];$dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";//echo $dsn;//echo '<hr>';//$pdo = new PDO($dsn,$db['username'],$db['password']);////print_r($pdo);//echo'<hr>';//连接数据库try{    $pdo = new PDO($dsn,$db['username'],$db['password']);}catch (PDOException $e) {    die('错误信息:'. $e->getMessage());}//print_r($pdo);//PDO、是PHP系统中的类,我们在自己的php文件里是看不到代码的,只能通过打印查看

dispatch.php

<?phpsession_start();include 'connect.php';$a=isset($_GET['a'])?$_GET['a']:'logout';$a=htmlentities(strtolower(trim($a)));switch ($a){    case    'login':        include 'login.php';        break;    case 'check':        include 'check.php';        break;    case 'logout':        include 'logout.php';        break;    case 'index':        include 'index.php';        break;}

check.php

<?phpif ($_SERVER['REQUEST_METHOD'] === 'POST') {    $phone = $_POST['phone'];    $pwd = md5($_POST['pwd']);    $sql = 'SELECT * FROM `user` WHERE `phone` = :phone AND `pwd` = :pwd LIMIT 1';    $stmt = $pdo->prepare($sql);    $stmt->execute(['phone'=>$phone, 'pwd'=> $pwd ]);    $user = $stmt->fetch(PDO::FETCH_ASSOC);    // 4. 判断验证的结果    if (false === $user) {        // 验证失败,返回上一下页面        echo '<script>alert("验证失败");history.back();</script>';        die;    }    // 验证成功,将用户的信息写到session    $_SESSION['phone'] = $user['phone'];    echo '<script>alert("登录成功");location.assign("index.php");</script>';    exit;} else {    die('请求类型错误');}

PDO操作数据库基本流程

1.连接数据库

$pdo = new PDO ($dsn,$username,$password);

2.执行SQL语句完成数据库操作

①创建SQL语句对象:$stmt = $pdo ->prepare($sql)

$sql就是增删查改标准语句例如:
$sql = ‘SELECT * FROM movies WHERE cate_id = :cate_id’;

②执行SQL语句:$stmt->execute()
③对执行结果进行处理

3.销毁PDO对象关闭连接:$pdo=null;


1.连接数据库———基本实例:

//数据库连接参数
$db=[
   ‘type’=>’mysql’,
   ‘host’=>’localhost’,
   ‘dbname’=>’shujuku’,
   ‘username’=>’123456’,
   ‘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());
}

2.执行SQL语句完成数据库操作———基本实例:

$sql = ‘INSERT INTO category SET name=:name , alias=:alias’;

①创建SQL语句对象:

$stmt = $pdo ->prepare($sql)

②执行SQL语句:

//先绑定:$stmt->bindParam(‘name’,$name,PDO::PARAM_STR);
$stmt ->execute();

也可以这样绑定+执行:$stmt ->execute([‘name’=>$name,’name2’=>$name2]);

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错,继续加油。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post