0

I'm creating a very simple PHP (Version 5.3.15) prototype for usability testing. I'm using sessions to pass along data to make the prototype more realistic. (In other words, security and rigor aren't important.) Right now I'm developing this on my localhost, a Mac.

On each page I include a very simple PHP file called "sessionheader.php". This is called before the HTML tag, etc.:

<?php session_start(); ?>

Page 1:

<?php   
include( 'sessionheader.php' ); 
?>

<!DOCTYPE html>
<html lang="en">
<head>...</head>

<body>
  ...
  <?php
    $_SESSION['step'] = "foo";  
    echo $_SESSION['step'];
  ?>
  ...
</body>
</html>

The session variables are not playing back. A couple more details:

  • my PHPSESSID cookie is getting set.
  • I have a destroyer page in my prototype that I use to reset things:

-

<?php
session_start();
session_destroy();
header('Location: index.php');
?>

Any help would be appreciated. Please LMK if I can provide more details.

ElBel
  • 1,954
  • 5
  • 16
  • 27
  • 3
    You develop with `error_reporting` at maximum and `display_errors` enabled, right? – Jon Oct 02 '13 at 21:59
  • 1
    I would change `include` to `require_once( 'sessionheader.php' );` Seems like the sessionheader is required before continuing – immulatin Oct 02 '13 at 22:01
  • 1
    There is nothing really wrong with the information you've posted. There has to be something else going on. Try to place `print_r($_SESSION);` right after the `session_start();` to see if it's tracking anything. – rfoo Oct 02 '13 at 22:01
  • display_errors is on, but error_reporting is not. I need to follow these instructions? http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php – ElBel Oct 02 '13 at 22:03
  • If you're setting the session['step'] on another page, it is not displaying because you keep changing it to "foo" just before echo. – Gary Hayes Oct 02 '13 at 22:05
  • you can not set session variable in the middle of the page! – pregmatch Oct 02 '13 at 22:07
  • @pregmatch, can you elaborate? – ElBel Oct 02 '13 at 22:12
  • @GaryHayes, the issue is that nothing is getting output when I echo the $_SESSION variable. :-/ – ElBel Oct 02 '13 at 22:13
  • @ Ellen B session is already started at the header. only way that you can assign value to session is to do that before any html output. At the beginning of page! – pregmatch Oct 02 '13 at 22:14
  • 1
    do this then instead of echo. var_dump($_SESSION); it will give you an array of all the session variables that are currently set. – Gary Hayes Oct 02 '13 at 22:16
  • 2
    That's not true pregmatch. You can change session vars after output. – Gary Hayes Oct 02 '13 at 22:22
  • 1
    Is there a setting in php.ini that disables the use of Sessions? That's where I'd be looking, unless you're destroying the sessions between each page load. – Gary Hayes Oct 02 '13 at 22:24
  • So: If I set a session variable (before HTML output, thanks @pregmatch), var_dump($_SESSION) outputs nothing (i.e. no results -- but the session is getting started). But if I progress forward and then hit "back" to the original pae, var_dump outputs the variables I was expecting. Huh???? – ElBel Oct 02 '13 at 22:25
  • @Gary Hayes if you try to set session variable in a middle of the page you will get error!!! put ini_set("display_errors",1) in your file and you will see message can not set session blabla, headers are already started! – pregmatch Oct 02 '13 at 22:29

2 Answers2

0

You need to set up a test such as this:

if (!isset($_SESSION['step'])){
$_SESSION['step'] = "foo";
}
echo $_SESSION['step'];
Gary Hayes
  • 1,728
  • 1
  • 15
  • 23
0

I had a number of problems that were solved by different answers here and elsewhere.

  1. I had a typo in one place.
  2. per jquery progressbar - loads all at once I added session_write_close() statements, which seemed to help.
  3. Thanks to @jon's comment I turned on full error messages in my local php.ini; this has helped debugging a lot.

This isn't really an "answer" but I appreciate the comments that helped me find what was wrong. Thank you!

Community
  • 1
  • 1
ElBel
  • 1,954
  • 5
  • 16
  • 27