Blogger Information
Blog 30
fans 0
comment 0
visits 18009
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4.20cookie与session的用户登录
宋的博客
Original
825 people have browsed it

实例

<?php 

$page_title='首页';
include('inc/header.php');
echo '首页!';

// 登录检测 判断cookie的id是否存在和当前页面是否为退出页面
 if ((isset($_COOKIE['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php'){
 	echo '<a href="logout.php" title="">登录</a>';
 }else {
 	echo '<a href="login.php" title="">退出</a>';
 }
 include('inc/footer.php');
 ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
if (!isset($_COOKIE['user_id'])) {
    require ('inc/function.php');
    //跳转到默认首页
    redirect_user();
}

//如果已经登录
//设置页面标题
$page_title = '已经登录';
// include ('inc/header.php');

//打印欢迎信息,并提供退出功能
echo <<< "WELCOME"
<h2 style="color:red">登陆成功</h2>
<p>欢迎您: {$_COOKIE['user_name']}</p>
<p><a href="logout.php">退出</a></p>
WELCOME;

//加载底部
// include ('inc/footer.php');

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php 

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

	// 验证用户的邮箱和密码,跳转页面
	// 加载函数库
	require('inc/function.php');
	// 连接数据库
	require('inc/connect.php');

	// 验证登录
	list($check,$data) = check_login($dbc,$_POST['email'],$_POST['password']);	

    if ($check) {
        //设置cookies
      setcookie('user_id', $data['user_id']);
       setcookie('user_name', $data['user_name']);
	// 成功跳转
	redirect_user('log.php');
		} else {
	$errors = $data;
				}

// 关闭链接
 mysqli_close($dbc);
}
 // 加载
include('login.php');

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php

include('inc/header.php');


	if (isset($errors) && !empty($errors)) {
    $errors_msg = '<p style="color:red">';
    foreach ($errors as $msg) {
        $errors_msg .= $msg.'<br>';
    }
    echo $errors_msg.'</p>';
}
?>
	<h2>用户登录</h2>
	<form action="login-check.php" method="post">
		<p>
			<label for="email">邮箱:</label>
			<input type="email" name="email" id="email"  value="<?php echo  isset($_POST['email'])?$_POST['email']:'' ?>">
		</p>
		<p>
			<label for="password">密码:</label>
			<input type="password" name="password" id="password"  value="<?php echo  isset($_POST['password'])?$_POST['password']:'' ?>">
		</p>
		<p>
			<button type="submit" name="submit" id="submit">登录</button>
		</p>
	</form>

	 <?php 
	 include('inc/footer.php');
  ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php 

if (!isset($_COOKIE['user_id'])) {
    require ('inc/function.php');
    //跳转到默认首页
    redirect_user();
} else {
	setcookie('user_id','',time()-1);
	setcookie('user_name','',time()-1);
}

$page_title ='退出登录';

//打印退出信息
echo <<< aaa
<h2>退出成功</h2>
<p><a href="login.php" title="">登录</a></p>
aaa;

 ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

QQ图片20180425000410.pngQQ图片20180425000438.png

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!