birk 发表于 2017-3-29 10:06:09

php学习笔记(二十七)php中session的使用(基于url的)

  session有几种使用方式
  1.基于cookie的
  2.基于url的sid字符串的
  3.存储到数据库中的
  4.存储的memcache中的(效率最好的)
  

  写了一个登陆的小例子:
  具体实现如下:
  comm.php


<?php
/**
* 当浏览器禁用掉cookie之后,可以采取传递sessionID
*/
session_start();
echo "id:" . session_id() . "<br>";
//判断用户是否登录,如果未登录实现跳转
if(!$_SESSION["isLogin"]){
header("Location:login.php");
}
?>



conn.inc.php
<?php
$mysqli=new mysqli("localhost", "root", "root", "phpdb");
?>


control.php
<?php
include "conn.inc.php";
echo "你的权限如下:<br>";
$sql="select allow_1, allow_2, allow_3, allow_4 from users where id='{$_SESSION["uid"]}'";
$result=$mysqli->Query($sql);
$user=$result->fetch_assoc();
if($user["allow_1"]){
echo "111111111111111111111111<br>";
}
if($user["allow_2"]){
echo "2222222222222222<br>";
}
if($user["allow_3"]){
echo "333333333333333333333<br>";
}
if($user["allow_4"]){
echo "444444444444444444444444<br>";
}
?>


index.php
<?php
include "comm.php";
echo "用户<b>".$_SESSION["username"]."</b>您好, 这是网站这首页!";
include "control.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<a href="two.php?sid=<?php echo session_id()?>">第二页</a> <br>
<a href="three.php?sid=<?php echo session_id()?>">第三页</a> <br>
<a href="logout.php?sid=<?php echo session_id()?>">退出</a> <br>


  login.php

<?php
session_start();
echo "id:" . session_id() . "<br>";
if (isset($_POST["sub"])) {
include "conn.inc.php";
$sql = "select id from users where name='{$_POST["name"]}' and password='" .
md5($_POST["password"]) . "'";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$_SESSION["username"] = $_POST["name"];
$_SESSION["uid"] = $row["id"];
$_SESSION["isLogin"] = 1;
//跳转到index.php
echo '<script>';
//1.自定义的参数名sid;需要在其他页面开启session的时候先session_id($_GET["sid"]);
//      echo "location='index.php?sid=".session_id()."'";
//2.或者使用常量SID来替换掉PHPSESSDI=xxxxxxxxxxxx
//好处是:如果cookie开启SID为空,如果cookie未开启,则采用SID
//3.或者修改php.ini的session.use_trans_sid=1
//这样只有在php中的跳转需要加上SID,其他的页面跳转不需要加SID了
//4.
echo "location='index.php?PHPSESSID=".session_id()."'";
//一般是基于3的方式
echo '</script>';
}else{
echo "用户名密码有误!<br>";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<head>
<title>用户登录</title>
</head>
<body>
<form action="login.php?PHPSESSID=<?php echo session_id()?>" method="post">
<table align="center" border="1" width="300">
<caption>用户登录</caption>
<tr>
<th>用户名</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>密 码</th>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="sub"
value="登 录"></td>
</tr>
</table>
</form>
</body>
</html>


  logout.php

<?php
include "comm.php";
$username = $_SESSION["username"];
destroySession();
$_SESSION["username"]=$username;
echo $username . "再见!";
/**
* 销毁session
*/
function destroySession(){
//1.开启session
//在新页面中需要先开启session
//session_start();
//2.删除数据
//删除session中的值方法一:
unset($_SESSION["aa"]);
//删除session中的值方法二:
$_SESSION=array();
//3.删除客户端在COOKIE中 sessionID
if (isset($_COOKIE)){
//需要指定cookie的路径在php.ini中session.cookie_path
setcookie(session_name(),'',time()-3600,'/');
};
//4.彻底销毁session
session_destroy();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<br>
<a href="login.php">重新登录</a>

  three.php

<?php
include "comm.php";
echo "用户<b>".$_SESSION["username"]."</b>您好, 这是网站这三个个页面!";
include "control.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<a href="two.php?PHPSESSID=<?php echo session_id()?>">第二页</a> <br>
<a href="three.php?PHPSESSID=<?php echo session_id()?>">第三页</a> <br>
<a href="logout.php?PHPSESSID=<?php echo session_id()?>">退出</a> <br>


  two.php

<?php
include "comm.php";
echo "用户<b>".$_SESSION["username"]."</b>您好, 这是网站这二个页面!";
include "control.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<a href="two.php?PHPSESSID=<?php echo session_id()?>">第二页</a> <br>
<a href="three.php?PHPSESSID=<?php echo session_id()?>">第三页</a> <br>
<a href="logout.php?PHPSESSID=<?php echo session_id()?>">退出</a> <br>



  

  

  

  

  

  

  

  

  

  
页: [1]
查看完整版本: php学习笔记(二十七)php中session的使用(基于url的)