简体   繁体   English

PHP session_start 不起作用

[英]PHP session_start doesn't work

My issue is that, when I use session_start();我的问题是,当我使用 session_start(); in my php code, instead of a PHPSESSID cookie being set, a cookie with blank title and value "HttpOnly" is set instead.在我的 php 代码中,没有设置 PHPSESSID cookie,而是设置了一个具有空白标题和值“HttpOnly”的 cookie。 Using var_dump($_SESSION), I see that I can set session variables and they'll display on the page, but they won't display on any other page.使用 var_dump($_SESSION),我看到我可以设置会话变量,它们会显示在页面上,但不会显示在任何其他页面上。 For what it's worth, the two pages are at: login.domain.com/index.php and login.domain.com/login.就其价值而言,这两个页面位于:login.domain.com/index.php 和 login.domain.com/login。 The same code works fine locally, and other php files running on different subdomains on the same server work.相同的代码在本地运行良好,运行在同一服务器不同子域上的其他 php 文件也可以正常运行。 I can't find any info, so if anyone has any ideas, I'd love to hear them.我找不到任何信息,所以如果有人有任何想法,我很想听听。

This is the php on index.php:这是 index.php 上的 php:

    <?php
        session_start();
    ?>

And this is the php on login/login.php这是 login/login.php 上的 php

<?php
session_start();
$role = 0; //default to "guest"
$was_success = false; //default to a failed login
if(isset($_POST["user"]) && isset($_POST["password"])){ //if the post details are set then continue
    $pass = password_hash("PASSWORD", PASSWORD_DEFAULT);

    if (!isset($_COOKIE["mellifluous_loginRefer"])){
            $arr = array("Username" => $_POST["user"],
            "Error" => "No destination set!",
            "Success" => false
            );
            die(json_encode($arr));
    }

    if (password_verify($_POST["password"], $pass) && ($_POST["user"] == "USER")){
       $was_success = true;
       if ($_COOKIE['mellifluous_loginRefer'] == "home"){
          $_SESSION['mellifluous']['home']['username'] = $_POST['user'];
       }
    }
    else $was_success = false;
    $arr = array("Username" => $_POST["user"],
                 "Role" => $role,
                 "Success" => $was_success
    );
    if ($was_success) setcookie("mellifluous_loginRefer", "", time() - 10, "/");
    echo(json_encode($arr));
    //echo "You sent in: ";//Username: " . $_POST["user"] . " Password: ";//. $password;
}
else if(isset($_GET["user"]) && isset($_GET["password"])){
    die("This interface has been deprecated.");
    //$pass = password_hash($_POST["password"], PASSWORD_DEFAULT);
    $arr = array("Username" => $_GET["user"]);
    echo(json_encode($arr));
    //echo "You sent in: ";//Username: " . $_POST["user"] . " Password: ";//. $password;
}
else{
    die("ERROR!");
}
?>

Many thanks in advance!非常感谢提前!

Check assigned values to session.use_cookies , session.use_only_cookies on php.ini file in your server.检查服务器中 php.ini 文件中session.use_cookiessession.use_only_cookies分配值。

You need to set the value of session.use_cookies and session.use_only_cookies in php.ini:需要在php.ini中设置session.use_cookiessession.use_only_cookies的值:

session.use_cookies=1
session.use_only_cookies=0

I figured it out.我想通了。 I had some weird cookie settings in my apache2 conf file for that site that looked weird/out of place:我的 apache2 conf 文件中有一些奇怪的 cookie 设置,用于该站点,看起来很奇怪/不合适:

     Header set Set-Cookie HttpOnly;Secure
     Header always edit Set-Cookie (.*) "$1; HTTPOnly"
     Header always edit Set-Cookie (.*) "$1; Secure"

Once I removed those lines, things worked fine.一旦我删除了这些行,事情就正常了。

I can't answer to the two previous answer, but actually you shouldn't do that.我无法回答前两个答案,但实际上您不应该这样做。

session.use_only_cookies is as security feature, it prevents from attacks using session identifiant inside URL. session.use_only_cookies作为安全功能,它可以防止使用 URL 内的会话标识符进行攻击。 see php docs for more information.有关更多信息,请参阅 php 文档。

 Header set Set-Cookie HttpOnly;Secure
 Header always edit Set-Cookie (.*) "$1; HTTPOnly"
 Header always edit Set-Cookie (.*) "$1; Secure"

About this part, HTTPOnly is required, it prevents cookie to be stolen by an XSS attacks.In fact with Javascript, by default it's possible to show PHPSESSID value.关于这部分, HTTPOnly是必需的,它可以防止 cookie 被 XSS 攻击窃取。实际上在 Javascript 中,默认情况下可以显示 PHPSESSID 值。 this feature do not provide the value to the source request if is not HTTP.如果不是 HTTP,则此功能不向源请求提供值。 Not sure to be clear, see OWASP recommandation for more informations.不确定是否清楚,请参阅 OWASP 推荐以获取更多信息。

Concerning Secure attribute,, the user agent will include the cookie in an HTTP request only if the request is transmitted through a secure channel (HTTPS).关于Secure属性,只有当请求通过安全通道 (HTTPS) 传输时,用户代理才会在 HTTP 请求中包含 cookie。 see OWASP recommandation for this feature请参阅此功能的 OWASP 推荐

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

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