使用XAMPP在本地主机上设置$ _SESSION无效 [英] Setting $_SESSION doesn't work on localhost using XAMPP

查看:211
本文介绍了使用XAMPP在本地主机上设置$ _SESSION无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的PHP代码中设置了会话:$_SESSION['user_id'] = $login;,这在上传到服务器并跨不同页面时似乎可以正常工作,但是当我在本地计算机上测试网站时,会话似乎变成了离开设置好的脚本后立即取消设置.

我正在使用XAMPP和完全相同的代码在本地计算机上进行测试.

我不确定为什么会发生此问题,将不胜感激任何有帮助的答案.

示例不起作用:

$_SESSION['user_id'] = $login;
echo '<META HTTP-EQUIV="refresh" content="0;URL=../home.php">';  

这是我登录(属于课程的一部分)的整个功能:

public function loggingIn(){
    session_start();

    $db = new Database('localhost','root','','userdata');
    $userFunctions = new Users($db);

    $username = $_POST['usernameInput'];
    $password = $_POST['passwordInput'];

    if(empty($username) || empty($password)){
        $this->errors['u&p'] = 'Please Enter Your Username AND Password';
        $this->printE();
    } elseif($userFunctions->user_exists($username)===false){
        $this->errors['nm'] = 'That Username/Password Combination Is Not Valid';
        $this->printE();
    } else {
            $login = $userFunctions->login($username, $password);

    if($login === false){
            $this->errors['nm'] = 'That Username/Password Combination Is Not Valid';
            echo 'Login Failed!';
        } else  {
           if(!$userFunctions->economyTableExistsForUser($login)){
               $userFunctions->createEconomyTableForUser($login);   
           }
           if(!$userFunctions->schoolTableExistsForUser($login)) {
               $userFunctions->createSchoolTableForUser($login);    
           }

               $_SESSION['user_id'] = $login;

               echo $_SESSION['user_id'];  // working fine

               header('Location: ../home.php');
        }
    }   
}

解决方案

让我们猜测您的家用计算机没有:

session.autostart = On

在您的 php.ini 中,而其他计算机显然是. /p>

确保已设置PHP代码:

session_start();

PHP:session_start-手动

如果不这样做,除非您的第一个猜想是真的,否则您的会话将不会开始.

此外,您还必须在本地主机上检查 PHP 版本以及 php.ini 中的设置.确保存储会话文件的目录是可写的,并且会话文件确实存在.

您最好使用PHP解决方案进行重定向:

header("Location: /");
exit();

PHP具有修改HTTP标头的功能.其中一些:

换行符和空格可能是一个问题,但是也存在一些可能导致Warning: Cannot modify header information的不可见字符序列,例如

要在Sublime Text中查看空白,请编辑设置:

// Set to "none" to turn off drawing white space, "selection" to draw only the
// white space within the selection, and "all" to draw all white space
"draw_white_space": "selection",

您可以在首选项"->默认设置"中进行设置.如果您编辑用户设置偏好设置"->设置"-用户",然后按如下所示添加行:

{
    "font_size": 10,
    "draw_white_space": "all"
}

还要确保它显示所有其他特殊字符以正确调试您的代码!

最后在重定向之前尝试添加session_write_close();.

session_start();session.save_path = "/home/username/tmp";指令之前在PHP文件中进行设置. 在 public_html 之外创建tmp目录.确保tmp目录具有 chmod 770 并使用相同的用户/组特权创建.在主目录中运行ls -lsa,以检查新目录是否具有与其他目录相同的 user/group ,例如 public_html .如果不是,请确保通过运行chown username:groupname /home/username/tmp以root用户身份在tmp上更改权限.

I set my session like so in my PHP code : $_SESSION['user_id'] = $login; and this seems to work fine whilst uploaded to my server and carries across different pages, however when I am testing the website on my local machine the session seems to become immediately unset after leaving the script where it is set.

I am testing on my local machine using XAMPP and exactly the same code.

I am not sure why this problem is occurring and would greatly appreciate any helpful answer.

Example that is not working:

$_SESSION['user_id'] = $login;
echo '<META HTTP-EQUIV="refresh" content="0;URL=../home.php">';  

EDIT 1:

Here is my entire function in which I log in (it's part of the class):

public function loggingIn(){
    session_start();

    $db = new Database('localhost','root','','userdata');
    $userFunctions = new Users($db);

    $username = $_POST['usernameInput'];
    $password = $_POST['passwordInput'];

    if(empty($username) || empty($password)){
        $this->errors['u&p'] = 'Please Enter Your Username AND Password';
        $this->printE();
    } elseif($userFunctions->user_exists($username)===false){
        $this->errors['nm'] = 'That Username/Password Combination Is Not Valid';
        $this->printE();
    } else {
            $login = $userFunctions->login($username, $password);

    if($login === false){
            $this->errors['nm'] = 'That Username/Password Combination Is Not Valid';
            echo 'Login Failed!';
        } else  {
           if(!$userFunctions->economyTableExistsForUser($login)){
               $userFunctions->createEconomyTableForUser($login);   
           }
           if(!$userFunctions->schoolTableExistsForUser($login)) {
               $userFunctions->createSchoolTableForUser($login);    
           }

               $_SESSION['user_id'] = $login;

               echo $_SESSION['user_id'];  // working fine

               header('Location: ../home.php');
        }
    }   
}

解决方案

Let's guess that your home machine is not having:

session.autostart = On

in your php.ini, while your other machine obviously is.

Make sure that you have set in your PHP code:

session_start();

PHP: session_start - Manual

If you don't, then your session is not started, unless my first conjecture is true.

Besides, you must check for you PHP version on your localhost and settings in php.ini. Make sure that the directory where you store you session files is writeable and session files do really exist.

EDIT 1:

You better use PHP solution for redirection:

header("Location: /");
exit();

EDIT 2:

PHP has functions that modify HTTP headers. Some of them:

EDIT 3:

Line-breaks and spaces could be a problem but there are also invisible character sequences which can cause Warning: Cannot modify header information, like the UTF-8 BOM (Byte-Order-Mark). Try re-saving your file making sure that it's saved just as UTF-8 (no BOM).

EDIT 4:

To properly debug your code make sure that PHP displays all warning and notices. Run it before session start:

ini_set('display_errors', -1);

EDIT 5:

You must also check session.gc_maxlifetime setting:

session.gc_maxlifetime
session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and cleaned up. Garbage collection occurs during session start.

EDIT 6:

To view white-spaces in Sublime Text, edit the settings:

// Set to "none" to turn off drawing white space, "selection" to draw only the
// white space within the selection, and "all" to draw all white space
"draw_white_space": "selection",

You can set it in Preferences->Settings Default. If you edit your user settings Preferences->Settings - User and add the line as per below:

{
    "font_size": 10,
    "draw_white_space": "all"
}

Also make sure it shows all other special characters to properly debug your code!

EDIT 7:

Finally try adding session_write_close(); right before redirecting.

EDIT 8:

Set in your PHP file, before session_start();, session.save_path = "/home/username/tmp"; directive. Create tmp dir outside of public_html. Make sure that tmp dir has chmod 770 and created with the same user/group privileges . Run ls -lsa in your home dir to check if the new directory has the same user/group as other directories, like public_html for instance. If not, make sure changing permissions as root on tmp by running chown username:groupname /home/username/tmp.

这篇关于使用XAMPP在本地主机上设置$ _SESSION无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆