Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:pdo是主流, 现在几乎都在用, 一定要重视
数据库连接:
<?php $db = [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'localhost', 'username' => 'wenbin0209', 'password' => 'a384309596', 'port' => 3306 ]; $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try { $pdo = new PDO($dsn,$db['username'],$db['password']); } catch(PDOException $e){ die('错误信息:'.$e->getMessage()); } // echo '请求成功'; ?>
点击 "运行实例" 按钮查看在线实例
转发器:
<?php session_start(); $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); // print_r($action); switch($action){ case 'login'; include __DIR__.'/login.php'; break; case 'index'; include __DIR__.'/index.php'; break; case 'check'; include __DIR__.'/check.php'; break; case 'logout': include __DIR__ . '/logout.php'; break; default: header('Location: index.php'); echo '<script>location.assign("index.php");</script>'; }
点击 "运行实例" 按钮查看在线实例
首页:
<?php // 首页判断是否登录 if(isset($_SESSION['username'])){ echo '欢迎'.$_SESSION['username'].'登录'; echo '<a href="dispatch.php?action=logout">退出</a>'; }else { echo '<a href="dispatch.php?action=login">请登录</a>'; }
点击 "运行实例" 按钮查看在线实例
检查用户登录:
<?php // echo $_GET['username']; // echo $_GET['password']; // echo 11111; require('db.php'); if(isset($_POST['username']) && isset($_POST['password'])) { // print_r($_POST['username']); $username = $_POST['username']; $password = $_POST['password']; // 1.先查询登录账号和密码 $sql = 'SELECT * from `ls_admin` where `username` = :username AND `password` = :password'; $stmt = $pdo->prepare($sql); $stmt->execute(['username' => $username,'password'=> $password]); $user = $stmt->fetch(PDO::FETCH_ASSOC); // print_r($user); // exit; if(empty($user)) { echo '未找到该用户'; }else{ $_SESSION['username'] = $user['username']; echo '<script>alert("登录成功");location.assign("dispatch.php?action=index");</script>'; exit; } // print_r($user); }else { echo '请填写账号或密码'; }
点击 "运行实例" 按钮查看在线实例
登录:
<?php echo '请登录'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="dispatch.php?action=check" method="post"> <p> <label for="model">用户名:</label> <input type="text" name="username" id='model'> </p> <p> <label for="pasd">密码:</label> <input type="text" name="password" id='pasd'> </p> <button>登录</button> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
退出登录:
<?php //退出登录 if(isset($_SESSION['username'])){ session_destroy(); echo '退出成功'; }else{ echo '<a href="dispatch.php?action=login">请登录</a>'; }
点击 "运行实例" 按钮查看在线实例
PDO练习:
<?php // 连接数据库 require('db.php'); //-----------查询数据---------- //查询数据 $sql = 'SELECT id,username,nickname from `ls_admin`'; $stmt = $pdo->prepare($sql); $select = $stmt->execute(); // 通过列号绑定 $stmt -> bindColumn(1,$id); // 通过列名绑定 $stmt->bindColumn('username', $name); $stmt->bindColumn('nickname', $nickname); // 返回数据 while($stmt->fetch(PDO::FETCH_ASSOC)){ $data = 'id:'.$id . "\t" . '用户名:'.$name . "\t" . '昵称:'.$nickname . "\n"; echo'<hr>'; print $data; }
点击 "运行实例" 按钮查看在线实例
手写: