Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Browser Persistent Data with PHP Data Persistence on the Web Checking for Sessions

Julien Steel
Julien Steel
10,790 Points

Redirect to play.php when $_SESSION['word'] is not set. What do I do wrong?

Can somebody please assist me with the following error. Does seems to work in workspaces but not in the exercise.

story.php
<?php
session_start();
if ( !isset($_SESSION['word'])) {
  header ('location: play.php'); 
  exit;
}
$word1 = htmlspecialchars($_SESSION['word'][1]);
$word2 = htmlspecialchars($_SESSION['word'][2]);
$word3 = htmlspecialchars($_SESSION['word'][3]);
$word4 = htmlspecialchars($_SESSION['word'][4]);
$word5 = htmlspecialchars($_SESSION['word'][5]);

include 'inc/header.php';

echo '<h1>My Treehouse Story</h1>';

echo '<p>There once was a(n) ' . $word1;
echo ' programmer named ' . $word2; 
echo '. </p>';
echo '<p>This ' .  $word3; 
echo ' programmer used Treehouse to learn to ' . $word4;
echo ' the ' . $word5 . '.</p>';

echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>';
echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>';
echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';


include 'inc/footer.php';

2 Answers

Read the question fully, it's asking you:

Check that each of the required session variables are not set before attempting to write the story. If the session variables are not set, redirect to play.php with the p parameter set to the first missing session variable.

So this means you need to create a for loop and loop through the $_session['word'] to see if one is empty. If it's empty then you need to perform the redirect and pass the parameter P = the value of the $_session['word'] that it empty.

Julien Steel
Julien Steel
10,790 Points

Thank you! I had figured it out in the meantime

Ray Knag
Ray Knag
7,436 Points

I am also having an issue with this challenge and have similar code. I am running into a mental block on my lunch break as to how to set up the for loop to pass the p value of the empty session['word']. Just drawing a blank here... Could you guys help me out?

session_start();
$word1 = htmlspecialchars($_SESSION['word'][1]);
$word2 = htmlspecialchars($_SESSION['word'][2]);
$word3 = htmlspecialchars($_SESSION['word'][3]);
$word4 = htmlspecialchars($_SESSION['word'][4]);
$word5 = htmlspecialchars($_SESSION['word'][5]);

if (empty($_SESSION['word'])) {
  header('location: story.php');
  exit;
}

include 'inc/header.php';

echo '<h1>My Treehouse Story</h1>';

echo '<p>There once was a(n) ' . $word1;
echo ' programmer named ' . $word2; 
echo '. </p>';
echo '<p>This ' .  $word3; 
echo ' programmer used Treehouse to learn to ' . $word4;
echo ' the ' . $word5 . '.</p>';

echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>';
echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>';
echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';


include 'inc/footer.php';
?>
Ray Knag
Ray Knag
7,436 Points

Found out what I was doing wrong, did not realize that I had to establish a variable for the page. Turns out the For loop was not too difficult either, I just plain forgot how need to be setup to run through an array.

for ($i = 1; $i <= 5; $i++) {
  if (!isset($_SESSION['word'][$i])) {
    header("location: story.php?p=$i");
    exit;
  }
}