You are here:

PHP5/PHP Cookies

Advertisement


Question
QUESTION: Thank you so much for that last answer. That worked great. So my next question is how do I protect q1.php? If I go to the site, and I just type in url.com/q1.php, I get to that page, when I want to be redirected to the index.php if they haven't answered the question. I would know how to do this if it was a login. But I can't figure it out for this. Here is the code for it https://gist.github.com/burn123/5009737

ANSWER: At the top of q1.php, just check for the existence of the cookie that you set when a user answers the question on index.php correctly:

if(!isset($_COOKIE['passed_index'])) {
  header("Location:index.php");
  exit;
}


---------- FOLLOW-UP ----------

QUESTION: So if I wanted to use this for multiple cookies, would I write this as

if(!isset($_COOKIE['c1']) and !isset($_COOKIE['c2'])) {
 header("Location:index.php");
 exit;
}

Thank you so much!

ANSWER: Absolutely. That would work perfectly fine.

---------- FOLLOW-UP ----------

QUESTION: Thanks for your help. A few more questions
https://gist.github.com/burn123/5009737

1. How would I set this up to have more than one answer to the question of the dad? Like, for spelling purposes?

2. How would I set this up so that there are multiple text boxes, and they have to get them both right to proceed?

Answer
Updated code can be viewed at http://pastebin.com/mHmkjmEU

For #1, you simply place your acceptable answers in an array, and use in_array() to check if the submitted value is in the array.

For #2, you simply add an additional conditional in your check to see if the answers were correct using &&.

The code is fairly self explanatory.

Please let me know if you need any more help.

Kevin

PHP5

All Answers


Answers by Expert:


Ask Experts

Volunteer


Kevin Cackler

Expertise

Any and everything related to PHP4 and PHP5. I specialize in functional, readable, scalable object oriented code, and can answer your troublesome class and object questions.

Experience

5 years developing in PHP using flat files and databases (MySQL, Oracle) Lead PHP developer for a very large Texas based web hosting company The coder behind some of the largest pet communities online.

Education/Credentials
BS - IT/CS

©2016 About.com. All rights reserved.