亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 17
粉絲 0
評(píng)論 1
訪問量 18987
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
應(yīng)用cookie和session實(shí)現(xiàn)登錄驗(yàn)證、退出登錄和注冊(cè)功能-2019年7月26日
無名氏_連的博客
原創(chuàng)
1125人瀏覽過

cookie實(shí)現(xiàn)登錄驗(yàn)證、退出和注冊(cè)功能:

1-1 登錄頁面

實(shí)例

<?php 
if (isset($_COOKIE['username'])) {
	echo '<script>alert("賬號(hào)已登錄");location.assign("index.php");</script>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>登錄</title>
	<link rel="stylesheet" href="static/css/denglu.css">
</head>
<body>
	<div class="center">
		<div class="jz">
			<h2>登錄:</h2>
			<form action="dispatch.php?action=check" method="post" onsubmit="return dlyz();">
				<p>
					<label for="username">賬號(hào):</label>
					<input type="text" name="username" id="username">
				</p>
				<p>
					<label for="password">密碼:</label>
					<input type="password" name="password" id="password">
				</p>
				<p style="margin-left: 100px;">
					<button type="submit">登錄</button>
				</p>
			</form>
		</div>
	</div>
	<script>
		
		function dlyz(){
			var username = document.getElementById('username').value;
			var password = document.getElementById('password').value;
			if (username.length === 0 || password.length === 0) {
				alert('賬號(hào)或密碼不能為空');
				return false;
			}
		}

	</script>
	
</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

1-2 控制臺(tái)頁面

實(shí)例

<?php
require __DIR__.'\public-files/connect.php';
$action = isset($_GET['action'])?$_GET['action']:'login';
$actoin = htmlentities(strtolower(trim($action)));//HTML符號(hào)轉(zhuǎn)實(shí)體,轉(zhuǎn)換小寫,去除左右空格
 switch ($action) {
 	case 'login':
 		include __DIR__.'\denglu.php';
 		break;
	case 'check':
		include __DIR__.'\check.php';
		break;
	case 'tuichu':
		include __DIR__.'\tuichu.php';
		break;
	case 'zcyz':
		include __DIR__.'\zcyz.php';
		break;
	case 'zhuce':
		include __DIR__.'\zhuce.php';
		break;

 	default:
 		header('location:index.php');
 		break;
 }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

1-3 登錄驗(yàn)證后臺(tái)代碼


實(shí)例

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	//獲取表單參數(shù)
	$username = $_POST['username'];
	$password = sha1($_POST['password']);
	 
	//連接數(shù)據(jù)庫進(jìn)行驗(yàn)證
	$sql = 'SELECT * FROM `user` WHERE `username`= :username AND `password`= :password LIMIT 1';
	$stmt = $pdo->prepare($sql);
	// $stmt->bindparam(':username',$username,PDO::PARAM_STR);
	// $stmt->bindparam(':password',$password,PDO::PARAM_STR);
	$stmt->execute(['username'=>$username,'password'=>$password]);
	$user= $stmt->fetch(PDO::FETCH_ASSOC);

	//驗(yàn)證失敗
	if (false === $user) {
		echo "<script>alert('驗(yàn)證失敗');history.back()</script>";
		die;
	}

	//驗(yàn)證成功
	setcookie('username',$username);
	echo "<script>alert('登錄成功');location.assign('index.php');</script>";

}else{
	die('請(qǐng)求類型錯(cuò)誤');
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

1-4 首頁顯示用戶名

實(shí)例

<?php 
//引用PDO實(shí)例化
include 'connect.php';

//首頁數(shù)據(jù)
$sql = "SELECT * FROM `system` WHERE `sys_id`=1";
$mimt = $pdo->prepare($sql);
$mimt->execute();
$system = $mimt->fetch(PDO::FETCH_ASSOC);//查詢單條數(shù)據(jù)
	
//列表數(shù)據(jù)
$sql = "SELECT * FROM `cates`";
$mimt = $pdo->prepare($sql);
$mimt->execute();
$cates = $mimt->fetchAll(PDO::FETCH_ASSOC);

//內(nèi)容數(shù)據(jù)
$sql = "SELECT * FROM `movies`";
$mimt = $pdo->prepare($sql);
$mimt->execute();
$movies = $mimt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="keywords" content='<?php echo $system["key"] ?>'>
	<meta name="fdsjkflk" content='<?php echo $system["desc"] ?>'>
	<link rel="stylesheet" href="static/css/index.css">
	<link rel="shortcut icon" type="image/x-icon" href="static/images/<?php echo $system['logo'] ?>">
	<title><?php echo $system['title'] ?></title>
</head>
<body>	
	<!-- 頭部 -->
	<div class="header">
		<ul>
			<div style="float: left;margin-left: 50px;"><a href="index.php"><img src="static/images/<?php echo $system['logo'] ?>" alt="" width="50" height="30"></a></div>
			<li style="border-left: none;"><a href="./index.php">首頁</a></li>
			<?php foreach($cates as $cate): ?>
			<li><a href="./list.php?cate_id=<?php echo $cate['cate_id']?>"><?php echo $cate['alias'] ?></a></li>
			
			<?php endforeach; ?>
			<li><a href="http://zhibo.renren***/">直播</a></li>
			
			<?php if (isset($_COOKIE['username'])) {
				echo '<a href="dispatch.php?action=tuichu" class="dl">退出</a>';
				echo'<a class="dl">'.$_COOKIE["username"].'</a>';
				
			} else{
				echo '<a href="dispatch.php?action=zhuce" class="dl">注冊(cè)</a>';
				echo '<a href="dispatch.php?action=login" class="dl">登錄</a>';
			}
			?>
			
		</ul>
	</div>
	<div class="clear"></div>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

1-5 退出登錄后臺(tái)代碼

實(shí)例

<?php
if (isset($_COOKIE['username'])) {
	setcookie('username',null,time()-3600);
	echo '<script>alert("退出成功");location.assign("index.php");</script>';
}else{
	 // 要求用戶先登錄
    echo '<script>alert("請(qǐng)先登錄");location.assign("denglu.php");</script>';
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

1-6 注冊(cè)頁面

實(shí)例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>注冊(cè)</title>
	<link rel="stylesheet" href="static/css/denglu.css">
</head>
<body>
	<div class="center">
		<div class="jz">
			<h2>注冊(cè):</h2>
			<form action="dispatch.php?action=zcyz" method="post" onsubmit="return zcyz()">
				<p>
					<label for="username">賬號(hào)?。?lt;/label>
					<input type="text" name="username" id="username">
				</p>
				<p>
					<label for="password1">密碼1:</label>
					<input type="password" name="password1" id="password1">
				</p>
				<p>
					<label for="password2">密碼2:</label>
					<input type="password" name="password2" id="password2">
				</p>
				<p style="margin-left: 100px;">
					<button type="submit">注冊(cè)</button>
				</p>
			</form>
		</div>
	</div>
	<script>
		function zcyz(){
			var username = document.getElementById('username').value;
			var password1 = document.getElementById('password1').value;
			var password2 = document.getElementById('password2').value;
			if (username.length === 0 || password1.length === 0 || password2.length === 0) {
				alert('賬號(hào)或密碼不能為空');
				return false;
			}
			if (password1 != password2) {
				alert('兩次密碼輸入不一致');
				return false;
				}
		}
	</script>
	
</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

1-7 注冊(cè)后臺(tái)驗(yàn)證代碼

實(shí)例

<?php
if ($_SERVER['REQUEST_METHOD'] ==='POST') {
	$username = htmlentities(strtolower(trim($_POST['username'])));
	$password1 = sha1(htmlentities(strtolower(trim($_POST['password1']))));
	$password2 = sha1(htmlentities(strtolower(trim($_POST['password2']))));
	if ($password1 === $password2) {
		//連接數(shù)據(jù)庫進(jìn)行驗(yàn)證
		$sql = 'SELECT * FROM `user` WHERE `username`= :username LIMIT 1';
		$stmt = $pdo->prepare($sql);
		// $stmt->bindparam(':username',$username,PDO::PARAM_STR);
		// $stmt->bindparam(':password',$password,PDO::PARAM_STR);
		$stmt->execute(['username'=>$username]);
		$user= $stmt->fetch(PDO::FETCH_ASSOC);
		if ($username === $user['username']) {
			echo "<script>alert('此賬號(hào)已注冊(cè)');history.back()</script>";
			die;
		}

		$sql = 'INSERT INTO `user` SET `username`=:username,`password`=:password';
		$stmt = $pdo->prepare($sql);
		$stmt->execute(['username'=>$username,'password'=>$password2]);
		if ($stmt->rowCount()>0) {
			setcookie('username',$username);
			echo '<script>alert("注冊(cè)成功");location.assign("index.php");</script>';
		}else{
			echo '<script>alert("注冊(cè)失敗");history.back();</script>';
		}
	}else{
		echo '<script>alert("兩次密碼輸入不一致");history.back();</script>';
	}	
}else{
	die('請(qǐng)求類型錯(cuò)誤');
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


session注冊(cè)功能:

session與cookie最重要的區(qū)別是要開啟session_start會(huì)話,

以下只簡單展示注冊(cè)功能的session流程

2-1注冊(cè)頁面

實(shí)例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>注冊(cè)</title>
	<link rel="stylesheet" href="static/css/denglu.css">
</head>
<body>
	<div class="center">
		<div class="jz">
			<h2>注冊(cè):</h2>
			<form action="dispatch.php?action=zcyz" method="post" onsubmit="return zcyz()">
				<p>
					<label for="username">賬號(hào)?。?lt;/label>
					<input type="text" name="username" id="username">
				</p>
				<p>
					<label for="password1">密碼1:</label>
					<input type="password" name="password1" id="password1">
				</p>
				<p>
					<label for="password2">密碼2:</label>
					<input type="password" name="password2" id="password2">
				</p>
				<p style="margin-left: 100px;">
					<button type="submit">注冊(cè)</button>
				</p>
			</form>
		</div>
	</div>
	<script>
		function zcyz(){
			var username = document.getElementById('username').value;
			var password1 = document.getElementById('password1').value;
			var password2 = document.getElementById('password2').value;
			if (username.length === 0 || password1.length === 0 || password2.length === 0) {
				alert('賬號(hào)或密碼不能為空');
				return false;
			}
			if (password1 != password2) {
				alert('兩次密碼輸入不一致');
				return false;
				}
		}
	</script>
	
</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

2-2 控制臺(tái)(此文檔開啟session會(huì)話)


實(shí)例

<?php
session_start();
require __DIR__.'\public-files/connect.php';
$action = isset($_GET['action'])?$_GET['action']:'login';
$actoin = htmlentities(strtolower(trim($action)));//HTML符號(hào)轉(zhuǎn)實(shí)體,轉(zhuǎn)換小寫,去除左右空格
 switch ($action) {
 	case 'login':
 		include __DIR__.'\denglu.php';
 		break;
	case 'check':
		include __DIR__.'\check.php';
		break;
	case 'tuichu':
		include __DIR__.'\tuichu.php';
		break;
	case 'zcyz':
		include __DIR__.'\zcyz.php';
		break;
	case 'zhuce':
		include __DIR__.'\zhuce.php';
		break;

 	default:
 		header('location:index.php');
 		break;
 }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

2-3 注冊(cè)驗(yàn)證代碼

實(shí)例

<?php
if ($_SERVER['REQUEST_METHOD'] ==='POST') {
	$username = htmlentities(strtolower(trim($_POST['username'])));
	$password1 = sha1(htmlentities(strtolower(trim($_POST['password1']))));
	$password2 = sha1(htmlentities(strtolower(trim($_POST['password2']))));
	if ($password1 === $password2) {
		//連接數(shù)據(jù)庫進(jìn)行驗(yàn)證
		$sql = 'SELECT * FROM `user` WHERE `username`= :username LIMIT 1';
		$stmt = $pdo->prepare($sql);
		// $stmt->bindparam(':username',$username,PDO::PARAM_STR);
		// $stmt->bindparam(':password',$password,PDO::PARAM_STR);
		$stmt->execute(['username'=>$username]);
		$user= $stmt->fetch(PDO::FETCH_ASSOC);
		if ($username === $user['username']) {
			echo "<script>alert('此賬號(hào)已注冊(cè)');history.back()</script>";
			die;
		}

		$sql = 'INSERT INTO `user` SET `username`=:username,`password`=:password';
		$stmt = $pdo->prepare($sql);
		$stmt->execute(['username'=>$username,'password'=>$password2]);
		if ($stmt->rowCount()>0) {
			$_SESSION['username'] = $username;
			echo '<script>alert("注冊(cè)成功");location.assign("index.php");</script>';
		}else{
			echo '<script>alert("注冊(cè)失敗");history.back();</script>';
		}
	}else{
		echo '<script>alert("兩次密碼輸入不一致");history.back();</script>';
	}	
}else{
	die('請(qǐng)求類型錯(cuò)誤');
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

2-4 首頁顯示用戶名

實(shí)例

<?php 
session_start();
//引用PDO實(shí)例化
include 'connect.php';

//首頁數(shù)據(jù)
$sql = "SELECT * FROM `system` WHERE `sys_id`=1";
$mimt = $pdo->prepare($sql);
$mimt->execute();
$system = $mimt->fetch(PDO::FETCH_ASSOC);//查詢單條數(shù)據(jù)
	
//列表數(shù)據(jù)
$sql = "SELECT * FROM `cates`";
$mimt = $pdo->prepare($sql);
$mimt->execute();
$cates = $mimt->fetchAll(PDO::FETCH_ASSOC);

//內(nèi)容數(shù)據(jù)
$sql = "SELECT * FROM `movies`";
$mimt = $pdo->prepare($sql);
$mimt->execute();
$movies = $mimt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="keywords" content='<?php echo $system["key"] ?>'>
	<meta name="fdsjkflk" content='<?php echo $system["desc"] ?>'>
	<link rel="stylesheet" href="static/css/index.css">
	<link rel="shortcut icon" type="image/x-icon" href="static/images/<?php echo $system['logo'] ?>">
	<title><?php echo $system['title'] ?></title>
</head>
<body>	
	<!-- 頭部 -->
	<div class="header">
		<ul>
			<div style="float: left;margin-left: 50px;"><a href="index.php"><img src="static/images/<?php echo $system['logo'] ?>" alt="" width="50" height="30"></a></div>
			<li style="border-left: none;"><a href="./index.php">首頁</a></li>
			<?php foreach($cates as $cate): ?>
			<li><a href="./list.php?cate_id=<?php echo $cate['cate_id']?>"><?php echo $cate['alias'] ?></a></li>
			
			<?php endforeach; ?>
			<li><a href="http://zhibo.renren***/">直播</a></li>
			
			<?php if (isset($_SESSION['username'])) {
				echo '<a href="dispatch.php?action=tuichu" class="dl">退出</a>';
				echo'<a class="dl">'.$_SESSION["username"].'</a>';
				
			} else{
				echo '<a href="dispatch.php?action=zhuce" class="dl">注冊(cè)</a>';
				echo '<a href="dispatch.php?action=login" class="dl">登錄</a>';
			}
			?>
			
		</ul>
	</div>
	<div class="clear"></div>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


cookie.pngckqc.pngzc.pngsess.pngsesstui.pngdl.png

批改狀態(tài):合格

老師批語:還是補(bǔ)之前的作業(yè)呢, 建議之前的作業(yè)先放放, 盡可能跟上進(jìn)度, 否則二頭都顧不上, 之前的作業(yè) , 可以放在雙休日集中完成
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)