0

I have the following code on one page

<?php
// Account for the possibility of time out
session_start();
$_SESSION['LoggedIn']=$_GET['LoggedIn'];
$_SESSION['SetName']=$_GET['SetName'];
    $setName=$_SESSION['SetName'];
$_SESSION['UserName']=$_GET['UserName'];
if (!isset($_SESSION['LoggedIn']) || !$_SESSION['LoggedIn']) header("Location: Home.php");
    php var_dump($_SESSION); 
    header("Location: uploadFiles.php"); // DEBUG
?>

If the header("Location: uploadFiles.php"); is commented out, this gives

array(3) { ["LoggedIn"]=> string(4) "TRUE" ["SetName"]=> string(4) "test" ["UserName"]=> string(5) "OtagoHarbour" } 

uploadFiles.php has the following code

<?php
// Account for the possibility of time out
session_start();
if (!isset($_SESSION['LoggedIn']) || !$_SESSION['LoggedIn']) {
    php var_dump($_SESSION);
    ?>
    <script type="text/javascript">
    alert("Not logged in.  Session log in=<?php echo $_SESSION['LoggedIn'] ?>");
    document.location.href="Home.php";
    </script>
    <?php
}
?>

I get the alert

Array(0) {}
Not logged in.  Session log in=
OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81
  • 3
    What's the URL by which you call the first page, ergo what's in `$_GET['LoggedIn']`? – Wiseguy Jan 28 '14 at 03:14
  • echo ""; – OtagoHarbour Jan 28 '14 at 03:33

2 Answers2

2

You won't see the session data in uploadFiles.php as you're not actually printing them out. You need to use echo or even better while debugging, var_dump as this will highlight null variables. Also your JS syntax is incorrect - the output of the PHP needs to be inside the quotes or it will cause a syntax error. Bare in mind that if the session var contains speech-marks, they will need escaping:

<?php
// Account for the possibility of time out
session_start();
if (!isset($_SESSION['LoggedIn']) || !$_SESSION['LoggedIn']) {
    ?>
    <script type="text/javascript">
    alert("Not logged in.  Session log in=<?php var_dump( $_SESSION['LoggedIn'] ); ?>");
    document.location.href="Home.php";
    </script>
    <?php
}
?>
n00dle
  • 5,949
  • 2
  • 35
  • 48
  • I made the change, and made the appropriate edit above, but still get the same result. Thanks, – OtagoHarbour Jan 28 '14 at 03:38
  • 1
    OK, can you use `var_dump` to discover what is in `$_GET['LoggedIn']` in your first file? – n00dle Jan 28 '14 at 04:01
  • It returns string(4)"TRUE" – OtagoHarbour Jan 28 '14 at 04:26
  • 1
    Ok, in both files, please do and edit your question to include the results (the output is likely to be too long for a comment). – n00dle Jan 28 '14 at 04:29
  • I edited the question above. The former file gives array(3) { ["LoggedIn"]=> string(4) "TRUE" ["SetName"]=> string(4) "test" ["UserName"]=> string(5) "OtagoHarbour" } while the latter gives Array(0) {} – OtagoHarbour Jan 28 '14 at 04:39
  • I'm afraid I'm out of new ideas, but try this question - it seems pretty closely related to your own problem: http://stackoverflow.com/questions/17242346/php-session-lost-after-redirect – n00dle Jan 28 '14 at 04:50
0

I was remiss in not mentioning that I was using LAMP. I was also remiss in not checking

/var/log/apache2/error.log

It had the message

PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0, referer: http://whatever.com/uploadFiles.php

The following fixed the problem

sudo chown www-data /var/lib/php5
OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81