当出现问题时,我正在学习使用 PHP session 。
我正在使用一些空白页面来测试 PHP session 的功能,并且我已经在那些具有特定 ID(简单的东西)的页面上启动了一个 session 。
在父目录中,有我的学校元素,没有已经实现了 session 。只有一些基本的 PHP 才能写入/读取 .txt 文件。
当我打开我的元素页面时,我注意到 CSS 不见了。奇怪,我想,因为自从前一天以来我就没有修改过元素的文件。我在 Chrome 中打开了检查器,并注意到在我的 <style>
中href 属性,没有 style.css
而是style.css; PHPSESSID
这很奇怪,因为我没有在该页面链接到的任何页面上启动任何 session 。
它还覆盖了我用 JavaScript 创建的 CSS cookie(就像 css_page='style.css'
一样)。它还使我在元素的所有页面上的 CSS 变得乏味(CSS 样式没有加载)。
我的问题是,为什么 PHPSESSID 变量最终出现在不在 <?php
之间的位置?和 ?>
, 当页面中没有调用 session_start() 函数时?然后,如何防止它覆盖网站上的所有 cookie?
编辑:应 Martin 的要求,这里有一些代码:
我开始 session 只是为了做一些测试的页面:
<?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();
?>
我想完全关闭/销毁 session ,因为我需要至少两个 session 和一个“访客”模式(没有打开 session )。
最佳答案
关于 PHP session 的一些一般说明:
不要自己设置 session ID 值,但允许 PHP 自动生成它们。这更加安全并且减少了 Session Hyjacking 的机会。 .
session 不应在 URL 行中使用,而应更多地在后台传输,而不是让最终用户显而易见(因此更容易操作)。
在 php.ini 文件中或使用页面顶部的
ini_set
设置 session 参数(通常使用 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);
如果您希望网站上的不同用户或不同事件有不同的 session ,您需要设置不同的 session names 不是 session ID。
为每个人(“已登录”和未登录)设置 session 是一种更好的方法,只需在您的 session 数据中保存一个标志来定义哪个是哪个,这将回避许多潜在的一致性一个更发达的网站上的问题。
示例:
$_SESSION['loggedin'] = true / false;
关于PHP session 用 PHPSESSID 覆盖任何 cookie 和 CSS,而不调用 session_start(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36960093/