session 入库
来源:互联网 发布:张予曦开的淘宝店 编辑:程序博客网 时间:2024/05/17 08:22
<?php
header('content-type:text/html;charset=utf-8');
//将session存储方式设置为存入数据库的方式
//session.save_handler = files
ini_set("session.save_handler", "user");
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
//打开并连接数据库
function open()
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
}
//关闭连接
function close()
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$pdo = null;
}
//从表中中读信息
function read($session_id)
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$read = $pdo->query("select * from session where session_id='$session_id'")->fetch(PDO::FETCH_ASSOC);
return $read["session_info"];
}
//将session存入数据库
function write ($session_id,$session_info)
{
// echo $session_id. " " . $session_info;
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$read = $pdo->query("select * from session where session_id='$session_id'")->fetch(PDO::FETCH_ASSOC);
if(empty($read))
{
$time = time();
$pdo->exec("insert into session (session_id, session_info, session_life) values ('$session_id', '$session_info', '$time')");
}
else
{
$sql = "update session set session_info='$session_info' where session_id='$session_id'";
$pdo->exec($sql);
}
}
//销毁指定session
function destroy($session_id)
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$pdo->exec("delete from session where session_id='$session_id'");
}
//删除所有过期的session
function gc()
{
}
session_start();
//判断是否有session
if(isset($_SESSION['name']))
{
$status = 1;
}
else
{
$status = 0;
}
//获取需要接收的值
$user_name = isset($_POST['user_name'])?$_POST['user_name']:null;
$user_pwd = isset($_POST['user_pwd'])?$_POST['user_pwd']:null;
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$sql = "select * from users where user_name = '$user_name' and user_pwd = '$user_pwd' ";
$re = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
if($re)
{
$status = 1;
$_SESSION['name'] = $user_name;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php if($status == 0){?>
<h3>服务器3</h3>
<form action="index.php" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="user_name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user_pwd"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
<?php } else{?>
<h2>欢迎<?=$_SESSION['name']?>登录</h2>
<h3>服务器3</h3>
<?php }
?>
</body>
</html>
header('content-type:text/html;charset=utf-8');
//将session存储方式设置为存入数据库的方式
//session.save_handler = files
ini_set("session.save_handler", "user");
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
//打开并连接数据库
function open()
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
}
//关闭连接
function close()
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$pdo = null;
}
//从表中中读信息
function read($session_id)
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$read = $pdo->query("select * from session where session_id='$session_id'")->fetch(PDO::FETCH_ASSOC);
return $read["session_info"];
}
//将session存入数据库
function write ($session_id,$session_info)
{
// echo $session_id. " " . $session_info;
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$read = $pdo->query("select * from session where session_id='$session_id'")->fetch(PDO::FETCH_ASSOC);
if(empty($read))
{
$time = time();
$pdo->exec("insert into session (session_id, session_info, session_life) values ('$session_id', '$session_info', '$time')");
}
else
{
$sql = "update session set session_info='$session_info' where session_id='$session_id'";
$pdo->exec($sql);
}
}
//销毁指定session
function destroy($session_id)
{
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$pdo->exec("delete from session where session_id='$session_id'");
}
//删除所有过期的session
function gc()
{
}
session_start();
//判断是否有session
if(isset($_SESSION['name']))
{
$status = 1;
}
else
{
$status = 0;
}
//获取需要接收的值
$user_name = isset($_POST['user_name'])?$_POST['user_name']:null;
$user_pwd = isset($_POST['user_pwd'])?$_POST['user_pwd']:null;
//使用pdo
$pdo = new PDO('mysql:host=192.168.1.54;dbname=yii','root','root');
$pdo->exec('set names utf8');
$sql = "select * from users where user_name = '$user_name' and user_pwd = '$user_pwd' ";
$re = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
if($re)
{
$status = 1;
$_SESSION['name'] = $user_name;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php if($status == 0){?>
<h3>服务器3</h3>
<form action="index.php" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="user_name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user_pwd"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
<?php } else{?>
<h2>欢迎<?=$_SESSION['name']?>登录</h2>
<h3>服务器3</h3>
<?php }
?>
</body>
</html>
0 0
- session入库
- session入库
- session入库
- session入库
- session入库
- Session入库
- session 入库
- session入库
- session入库
- session入库
- session入库
- session入库
- session入库
- session入库
- session入库
- session入库
- session入库
- session入库
- STRUTS系列之 Action配置
- poj 3468 线段树 lazy标记模板
- nyoj711&&zznu1624 最舒适的路线(第六届河南省程序设计大赛 广搜)
- PHP7和HHVM的性能之争
- 一种局部ui界面切换解决方案
- session 入库
- java数据结构与算法分析学习2(泛型)
- Extjs替换自带theme
- 泛型在Web中的作用
- springMVC–参数绑定常用的注解
- 在Linux下安装配置JDK
- Android cache和files的区别
- Python 执行shell命令:system
- 安卓开发——安卓5.0以上版本如何去除Button自带阴影效果