You are here:

PHP5/PHP Cookies


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, 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

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'])) {

---------- 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'])) {

Thank you so much!

ANSWER: Absolutely. That would work perfectly fine.

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

QUESTION: Thanks for your help. A few more questions

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?

Updated code can be viewed at

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.



All Answers

Answers by Expert:

Ask Experts


Kevin Cackler


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.


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.


©2017 All rights reserved.