我想从我的数据库中的表中获取一个特定的行,即只有与登录用户相关的行。我正在使用以下脚本:
<?php
include('./classes/DB.php');
include('./classes/Login.php');
$connect = mysqli_connect("localhost", "root", "", "gaming");
$playerid = Login::isLoggedIn();
$sql = "SELECT * FROM games";// WHERE player_id =" .$playerid;
$result = mysqli_query($connect, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
if ($row['player_id']==$playerid) {
$json_array[] = $row;
}
}
echo json_encode($json_array);
?>
Login.php:
<?php
class Login {
public static function isLoggedIn() {
if (isset($_COOKIE['CHEZA'])) {
if (DB::query('SELECT user_id FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['CHEZA'])))) {
$userid = DB::query('SELECT user_id FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['CHEZA'])))[0]['user_id'];
if (isset($_COOKIE['CHEZACHEZA'])) {
return $userid;
} else {
$cstrong = True;
$token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
DB::query('INSERT INTO login_tokens VALUES (\'\', :token, :user_id)', array(':token'=>sha1($token), ':user_id'=>$userid));
DB::query('DELETE FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['CHEZA'])));
setcookie("CHEZA", $token, time() + 60 * 60 * 24 * 7, '/', NULL, NULL, TRUE);
setcookie("CHEZACHEZA", '1', time() + 60 * 60 * 24 * 3, '/', NULL, NULL, TRUE);
return $userid;
}
}
}
return false;
}
}
?>
我在浏览器中得到了所需的响应但是当我使用REST客户端时,我得到的是一个空数组[]。我做错了什么?
答案 0 :(得分:0)
$playerid = Login::isLoggedIn();
未设置,请检查。
修改强>
现在发布了isLoggedIn()
的代码:您确定您的REST客户端正在发送CHEZA cookie吗?我不认为在REST调用中使用cookie是个好主意。我发现这篇阅读很有用:https://softwareengineering.stackexchange.com/questions/141019/should-cookies-be-used-in-a-restful-api
答案 1 :(得分:0)
尝试修改您的PHP代码,我在运行您的查询和逻辑代码之前添加了 isset 检查:
print_r($playerid)
如果它不起作用,请尝试添加=IF(B2<>"",B2*C2,IF(D2<>"",D2*E2))
并检查您是否获得 loggedin 用户ID。