简体   繁体   English

PHP重定向后丢失会话数据

[英]PHP Losing Session Data After a Redirect

I am having an issue that it seems like a lot of people are having though have tried all the solutions provided and none seem to be working. 我遇到的一个问题是,尽管很多人都尝试了提供的所有解决方案,但似乎都没有任何效果。

I have my session starting on every page redirecting with a header(location: exact url) Calling exit(); 我的会话在每个页面上都以header(location: exact url)重定向开始,并调用exit(); after the redirect (I have tried session save and a few others). 重定向后(我尝试了会话保存和其他一些方法)。 Globals are not an issue (PHP 7) and files are in a sub domain (in development at the moment) exact link: 全局变量不是问题(PHP 7),文件位于子域中(当前正在开发中)确切的链接:

localhost:1080/basefolder/admin/file.php

And the file that starts the session is in folder 并且开始会话的文件在文件夹中

localhost:1080/basefolder/admin/php/file.php

login.php - localhost:1080/basefolder/admin/login.php login.php-本地主机:1080 / basefolder / admin / login.php

<?php
 include 'php/adminloginfunctions.php';

 if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if ($_POST['type'] == 'login'){
      $username = $_POST['loginusername']; 
      $password = $_POST['loginpassword'];     
    if (login($username, $password)) {
      header('Location: http://' . $_SERVER['SERVER_NAME'].':1080' . dirname($_SERVER['REQUEST_URI']) . '/home.php');
        exit();
    }

} else {
    logout();
}

}

?>

The session functions are in localhost:1080/basefolder/admin/php/adminloginfunctions.php: 会话函数位于localhost:1080 / basefolder / admin / php / adminloginfunctions.php中:

<?php
  include 'adminmySQLCon.php';
  sec_session_start();

  function sec_session_start() {
    $session_name = 'sec_session_id'; 
    $secure = true;
    $httponly = true;
     if (ini_set('session.use_only_cookies', 1) === FALSE) {
       header("Location: ../error.php?err=Could not initiate a safe session            (ini_set)");
        exit();
  }
      $cookieParams = session_get_cookie_params();
      session_set_cookie_params($cookieParams["lifetime"],
      $cookieParams["path"], 
      $cookieParams["domain"], 
      $secure,
      $httponly);
       session_name($session_name);
       session_start(); 
       session_regenerate_id(true); 
}


 function login($email, $password) {
    global $conn;
     if ($stmt = $conn->prepare("SELECT Occupant.idOccupant,           Occupant.Occ_Email, Occupant.Occ_Password, roles.RoleLevel
         FROM Occupant INNER JOIN userrolemapping ON Occupant.idOccupant = userrolemapping.URMUserId
          INNER JOIN roles on roles.idRoles = userrolemapping.URMRoleID
       WHERE Occ_Email = ?
        LIMIT 1")) {
       $stmt->bind_param('s', $email);
       $stmt->execute();
       $stmt->store_result();
       $stmt->bind_result($user_id, $username, $db_password, $userrole);
       $stmt->fetch();

       if ($stmt->num_rows == 1) {
          if (checkbrute($user_id) == true) {
            return false;
        } else {
            if (password_verify($password, $db_password)) {
                $user_browser = $_SERVER['HTTP_USER_AGENT'];
                $_SESSION['user_id'] = $user_id;
                $_SESSION['username'] = $username;
                $_SESSION['login_string'] = hash('sha512', 
                          $db_password . $user_browser);
                $_SESSION['userrole'] = $userrole;
                // Login successful.
                return true;
            } else {
                $now = time();
                $conn->query("INSERT INTO login_attempts(idOccupant, time)
                                VALUES ('$user_id', '$now')");

                return false;
            }
        }
    } else {

        return false;
    }
}
}

The redirect page home.php is located in localhost:1080/basefolder/admin/home.php: 重定向页面home.php位于localhost:1080 / basefolder / admin / home.php:

<?php
   include 'php/adminloginfunctions.php';
   echo $_SESSION['userrole'];
 ?>

Resulting in the error: 导致错误:

Notice: Undefined index: userrole in 'folder link' on line 3 注意:未定义的索引:第3行“文件夹链接”中的userrole

Running out of things to try, any help would be great. 用尽所有尝试的资源,任何帮助都将非常有用。

Setting secure to false in the "session_set_cookie_params" seems to have resolved the issue. 在“ session_set_cookie_params”中将secure设置为false似乎已经解决了该问题。

function sec_session_start() {
$session_name = 'sec_session_id'; 
$secure = FALSE;
$httponly = true;
 if (ini_set('session.use_only_cookies', 1) === FALSE) {
   header("Location: ../error.php?err=Could not initiate a safe session            (ini_set)");
    exit();
}
    $cookieParams = session_get_cookie_params();
  session_set_cookie_params($cookieParams["lifetime"],
  $cookieParams["path"], 
  $cookieParams["domain"], 
  $secure,
  $httponly);
   session_name($session_name);
   session_start(); 
   session_regenerate_id(true); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM