1、connec.php
<?php $db = [ 'type' => 'mysql', 'host' => '127.0.0.1', 'dbname' => 'baoge', 'username' => 'root', 'password' => 'root' ]; $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; $username = $db['username']; $password = $db['password']; try { $pdo = new PDO($dsn, $username, $password); } catch (PDOException $e) { die('连接失败' . $e->getMessage()); } ?>
点击 "运行实例" 按钮查看在线实例
2、login.php
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <h3>用户登录</h3> <!-- 只要地址,能访问到,就可以把表单提交过去 --> <!-- 账号密码,如果用get提交,是会被浏览器显示出来的。 --> <form action="dispatch.php?a=check" method="post"> <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> </body> </html>
点击 "运行实例" 按钮查看在线实例
3 index.php
<?php // 点击去登录、退出,用户自己触发。 // 登录成功、退出成功,返回首页是js触发的。 // 我们现在没有学习js,所以 做了按钮跳转 // echo time(); if( isset( $_COOKIE['user'] ) ){ echo '登录成功,用户昵称:' . $_COOKIE['user']; echo '<br>'; echo '<a href="dispatch.php?a=out">退出</a>'; }else{ echo '<a href="dispatch.php?a=login">请登录</a>'; } ?>
点击 "运行实例" 按钮查看在线实例
、
4 out.php
<?php setcookie('user',null,time()-3600); echo '已退出'; echo '<br>'; echo '<a href="dispatch.php">首页</a>'; ?>
点击 "运行实例" 按钮查看在线实例
5 dispatch.php
<?php header("Content-Type: text/html;charset=utf-8"); require __DIR__ . '/connect.php'; $a = isset($_GET['a']) ? $_GET['a'] : 'index'; switch ($a) { case 'login': # code... include __DIR__ . '/login.php'; break; case 'check': include __DIR__ . '/check.php'; break; case 'out': include __DIR__ . '/out.php'; break; case 'index': include __DIR__ . '/index.php'; break; default: # code... break; } ?>
点击 "运行实例" 按钮查看在线实例