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.
Thomas Morling
12,754 PointsStumped Where do I begin? Setting Session Variables Challenge
Stumped Where do I begin? Setting Session Variables Challenge
<?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]);
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';

Corey Cramer
9,453 PointsYou're very welcome!
2 Answers

Corey Cramer
9,453 PointsThe first thing you need to do is see if the session word array has a value for each index (1 through 5)
<?php
session_start();
if ( ! isset($_SESSION['word'][1]))
{
header("location: play.php?p=1");
}
if ( ! isset($_SESSION['word'][2]))
{
header("location: play.php?p=2");
}
if ( ! isset($_SESSION['word'][3]))
{
header("location: play.php?p=3");
}
if ( ! isset($_SESSION['word'][4]))
{
header("location: play.php?p=4");
}
if ( ! isset($_SESSION['word'][5]))
{
header("location: play.php?p=5");
}
// ... Challenge code continues below
Or a cleaner way to approach the problem:
<?php
session_start();
for ($i = 1; $i <= 5; $i++)
{
if ( ! isset($_SESSION['word'][$i]))
{
header("location: play.php?p=$i");
}
}
// ... Challenge code continues below

Scott Landon
Courses Plus Student 5,090 PointsThis works well enough and also limits duplicating code!
for ($i = 1; $i <= 5; $i++){ if (!isset($_SESSION['word'][$i])){ header("location:play.php?p=$i"); } }
Thomas Morling
12,754 PointsThomas Morling
12,754 PointsThanks!