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 Reading, Writing and Destroying Sessions

Brenda Krafft
Brenda Krafft
18,036 Points

Checking to see if $_Session variables are unset. Course: Browser Persistent Data with PHP

I am getting this error: "When the $_SESSION['word'] array is not set, you should redirect to the play.php page."

Isn't that what I'm doing?

<?php
session_start();
if (!isset($_SESSION['word'])) {
    header('location: play.php');
    exit;
} else {
    //Do other stuff
}

3 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

Hi Brenda -

You have to check if each of the 5 words have been set and if one of them has not, redirect to play.php?p=, setting p equal to the first word reached that was not set. I wasn't sure the best way to tackle this...I just used a for loop evaluating

$_SESSION['word'][$i], from i=1 to i=5

Let me know if you have other questions, I left my description somewhat vague so as to not simply post the answer.

Rafael Felipe
Rafael Felipe
6,058 Points

Thanks Benjamin your explanation helped me a lot!

Would you mind explaining further? i've tried out a few different answers but I'm in the ballpark just not getting the correct answer.

<?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]);

Try using the following (I have not put the full solution):

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

remember $word1 to $word5; & p=1 to p=5

Alena's quizzes are a bit more tricky.