PHP session_start不工作 [英] PHP session_start doesn't work

查看:112
本文介绍了PHP session_start不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我使用session_start();在我的PHP代码中,而不是设置的PHPSESSID cookie,设置空白标题和值HttpOnly的cookie。使用var_dump($ _ SESSION),我看到我可以设置会话变量,他们将显示在页面上,但它们不会显示在任何其他页面上。对于值得,这两个页面是:login.domain.com/index.php和login.domain.com/login。相同的代码在本地运行良好,并且在同一服务器上的不同子域上运行的其他PHP文件工作。我找不到任何信息,所以如果任何人有任何想法,我很乐意听到他们。



这是php上的index.php:

 <?php 
session_start();
?>

这是login / login.php上的php

 <?php 
session_start();
$ role = 0; //默认为guest
$ was_success = false; //默认为失败的登录
if(isset($ _ POST [user])&& ampetet($ _ POST [password])){//如果设置了帖子详细信息, b $ b $ pass = password_hash(PASSWORD,PASSWORD_DEFAULT);

if(!isset($ _ COOKIE [mellifluous_loginRefer])){
$ arr = array(Username=> $ _POST [user],
Error=>无目标设置!,
成功=> 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));
// echoYou sent in:; //用户名:。$ _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 );
// echoYou sent in:; //用户名:。 $ _POST [user]。 Password:; //。 $ password;
}
else {
die(ERROR!);
}
?>非常感谢!



$ b解决方案

我想出来了。我在我的apache2 conf文件中有一些奇怪的cookie设置,该网站看起来很奇怪/不合适:

  Cookie HttpOnly; Secure 
头文件总是编辑Set-Cookie(。*)$ 1; HTTPOnly
头文件总是编辑Set-Cookie(。*)$ 1; Secure

一旦我删除了这些行,事情就很好了。


My issue is that, when I use session_start(); in my php code, instead of a PHPSESSID cookie being set, a cookie with blank title and value "HttpOnly" is set instead. 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. For what it's worth, the two pages are at: login.domain.com/index.php and login.domain.com/login. The same code works fine locally, and other php files running on different subdomains on the same server work. 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:

    <?php
        session_start();
    ?>

And this is the php on login/login.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!

解决方案

I figured it out. I had some weird cookie settings in my apache2 conf file for that site that looked weird/out of place:

     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.

这篇关于PHP session_start不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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