drrhr20884 2016-06-09 04:37
浏览 89
已采纳

PHP会话在页面更改时没有保留值

I know there are quite a few posts about this topic but none of them have fixed my issue.

I have WAMP server running on my windows computer. I've created a login system that is able to set PHP sessions and verify that they are working while on the page that they were created. Once I change to a different page, either by typing in the URL or a javascript function I lose the session.

Here is my test.php that logs a user in

  require_once('config.php');

  $user = new User();
  $result = $user->login('email@gmail.com', 'mysecretpassword');
  echo $result;
  echo '</br>';
  $result = $user->isLoggedIn();
  echo $result;

This is the function that actually logs the user in

public function login($email, $password) {

  // Hash Password
  $password = $this->hashPassword($password);

  // Check if email and password match
  $query = "SELECT id, confirm_email FROM users WHERE email = ? AND password = ?";

  $a_bind_params = array($email, $password);
  $a_param_types = array('s','s');
  $results = $this->db->select($query, $a_bind_params, $a_param_types);

  // If we didnt get a result then email/password must be wrong
  if(count($results) == 0) return 1;

  // Now check that they verrified their email
  if($results[0]['confirm_email'] == 'N') return 2;

  // User is real and everything is good

  // Update login Date
  $a_bind_params = array(date('Y-m-d H:i:s'), $results[0]['id']);
  $a_param_types = array('s','s');
  $query = "UPDATE users SET login_at = ? WHERE id = ?";

  // There was a problem updating their login table so just fail the login
  if(!$this->db->update($query, $a_bind_params, $a_param_types)) return 3;

  // Login user
  Session::set("user_id", $results[0]['id']);
  session_regenerate_id(true);
  Session::set("login_fingerprint", $this->_generateLoginString ());

  return 0;
}

Here is the function that checks if the user is logged in

  // Checks if user is logged in
  public function isLoggedIn() {
      //if $_SESSION['user_id'] is not set return false
      if(Session::get("user_id") == null)
           return false;

      $loginString  = $this->_generateLoginString();
      $currentString = Session::get("login_fingerprint");
      if($currentString != null && $currentString == $loginString)
          return true;
      else  {
         //destroy session, it is probably stolen by someone
         $this->logout();
         return false;
      }
  }

Here is the Session function that creates a session

  public static function startSession() {
      ini_set('session.use_only_cookies', true);

      $cookieParams = session_get_cookie_params();
      session_set_cookie_params(
          $cookieParams["lifetime"],
          $cookieParams["path"],
          $cookieParams["domain"],
          SESSION_SECURE,
          SESSION_HTTP_ONLY
       );

      session_start();
      session_regenerate_id(true);
  }

These are the function that set/get the user session

  public static function set($key, $value) {
      $_SESSION[$key] = $value;
  }

  public static function get($key, $default = null) {
      if(isset($_SESSION[$key]))
          return $_SESSION[$key];
      else
          return $default;
  }

Finally this is my config.php file that actually calls session_start() and include some constants

// REQUIRE ALL FILES
require_once("ClassSession.php");
require_once("ClassDatabase.php");
require_once("ClassUser.php");
Session::startSession();

If I navigate to another page called test.php

include "config.php";
$user = new User();
if($user->isLoggedIn()) echo 'logged in';
else 'Not logged in';

The session is lost and the user is not logged in anymore.

I've checked my sessions.save_path in PHP.ini and have checked the wamp64/temp folder and my sessions are being stored in there. I am also calling session_start() on every page because I am including config.php on both the test pages. Not sure why I am losing my sessions.

EDIT

I forgot to mention what is actually happening. When I login the user I look up their user_id from the database and store that into $_SESSION['user_id']. When I access $_SESSION[user_id] from the page that logged the user in I get back the correct value. However, when I change to another page $_SESSION['user_id'] is null.

UPDATE

session_regenerate_id(true) is not the problem.

Here are my Session settings when printing php_info() enter image description here

  • 写回答

4条回答 默认 最新

  • douhuanglou1445 2016-06-11 16:07
    关注
    session_set_cookie_params(
          $cookieParams["lifetime"],
          $cookieParams["path"],
          $cookieParams["domain"],
          SESSION_SECURE,
          SESSION_HTTP_ONLY
       );
    

    Check values here. What is SESSION_SECURE const value? Maybe you are restricting cookie usage to https only and your site is on http.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系