我写了一个全新的脚本来将我的会话存储在数据库中,一切似乎都在起作用。出于某种原因,每当我打开页面时,会话变量就会消失。
我假设这与我使用session_start();当我使用session_start($ session_id)时,在页面顶部;在我的脚本上启动会话。这会重置session_id吗?
这是设置变量/会话
的主要php脚本<?php
session_start();
include($_SERVER['DOCUMENT_ROOT']. '/../db_connect.php');
if (!Isset($_SESSION['crecketgaming_usergroup'])){
$_SESSION['crecketgaming_usergroup'] = "Guest";
}
try {
$conn = new PDO("mysql:host=$servername:3307;dbname=$dbname", $username, $password);
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
onLoad($conn);
function onLoad($conn){
$session_id = "";
if(isset($_COOKIE['Crecketgaming_sessionid'])){
$time = time();
$session_id = $_COOKIE['Crecketgaming_sessionid'];
$addrip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM sessions WHERE session_id = :sessionid AND ip = :addrip";
$sth = $conn->prepare($sql);
$sth->bindParam(':sessionid', $session_id, PDO::PARAM_STR);
$sth->bindParam(':addrip', $addrip, PDO::PARAM_STR);
if($sth->execute()){
}else{
echo "error";
}
$rowcount = $sth->rowCount();
$row = $sth->fetch();
$userid = $row['user_id'];
//echo $row['user_id'];
if ($rowcount > 0) {
} else {
$session_id = storeUID($conn);
}
} else {
$session_id = storeUID($conn);
}
if($session_id !== "") {
if($userid === NULL){
}else{
//echo $userid;
setSessionDetails($conn, $userid);
}
}
}
function storeUID($conn){
$addrip = $_SERVER['REMOTE_ADDR'];
$session_id = createUID(100);
$time = time();
if(setcookie("Crecketgaming_sessionid", $session_id, mktime(). time()+60*60*24*30)){
$agent = $_SERVER['HTTP_USER_AGENT'];
$ip = $ip = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO sessions (session_id, ip, time) VALUES (:sessionid, :addrip, :time)";
$sth = $conn->prepare($sql);
$sth->bindParam(':sessionid', $session_id, PDO::PARAM_STR);
$sth->bindParam(':addrip', $addrip, PDO::PARAM_STR);
$sth->bindParam(':time', $time, PDO::PARAM_INT);
$sth->execute();
}
return $session_id;
session_start($session_id);
}
function createUID($length) {
$chars = "abcdefghijkmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i < $length) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
function setSessionDetails($conn, $userid) {
session_start($session_id);
session_id($session_id);
$sql = "SELECT * FROM users WHERE user_ID = :user_ID";
$sth = $conn->prepare($sql);
$sth->bindParam(':user_ID', $userid);
$sth->execute();
$row = $sth->fetch();
$_SESSION['crecketgaming_usergroup'] = $row['Usergroup'];
$_SESSION['crecketgaming_username'] = $row['Username'];
$_SESSION['crecketgaming_userid'] = $row['user_ID'];
}
?>
例如,在我的索引页面的顶部,这是我唯一的PHP代码:
<?php
session_start();
include($_SERVER['DOCUMENT_ROOT']. '/../db_connect.php');
include($_SERVER['DOCUMENT_ROOT']. '/includes/page_load.php');
include($_SERVER['DOCUMENT_ROOT']. '/includes/check_session.php');
?>
这在页面顶部调用。 db_connect包含我对服务器的连接详细信息,page_load计算ip地址刷新页面的次数,check_session是上面显示的文件。
我检查过,如果我把
echo $_SESSION['crecketgaming_userid']; //outputs "1"
echo $_SESSION['crecketgaming_usename ']; //outputs "crecket"
exit;
在check_session.php的底部,它显示了id 1。但是在索引页面或任何页面上,它们都是空的,好像没有人登录一样。
所以我的问题是session_start();像我一样删除以前的会话。我读到它启动或恢复会话但我从未见过有关会话ID的任何内容。
我对php有点新鲜,所以如果有人在任何地方看到改进的地方请告诉我。
答案 0 :(得分:0)
尝试更改:
$_COOKIE
到
$_SESSION
答案 1 :(得分:0)
我在启动之后设置了session_id()。我所要做的只是交换这两个:
session_id($session_id);
session_start();