PHP $ _SESSION变量使用Slim未定义

时间:2016-02-17 21:54:50

标签: php session

我尝试使用Slim在php中设置会话变量。我希望将用户ID存储为变量以在其他地方使用。我认为我的函数中的语法或顺序错误。

这是我设置变量的函数:

function loadAdmin()
{
    //Set new session and save user id to variable
    if (!isset($_SESSION)) {
        session_start();
    }

    $app          = \Slim\Slim::getInstance();
    $token        = $app->request->headers->get('token');
    $token_exists = getToken_Validate();

    if($token_exists) {
        $sql = "SELECT id, title AS admin_title, last_name AS admin_last_name
                FROM admin WHERE token=:token";
        try {
            $db   = getDB();
            $stmt = $db->prepare($sql);

            $stmt->bindValue(':token', $token);
            $stmt->execute();

            $admin = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db    = null;

            echo json_encode($admin); 

            $_SESSION['uid'] = $stmt['id'];          
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
        }
    } else {
        echo '{"err": "failed"}';
    }
}

这是我在获取特定数据时使用变量的功能:

function loadDashboard() 
{
    session_start();    
    $uid = $_SESSION['uid'];

    $token_exists = getToken_Validate();

    if ($token_exists) {
        //Get number of rows from multiple tables
        $sql = "SELECT 
            (SELECT COUNT(*) FROM users WHERE id=:uid) AS total_students,
            (SELECT COUNT(*) FROM subjects) AS total_subjects,
            (SELECT COUNT(*) FROM notes) AS total_notes";

        try {
            $db = getDB();
            $stmt = $db->prepare($sql);
            $stmt->bindValue(':uid', $uid);
            $stmt->execute();
            $users = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db = null;

            echo json_encode($users);
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
        }
    } else {
        echo '{"err": "failed"}';
    }
}

尝试loadDashboard后得到的Slim错误是:

  

未定义索引:uid

很抱歉,如果我的PHP很糟糕,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

原来它与Slim无关。

$ _ SESSION [' uid'] = $ stmt [' id'];没有将任何内容存储到变量中。

我必须先将id列绑定到变量:

$stmt->bindColumn('id', $uid);

然后我可以将该变量设置为会话变量:

$_SESSION['uid'] = $uid;

以下是完整的工作职能:

function loadAdmin() 
{
    if (!isset($_SESSION)) {
        session_start();
    }

    $app            = \Slim\Slim::getInstance();
    $token          = $app->request->headers->get('token');
    $token_exists   = getToken_Validate();

    if($token_exists) {
        $sql = "SELECT 
        id,
        title AS admin_title, 
        last_name AS admin_last_name 
        FROM admin WHERE token=:token";
        try {
            $db     = getDB();
            $stmt   = $db->prepare($sql);

            $stmt->bindValue(':token', $token);
            $stmt->execute();

            $stmt->bindColumn('id', $uid);

            $admin  = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db     = null;

            $_SESSION['uid'] = $uid;            

            echo json_encode($admin);
        }

        catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
        }

    }

    else {
      echo '{"err": "failed"}';
    }
}