我正在使用PDO和Sessions处理登录系统。我试图在以随机字符串形式成功登录到名为“session”的表之后,存储来自每个会话的数据。
这是我的config.php
文件:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$dbh = new PDO('mysql:host=localhost;dbname=rineva', 'root', '');
$stmt = $dbh->prepare("SELECT * FROM user;");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row)
{
echo $row['user_username'];
}
?>
我的functions.php
文件是:
<?php
class func
{
public static function checkLoginState($dbh)
{
if (!isset($_SESSION['id']) || !isset($_COOKIE['PHPSESSID']))
{
session_start();
}
if (isset($_COOKIE['id']) && isset($_COOKIE['token']) && isset($_COOKIE['serial']))
{
$query = "SELECT * FROM sessions WHERE sessions_userid = :user_id AND sessions_token =:token AND sessions_serial =:serial;";
$id = $_COOKIE['id'];
$token = $_COOKIE['token'];
$serial = $_COOKIE['serial'];
$stmt = $dbh->prepare($query);
$stmt->execute(array(':user_id' => $user_id, ':token' => $token, ':serial' => $serial));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row['sessions_userid'] > 0)
{
if (
$row['sessions_userid'] == $_COOKIE['user_id'] &&
$row['sessions_token'] == $_COOKIE['token'] &&
$row['sessions_serial'] == $_COOKIE['serial']
)
if (
$row['sessions_userid'] == $_SESSION['user_id'] &&
$row['sessions_token'] == $_SESSION['token'] &&
$row['sessions_serial'] == $_SESSION['serial']
)
{
return true;
}
}
}
}
public static function createRecord($dbh, $user_username, $user_id)
{
$query ="INSERT INTO sessions(sessions_userid, sessions_token, sessions_serial) VALUES (:user_id,:token,:serial) '25/05/2018'";
$dbh->prepare("DELETE FROM sessions WHERE sessions_userid = :sessions_userid';")->execute(array(''));
$token = func:: createString(20);
$serial = func:: createString(20);
func :: createCookie($user_username, $user_id, $token, $serial);
func :: createSession($user_username, $user_id, $token, $serial);
$stmt = $dbh->prepare($query);
$stmt->execute(array(':user_id' => $user_id, ':token' => $token, ':serial' => $serial));
}
public static function createCookie($user_username, $user_id, $token, $serial)
{
setcookie('user_id', $user_id, time() + (86400) * 30, "/");
setcookie('token', $token, time() + (86400) * 30, "/");
setcookie('serial', $serial, time() + (86400) * 30, "/");
setcookie('user_username', $user_username, time() + (86400) * 30, "/");
}
public static function createSession($user_username, $user_id, $token, $serial)
{
if (!isset($_SESSION['id']) || isset($_COOKIE['PHPSESSID']))
{
session_start();
}
$_SESSION['user_username'] = $user_username;
}
public static function createString($len)
{
$string ="1lksjrienj4hjtJHBNKq5SWndmrtjihush45bhe0qwsdcnNER56NWMLOIwsQ";
$s = '';
$r_new = '';
$r_old = '';
for ($i = 1; $i < $len; $i++)
{
while ($r_old ==$r_new)
{
$r_new = rand(0, 60);
}
$r_old = $r_new;
$s = $s.$string[$r_new];
}
return $s;
}
}
?>
请帮帮我。我附上了我的数据库结构的截图
答案 0 :(得分:0)
也许你忘了在插入mysql db后提交你的数据。先检查一下