4月20日作业
用户登的:cookie(); session()
1、cookie()
cookie() 首页代码:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php $page_title = '首页'; ?> </title> <style> #dm { width: 800px; height:300px; background-color: #eaff56; text-align: center; line-height: 50px; color: red; margin: 0 auto; } </style> </head> <body> <?php include ('inc/header.php'); ?> <div id="dm"> <?php echo '<h2>我是首页</h2>'; if ((isset($_COOKIE['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') { echo '<p><a href="logout.php">退出</a><p/>'; } else { echo '<p><a href="login.php">登录</a></P>'; } ?> </div> <?php include ('inc/footer.php'); ?> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
执行结果图片:
cookie() 登录页代码:
实例
<?php if (!isset($_COOKIE['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } //如果已经登录 //设置页面标题 $page_title = '已经登录'; include ('inc/header.php'); ?> <style> #dy { width: 800px; height:300px; background-color: #eaff56; text-align: center; line-height: 50px; color: red; margin: 0 auto; } </style> <div id="dy"> <!-- //打印欢迎信息,并提供退出功能 echo <<< "WELCOME" --> <h2 style="color:red">登陆成功</h2> <p>欢迎您: <?php echo $_COOKIE['user_name'] ?></p> <p><a href="logout.php">退出</a></p> <!-- WELCOME; --> </div>
运行实例 »
点击 "运行实例" 按钮查看在线实例
已登录显示图片:
cookie() 退出登录页面代码:
实例
<?php if (!isset($_COOKIE['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } else { //删除cookies setcookie('user_id', '', time()-3600); setcookie('user_name','', time()-3600); } //设置页面标题 $page_title = '已经登录'; include ('inc/header.php'); ?> <style> #dz { width: 800px; height:300px; background-color: #eaff56; text-align: center; line-height: 50px; color: red; margin: 0 auto; } </style> <div id="dz"> <!-- 显示退出信息,并提供登录功能 --> <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a></p> </div> <?php include ('inc/footer.php'); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
退出页面执行图片:
登录成功首页图片:
2、session()
首页代码:
实例
<meta charset="utf-8"> <?php session_start(); //开启会话 $page_title = '首页'; include ('inc/header.php'); ?> <style> #d1 { width: 800px; height:300px; background-color: #eaff56; text-align: center; line-height: 50px; color: red; margin: 0 auto; } </style> <div id="d1"> <h2 style="color:red">我是首页</h2> <?php if ((isset($_SESSION['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') { echo '<a href="logout.php">退出</a>'; // <p>欢迎您: {$_SESSION['user_name']}</p> //hello; }else{ echo '<a href="login.php">登录</a>'; } ?> </div> <?php include ('inc/footer.php'); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
首页执行结果图片:
登录页执行结果图片:
退出页执行结果图片:
登录成功首页执行结果图片: