PHP Session是空的

时间:2016-06-14 12:52:03

标签: php session post

我的脚本如何运作:

  1. index.php获取文件将其发送到jscript进行分析,然后发送到upload.php,在那里进行会话。
  2. 但会话无效。 如果我去upload.php并打印变量,它会显示我的usernumber 2(有效)

    require_once("user/upload_session.php");
    if (!isset($_SESSION)) { session_start();}
    

    登录以创建会话:

    public function doLogin($uname,$umail,$upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT id, user_name, email, pass FROM accounts WHERE user_name=:uname OR email=:umail ");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['pass']))
                {
                    $_SESSION['user_session'] = $userRow['id'];
                      $thatsmysession = $_SESSION['user_session']; 
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }
    

    我正在使用这个:

    if (empty($thatsmysession)) {
        // Query if user is NOT logged in
        $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' .
                          'expire, delid, ip) VALUES (:hash, :orig, :name, :size, :date, ' .
                          ':exp, :del, :ip)');
    } else {
    
        // Query if user is logged in (insert user id together with other data)
        $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' .
                          'expire, delid, user, ip) VALUES (:hash, :orig, :name, :size, ' .
                          ':date, :expires, :delid, :user, :ip)');
        $q->bindValue(':user', $thatsmysession, PDO::PARAM_INT);
    }
    

    $q->bindValue(':user', $thatsmysession, PDO::PARAM_INT);会话为null,但如果我回显测试变量是否正确则可以正常工作

    如果您想查看请求:https://temp.venipa.net 在TAB网络上传一些视图,文件upload.php

    抱歉英语不好:/

3 个答案:

答案 0 :(得分:1)

您对现有会话的检查是错误的。试试这个帖子中提到的: Check if PHP session has already started

答案 1 :(得分:0)

像这样检查会议

if(session_id() == ''){ session_start();}

而不是这个

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

答案 2 :(得分:0)

您可以先检查您的会话是否存在,以及$ _SESSION ['user_session']并且它不是空的。

if(session_id() == ''){ session_start();}
// check if you user_session exists or not, doLogin
if(!isset($_SESSION['user_session'])){ doLogin(...); }