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 protected]', '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.

session_start()call.session_start()on every page.Sessionclass, if you use usual php$_SESSIONanyway?