简体   繁体   English

如何获取已登录站点的用户的会话ID

[英]how do I get the session id of a user who has logged in to site

I'm in need of some help with 'getting' the user id of who is logged into my website. 我需要一些帮助来“获取”登录我网站的用户的ID。 I essentially have a users dashboard which I only want to show that particular users details. 我实质上有一个用户仪表板,我只想显示该特定用户的详细信息。 Could someone tell me if I'm on the right sort of path and what I've done wrong. 有人可以告诉我我走的路是否正确以及我做错了什么。

So in the dashboard page, which is where the users land after logging in, I have this at the top of the page: 因此,在仪表板页面上(用户登录后将在该页面上登录),我将其显示在页面顶部:

$query = "SELECT * FROM `checkPointMod` WHERE `idUsers` = $_SESSION['id']";

Then in my header which is on every page I have this: 然后在每页的标题中,我有以下内容:

<?php
    session_start();
    require "includes/dbh.inc.php";
?>

Then in my login script I have: 然后在我的登录脚本中,我有:

$_SESSION['id'] = $row['idUsers']; //User ID
$_SESSION['uid'] = $row['uidUsers']; //Username
$_SESSION['email'] = $row['emailUsers']; //Users Email

So I thought by calling the $_SESSION['id'] at the top of my dashboard page it would just pick up who is logged in however the error I'm getting is: 因此,我认为通过调用仪表板页面顶部的$_SESSION['id'] ,它只会获取登录的人,但是我得到的错误是:

syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) 语法错误,意外的''(T_ENCAPSED_AND_WHITESPACE),期望为'-'或标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)

Please can someone tell me if this is not the correct way of getting which user has logged in or if it's just a simple case of syntax? 请问有人告诉我这不是获取哪个用户登录的正确方法还是仅仅是语法的一种简单情况?

You can make a variable to store the id and pass it in the query to ignore this error like this 您可以创建一个变量来存储id并将其传递给查询,以忽略此错误,如下所示

$id =  $_SESSION['id'];
$query = "SELECT * FROM `checkPointMod` WHERE `idUsers` = $id";

Or you can do this 或者你可以这样做

$query = "SELECT * FROM `checkPointMod` WHERE `idUsers` = ".$_SESSION['id'].'"';

I think problem in your sql query. 我认为您的sql查询有问题。

When you use assosiative array havin key as string you should concate it with '.' 当您使用关联数组havin键作为字符串时,应将其与'。'连接。 operator . 操作员。 It can not be used in double quotes directly like simple variables. 不能像简单变量一样直接在双引号中使用。

You should use query like 您应该使用类似的查询


$uid = $_SESSION['id'];

$query = "SELECT * FROM `checkPointMod` WHERE `idUsers` = $uid";

Or 要么

$query = "SELECT * FROM `checkPointMod` WHERE `idUsers` = ".$_SESSION['id'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM