Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Andrew Blackwell
Andrew Blackwell
13,264 Points

This feels impossible to solve

It feels like there might be a bug in the response engine in this code challenge. No matter what I try, I either get syntax errors where there are none (I've checke the code with another validator), or I'm told that I need to check for $_SESSION['word'] being set, which I do. I've tried with the following code, why is this not working? Or what would be a working solution?:

if (!isset($_SESSION['word'])) { header ('location: play.php'); }

if (empty($_SESSION['word'][1])){ header ('location: play.php?p=1'); } else { $word1 = htmlspecialchars($_SESSION['word'][1]); }

if (empty($_SESSION['word'][2])){ header ('location: play.php?p=2'); } else { $word2 = htmlspecialchars($_SESSION['word'][2]); }

if (empty($_SESSION['word'][3])){ header ('location: play.php?p=3'); } else { $word3 = htmlspecialchars($_SESSION['word'][3]); }

if (empty($_SESSION['word'][4])){ header ('location: play.php?p=4'); } else { $word4 = htmlspecialchars($_SESSION['word'][4]); }

if (empty($_SESSION['word'][5])){ header ('location: play.php?p=5'); } else { $word5 = htmlspecialchars($_SESSION['word'][5]); }

story.php
<?php
session_start();

if (!isset($_SESSION['word'])) {
header ('location: play.php');
}

if (empty($_SESSION['word'][1])){
  header ('location: play.php?p=1');
} else {
  $word1 = htmlspecialchars($_SESSION['word'][1]);
}

if (empty($_SESSION['word'][2])){
  header ('location: play.php?p=2');
} else {
  $word2 = htmlspecialchars($_SESSION['word'][2]);
}

if (empty($_SESSION['word'][3])){
  header ('location: play.php?p=3');
} else {
  $word3 = htmlspecialchars($_SESSION['word'][3]);
}

if (empty($_SESSION['word'][4])){
  header ('location: play.php?p=4');
} else {
  $word4 = htmlspecialchars($_SESSION['word'][4]);
}    

if (empty($_SESSION['word'][5])){
  header ('location: play.php?p=5');
} else {
  $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';

5 Answers

Place your if statements under where they declared the word variables by pulling from the session.

<?php
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($word1)) {
  header('location: play.php?p=1');
}

if (empty($word2)) {
  header('location: play.php?p=2');
}

if (empty($word3)) {
  header('location: play.php?p=3');
}

if (empty($word4)) {
  header('location: play.php?p=4');
}

if (empty($word5)) {
  header('location: play.php?p=5');
}
Andrew Blackwell
Andrew Blackwell
13,264 Points

Hi Corey - thanks for your answer! That definitely worked. I can see that my solution is overkill, but it should theoretically work, no? That's why I'm confused that I keep being thrown an error in the solution.

Yeah, in the real world that solution would totally work. A lot of the code challenges seem to look for a specific thing to exist as opposed to checking for the desired outcome.

You're very welcome

Torben Korb
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Torben Korb
Front End Web Development Techdegree Graduate 91,431 Points

...or collect them in an array which you can loop through:

<?php
$wordArray = [$word1, $word2, $word3, $word4, $word5];

foreach($wordArray as $key=>$word) {
  $page = $key + 1;
  if(empty($word)) {
    header('location: play.php?p=' . $page); 
  }
}