PHP会话使用PHPSSID覆盖任何cookie和CSS,而不调用session_start()


PHP session overrides any cookie and CSS with PHPSESSID, without session_start() called

当出现问题时,我正在学习使用PHP会话。

我使用了一些空白页面来测试PHP会话的功能,并在这些页面上用一个特定的id(一些简单的东西)启动了一个会话。

在家长目录中,有我的学校项目,DID NOT课程尚未实施。只有一些基本的PHP来编写/读取.txt文件。

当我打开我的项目页面时,我注意到CSS不见了。我觉得很奇怪,因为我从前一天起就没有修改过项目的文件。我在Chrome中打开了检查器,注意到在我的<style> href属性中,没有style.css,而是style.css; PHPSESSID,这很奇怪,因为我没有在该页面链接的任何页面上启动任何会话。

它还覆盖了我用JavaScript创建的CSS cookie(就像css_page='style.css'一样)。它还在项目的所有页面上调用了我的CSS(CSS样式没有加载)。

我的问题是,当页面中没有调用session_start()函数时,PHPSESSID变量为什么会出现在不在<?php?>之间的位置?然后,我如何防止它覆盖网站上的所有cookie?

编辑:根据Martin的请求,这里有一些代码:

我开始一个会话只是为了做一些测试的页面:

<?php
session_id("you");
session_start();
?>
<html>
<head>
   <title>Test</title>
</head>
<body>
  <?php
    echo "Bonjour<br/>";
    $_SESSION["pre"] = "firstName";
    $_SESSION["nom"] = "lastName";
    $_SESSION["idd"] = "identifiernumber";
    echo $_SESSION["pre"]."<br/>";
    echo $_SESSION["nom"]."<br/>";
    echo $_SESSION["idd"]."<br/>";
    print_r($_SESSION);
  ?>
</body>
</html>

我注意到borked CSS:的页面

<html>
<head>
  <meta charset="utf-8">
  <link id="css" rel="stylesheet" type="text/css" href="style.css">
  <script src="fonctions.js"></script>
</head>
<body onload="createcookie()">
  <!-- Some text in divs, nothing in php -->
</body>
</html>

foctions.js文件:

function changeCSS(){
    var sheet = document.getElementById('css').getAttribute('href');
    if (sheet == "style.css") {document.getElementById('css').setAttribute('href', 'style2.css');}
    else {document.getElementById('css').setAttribute('href', 'style.css');}
}
function chooseCSS(style) {
    document.getElementById('css').setAttribute('href', style);
}
function checkCookie(coo) {
    var cuki = document.cookie;
    var ind = cuki.indexOf(coo);
    if (ind != -1) {return true;}
    else {return false;}
}
function createcookie() {
    if (!checkCookie('css')) {
        foo = document.getElementById('css').getAttribute('href');
        if (foo == "") {foo = "style.css"};
        document.cookie = "css=" + foo;
    }
    var x = document.cookie.split("=");
    chooseCSS(x[1]);
}

注意:在另一个测试php页面中,我使用了以下函数(按顺序):

<?php
  session_start();
  $_SESSION = array();
  session_destroy();
?>

我想完全关闭/销毁会话,因为我至少需要两个会话和一个"来宾"模式(没有打开会话)。

关于PHP会话的一些一般注意事项:

  • 不要自己设置会话id值,但允许PHP自动生成它们。这更安全,减少了会话劫持的机会。

  • 会话不应该在URL行中使用,而应该在后台传输,而不是让最终用户反感(因此更容易操作)。

  • 在php.ini文件中或使用页面顶部的ini_set(通常带有include)设置会话参数。

php.ini示例:

session.savepath=/home/directory/custom_session_folder
session.name=a_custom_session_name
session.cookie_httponly=1 /* So not passed in URL */
session.cookie_secure=0 /* set to 1 for HTTPS cookies */
session.entropy_file=/dev/urandom /* better more secure session id generation */
session.hash_function=whirlpool /* same, better way of generating session hashes */
session.use_trans_sid=0 /* turn off transferable id's */
  • 在页面顶部使用以下行打开PHP脚本的错误报告:

PHP页面示例(顶部):

  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  error_reporting(E_ALL);
  • 如果您想要不同用户的不同会话或网站上的不同活动,您需要设置不同的会话名称而不是会话id。

  • 这是一种更好的方法,为每个人都设置会话,无论是"登录"的人还是未登录的人,只需在会话数据中保存一个标志来定义哪个是哪个,这将避免在更发达的网站上出现许多潜在的一致性问题。

示例:

$_SESSION['loggedin'] = true / false;