簡體   English   中英

PHP會話使用PHPSESSID覆蓋所有cookie和CSS,而無需調用session_start()

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

發生問題時,我正在學習使用PHP會話。

我當時使用一些空白頁來測試PHP會話的功能,並且我已經在具有特定ID(很簡單)的那些頁面上開始了會話。

在上級目錄中,有一個我要上學的項目,但DID尚未實施會議。 只有一些基本的PHP可以編寫/讀取.txt文件。

當我打開項目頁面時,我發現CSS消失了。 我覺得很奇怪,因為自從前一天以來我還沒有修改過項目的文件。 我在Chrome中打開了檢查器,並注意到在我的<style> href屬性中,沒有style.css ,而是style.css; PHPSESSID style.css; PHPSESSID很奇怪,因為我尚未在此頁面鏈接到的任何頁面上啟動任何會話。

它還覆蓋了我使用JavaScript創建的CSS cookie(就像css_page='style.css'類的東西)。 而且它還在項目的所有頁面上讓我的CSS感到厭煩(CSS樣式未加載)。

我的問題是,當未在頁面中調用session_start()函數時,為什么PHPSESSID變量為什么會出現在<?php?>之間的位置? 然后,如何防止它覆蓋網站上的所有 cookie?

編輯 :根據馬丁的要求,這是一些代碼:

我在這里開始一個會話的頁面只是為了進行一些測試:

<?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>

我注意到乏味的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>

fonctions.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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM