如何从会话中获取$ _SESSION ['id']

时间:2013-10-01 22:51:06

标签: php

您好我的数据库中有2个表用于课程,1个用于用户,每个表都有一个id列。

我正在尝试在他们之间建立关系,一个用户订阅一个课程。我试图将结果存储在名为subscription的第3个表中,该表包含课程ID列和用户ID列。

用户在通过登录后注册,该登录与新会话相关联。用户点击订阅链接

<a href='subscribe.php?id=".$row['id']."'>subscribe!</a>

它们被带到后端php页面,在那里插入数据库信息:

<?php 
session_start();

?>
   $userid = $_SESSION['userID'];    
   $cursoid = $_GET['id'];

    mysql_connect("localhost", "username", "password") or die(mysql_error()) ; 
    mysql_select_db("test") or die(mysql_error()) ; 


    mysql_query("INSERT INTO `subscriptions` 
                     (curso_id, user_id) 
                     VALUES ('$cursoid', '$userid ')")
                         or die(mysql_error()); 

此时我已经获得了课程的ID并将其插入其中,问题在于用户ID我没有得到任何东西。我如何获取当前登录用户的ID?

这是我的登录功能类的代码:

    public function userLogin() {
$success = false;
try{
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->execute();

$valid = $stmt->fetchColumn();

if( $valid ) {
$success = true;

}


$con = null;
return $success;
}catch (PDOException $e) {
echo $e->getMessage();
return $success;

$user = $stmt->fetchObj();

    if( $user->user_id > 0 ) {

        $success = true;

        // User has been successfully verified, lets sessionize his user id so we can refer to later
        $_SESSION['userID'] = $user->user_id;}

}
}

最后这里是登录功能的代码:

session_start();
$usr = new Users;
$usr->storeFormValues( $_POST );
if( $usr->userLogin() ) {
    header( 'Location: cursos.php' ) ;
    $_SESSION["loggedIn"] = true;
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    $_SESSION['id'] = $_POST['id'];

3 个答案:

答案 0 :(得分:1)

每次要使用会话时,都必须在文件开头调用session_start函数。您在登录功能中调用它,但不要在subscribe.php中调用它。试试这个:

session_start();

$userid = $_SESSION['id'];
$cursoid = $_GET['id'];
//rest of the code

答案 1 :(得分:1)

您不应该将sessionId用作userIds,而应该在插入用户行后使用用户表的主键。此外,可能是迂腐,但你应该将你的用户变量重命名为$user$usr让我畏缩。

获取会话ID的另一种方法是:session_id

- 编辑 -

public function userLogin() {

    ....

    $user = $stmt->fetchObj();

    if( $user->user_id > 0 ) {

        $success = true;

        // User has been successfully verified, lets sessionize his user id so we can refer to later
        $_SESSION['userId'] = $user->user_id;
    }
}

// We sessionized user id after validation, so we now have access to it
$userid = $_SESSION['userId'];

// Using straight mysql api is frowned upon, this should be converted to PDO before production use
mysql_query("INSERT INTO `subscriptions` (curso_id, user_id) VALUES ('$cursoid', '$userid ')")
    or die(mysql_error()); 

答案 2 :(得分:0)

此外,您还有一个小错误,此行VALUES ('$cursoid', '$userid ')")

中有空格
mysql_query("INSERT INTO `subscriptions` 
             (curso_id, user_id) 
             VALUES ('$cursoid', '$userid')")
                 or die(mysql_error());