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 trialAndrew mwendo
5,712 Pointshelp i am confused
Challenge Task 1 of 2
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.
<?php
session_start();
if(isset($_SESSION['word'][1] && $_SESSION['word'][2] && $_SESSION['word'][3] && $_SESSION['word'][4] && $_SESSION['word'][5])){
$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';
}else{
header('Location: play.php');
}
2 Answers
Josh Carter
5,737 PointsI don't know if this is the best code to get the result, but it works.
First I'm redirecting if the $_SESSION['word'] array is empty, back to the beginning of the game. This might not be necessary.
Then for each variable I am checking if that variable can receive data from the $_SESSION, otherwise I am redirecting to the page that needs to be completed for that value to be used in the variable.
<?php
session_start();
if(empty($_SESSION['word'])) {
header('Location: play.php');
}
if (isset($_SESSION['word'][1])) { $word1 = htmlspecialchars($_SESSION['word'][1]); } else { header('Location: play.php'); }
if (isset($_SESSION['word'][2])) { $word2 = htmlspecialchars($_SESSION['word'][2]); } else { header('Location: play.php?p=2'); }
if (isset($_SESSION['word'][3])) { $word3 = htmlspecialchars($_SESSION['word'][3]); } else { header('Location: play.php?p=3'); }
if (isset($_SESSION['word'][4])) { $word4 = htmlspecialchars($_SESSION['word'][4]); } else { header('Location: play.php?p=4'); }
if (isset($_SESSION['word'][5])) { $word5 = htmlspecialchars($_SESSION['word'][5]); } else { header('Location: play.php?p=5'); }
Ben Payne
1,464 PointsThe only thing I would add is an exit;
statement after setting the header.
Cheers, Ben
Andrew mwendo
5,712 PointsBen payne,its not working still
Ben Payne
1,464 PointsBen Payne
1,464 PointsYou need to warp each
$_SESSION
array check inisset()
in your if statement:A better way to handle this would be to just check that the
$_SESSION['word']
count does not equal five. If so redirect and exit, else write out the story.