0

I have 2 pages. page1 initiates a session variable and has a link towards page2. Both page include the same header, which has the session_start.

Yet I can't manage to understand why the session array isn't going through the second page.

header.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
    <body>
    <?php print_r($_SESSION); ?>

page1.php shows correctly the $_SESSION content as seen below

<?php
    include('header.php');
    $_SESSION['usager'] = "popo";
?>
    <a href="page2.php"> Page 2 </a>
    </body>
</html>

Page 1

page2.php has an empty array as $_SESSION, thus showing nothing

<?php
    include('header.php');
    echo "hi";
?>
    </body>
</html>

Page 2

What I have tried

  • Change the href from a relative to an absolute link. No difference.
  • Check PHPSESSID, it's there.
  • Some projects on the same server don't trigger that issue.
  • On Chrome, do shift + f5 to remove cache. No difference.

Pages I visited

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Maude
  • 512
  • 3
  • 8
  • 23
  • Just curious, how does `page1.php` show `Array [useager] => popo` when it runs `$_SESSION['usager'] = "popo";` _after_ the `print_r($_SESSION)`? (I just tried without the `` stuff and it seems to work for me using PHP's internal server) Which version of PHP are you using? Which server? – brombeer Dec 10 '19 at 00:52
  • At first glance looks ok to me. Anything in `phpinfo()` about sessions look weird? – Alan P. Dec 10 '19 at 01:00
  • Does error reporting show anything? P.s.: You'll have to ping one of us for direct responses. I / others might not be looking at the question or have kept the window/tab open. @Maude – Funk Forty Niner Dec 10 '19 at 02:44
  • @kerbholz Thanks for mentionning that. I hadn't noticed but indeed, the first time I run the page, the session array is empty. It's when I refresh the page that it shows. I am using 7.2.24 and it's on a school server so it's hard to know exactly what's happening. – Maude Dec 10 '19 at 15:04
  • @AlanP. I didn't know about this command! The session section looks fine, although the session.cookie_domain and session.cookie_httponly are both "no value". A little research online seems to indicate that this would not be linked to the problem though. – Maude Dec 10 '19 at 15:07
  • @FunkFortyNiner I added error_reporting(-1); on both page to show all possible errors, but nothing came up. Is it suppose to show on page or in some kind of file? Tried looking into various chrome dev tools tabs without luck – Maude Dec 10 '19 at 15:10
  • `error_reporting(E_ALL); ini_set('display_errors', 1);` < try adding that to the top and under your opening php tag(s). I also suspect outputting before header but I could be wrong. – Funk Forty Niner Dec 10 '19 at 15:11
  • @FunkFortyNiner Page 1 has no error. Page 2 has the following : Warning: session_start(): Cannot start session when headers already sent in /home/maude/project/includes/header.php on line 1 Notice: Undefined variable: _SESSION in /home/maude/project/includes/header.php on line 7 – Maude Dec 10 '19 at 16:41
  • Actually, it depends of the order I put my lines. If I put include('header.php') before error_reporting in page2.php I get no error at all – Maude Dec 10 '19 at 16:47
  • There you go. You need to place the php above the HTML, start the session and check if the session is set/not empty. I am pretty sure that if you fix the former first, you'll get the latter :) – Funk Forty Niner Dec 10 '19 at 16:52

1 Answers1

1

The file was encoded in UTF8-BOM!

I took the example available here and noticed it worked. I copied the w3schools code into my page1 and page2 and it wasn't working.

I ended up sending both file into notepad++ and noticed that the bugged project was encoded in UTF8-BOM, and the working one was in UTF8.

Reference to the explanation : PHP session with UTF-8-BOM encoding

Maude
  • 512
  • 3
  • 8
  • 23