0

I boiled my script down as simple as possible to illustrate the trouble I described above.

Here is testPage1.php

<?php
    session_start();
    $_SESSION['loggedIn'] = true;
?>
<form action="testPage2.php" method="post">
    <input type="submit" value="See vars" />
</form>

And here is testPage2.php

<?php 
    echo 'session variables:<br />';
    Print_r ($_SESSION); 
?>

Expected output in the browser for testPage2 would be:
session variables:
Array ( [loggedIn] => 1 )


Instead, i just get
session variables:

Checking /var/log/apache2/error.log reveals
[Mon Jul 08 22:00:25 2013] [error] [client 155.70.23.19] PHP Notice: Undefined variable: _SESSION in /var/www/euler/testPage2.php on line 3, referer: http://myURL.net/euler/testPage1.php

Any Ideas?

Rubens
  • 14,478
  • 11
  • 63
  • 92
travish82
  • 69
  • 1
  • 1
  • 5
  • 3
    put session_start() on every page – Markus Hofmann Jul 08 '13 at 22:10
  • 1
    `session_start()` on testPage2.php – SSH This Jul 08 '13 at 22:10
  • 1
    page2 is missing session_start() –  Jul 08 '13 at 22:10
  • Okay, I see that adding that to the page will remedy the situation. Now, I already have a head.php page like this: `` any idea why using "`include head.php;`" at the top of testPage2.php doesn't remedy the situation? I'd like to not have to add session_start to every page since I've already included that header page. – travish82 Jul 08 '13 at 22:25

2 Answers2

3

testPage2.php is missing session_start(); add

session_start();

on the top of testPage2.php

Fallen
  • 4,435
  • 2
  • 26
  • 46
  • Okay, I see that adding that to the page will remedy the situation. Now, I already have a head.php page like this: `` any idea why using "`include head.php;`" at the top of testPage2.php doesn't remedy the situation? I'd like to not have to add session_start to every page since I've already included that header page. – travish82 Jul 08 '13 at 22:22
  • are you literally using `include head.php;` or `include('head.php'); ` ? – Fallen Jul 08 '13 at 22:28
  • literally using `include 'head.php';` – travish82 Jul 09 '13 at 13:40
2

From the PHP Manual:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

So include session_start() at the very top in every php script you will use sessions in.

Markus Hofmann
  • 3,427
  • 4
  • 21
  • 31