I am learning to use PHP sessions, when a problem occured.
I was using some blank pages to test the PHP sessions' functions, and I had started a session on those pages with a specific id (something simple).
In the parent directory, there was my project for school, with DID NOT had the sessions implemented yet. Only some basic PHP to write/read a .txt file.
When I opened up my project page, I noticed the CSS was gone. Weird, I thought, because I had not modified the project's files since the day before. I opened up the inspector in Chrome, and had noticed that in my <style>
href attribute, there was not style.css
but rather style.css; PHPSESSID
which was weird, because I hadn't started ANY session on ANY pages this page was linked to.
It also overrode my CSS cookie I had created with JavaScript (just something like css_page='style.css'
). And it also borked my CSS on all pages of the project (the CSS styles did not load).
My question is, why does a PHPSESSID variable end up in a place that was not between <?php
and ?>
, when the session_start() function was not called in the page ? And then, how can I prevent it from overriding ALL cookies on the site ?
Edit : As a request from Martin, here is some code :
Page where I started a session just to do some testing :
<?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>
Page where I noticed the 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>
fonctions.js file :
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]);
}
Note : in another test php page, I used the following functions (in that order) :
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
I wanted to close/destroy the session completely, because I need at least two sessions and a 'guest' mode (no session turned on).