批改狀態(tài):未批改
老師批語:
文件結(jié)構(gòu):
根目錄下的文件:
<?php $page_title='首頁'; include('inc/header.php'); if ((isset($_COOKIE['id'])) && basename($_SERVER['PHP_SELF']) != 'loginout.php'){ echo '<a href="loginout.php">退出</a>'; }else{ echo '<a href="login.php">登錄</a>'; } include ('inc/footer.php');
點擊 "運行實例" 按鈕查看在線實例
<?php if ($_SERVER['REQUEST_METHOD']=='POST'){ require 'inc/mysql.php'; require 'inc/function.php'; list($check,$data) = login_check($mysql,$_POST['username'],$_POST['password']); if ($check){ setcookie('id',$data['id']); setcookie('name',$data['name']); jumpto('member.php'); }else{ $errors = $data; echo "<script>alert('".$errors[0]."');</script>"; } mysqli_close($mysql); } require 'login_page.php';
點擊 "運行實例" 按鈕查看在線實例
<?php $page_title = '用戶登錄'; include 'inc/header.php'; ?> <div class="login"> <h3>會員登錄</h3> <form action="login.php" method="post"> <p> <label for="username">賬號:</label> <input type="text" name="username" id="username"> </p> <p> <label for="password">密碼:</label> <input type="password" name="password" id="password"> </p> <p> <button type="submit" name="btn_login" id="btn_login">立即登錄</button> </p> </form> </div> <?php include ('inc/footer.php'); ?>
點擊 "運行實例" 按鈕查看在線實例
<?php if (!isset($_COOKIE['id'])){ require 'inc/function.php'; jumpto('login.php'); }else{ setcookie('id','',time()-3600); setcookie('name','',time()-3600); } $page_title='首頁'; include('inc/header.php'); ?> <div class="main"> <?php echo '<h1 style="color: #ff8bac;">退出成功</h1><a href="login.php">登錄</a><br>'; ?> </div> <?php include ('inc/footer.php'); ?>
點擊 "運行實例" 按鈕查看在線實例
member.php
<?php if (!isset($_COOKIE['id'])){ require 'inc/function.php'; jumpto('login.php'); } $page_title='會員中心'; include 'inc/header.php';?> <div class="main"> <?php echo '<h2>登錄成功</h2><br>'; echo '<span style="color: red;font-size: 2em">用戶id:'.$_COOKIE['id'].'</span>'; echo '<br>'; echo '<span style="color: red;font-size: 2em">用戶姓名:'.$_COOKIE['name'].'</span><a href="loginout.php">退出</a>'; ?> </div> <?php include ('inc/footer.php'); ?>
點擊 "運行實例" 按鈕查看在線實例
inc目錄下的文件:
header.php實例
<!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"> <link rel="stylesheet" href="../0420/css/style.css"> <title> <?php echo $page_title ? $page_title:'未設(shè)置標(biāo)題' ?> </title> <style type="text/css"> </style> </head> <body> <div class="header"> <div class="header_top_bg"> <div class="header_top"> <div class="header_top_left"> <span>歡迎訪問本站!</span> </div> <div class="header_top_right"> <?php if (isset($_COOKIE['id'])){ echo '<span>會員ID:'.$_COOKIE['id'].' </span>'; echo '<span>會員姓名:'.$_COOKIE['name'].' <a href="../0420/loginout.php">退出</a></span>'; }else{ echo '<span><a href="../0420/login.php">登錄</a></span>'; } ?> </div> </div> <div class="header_nav"> <h1>頂部</h1> </div> </div> </div>
點擊 "運行實例" 按鈕查看在線實例
<div class="footer_bg"> <div class="footer"> <h2>底部信息</h2> </div> </div> </body> </html>
點擊 "運行實例" 按鈕查看在線實例
<?php define('dbhost','127.0.0.1'); define('dbuser','xydb'); define('dbpass','123456'); define('dbname','xydb'); define('dbchar','utf8'); $mysql = mysqli_connect(dbhost,dbuser,dbpass,dbname); if (mysqli_connect_errno($mysql)){ echo '數(shù)據(jù)庫連接失敗'.mysqli_connect_errno($mysql); } mysqli_set_charset($mysql,dbchar);
點擊 "運行實例" 按鈕查看在線實例
function.php
<?php function jumpto($page='index.php'){ $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); $url = rtrim($url,'/\\'); $url .= '/'.$page; header('Location:'.$url); exit(); } function login_check($mysql,$username_post,$password_post){ $errors = []; // 賬號驗證 if (empty($username_post)){ $errors[]='用戶名不得為空!'; }else{ $username = mysqli_real_escape_string($mysql,trim($username_post)); } //密碼驗證 if (empty($password_post)){ $errors[]='密碼不得為空!'; }else{ $password = mysqli_real_escape_string($mysql,trim($password_post)); } if (empty($errors)){ $sql= "select `id`,`name` from `xy_user` where `username` = '$username' and `password` = '$password'"; $res = mysqli_query($mysql,$sql); if(mysqli_num_rows($res)==1){ $row = mysqli_fetch_array($res,MYSQLI_ASSOC); return [true,$row]; }else{ $errors[]='賬號或密碼錯誤'; } } return [false,$errors]; } ?>
點擊 "運行實例" 按鈕查看在線實例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號