0

I have created a small quiz in PHP, that asks you a series of yes/no questions and then tells you the answer at the end. I am trying to use PHP sessions so that if the user only gets half way through the quiz, for example then when they close their browser and re-open it it will be on the same question as when they left. Below is the code for one of the forms I am using.

<div class='questions'>
  <form method ='GET' action='Creatures.php'>
    <input type='submit' name='answer$number' value='Yes' class='buttons' />
    <input type='submit' name='answer$number' value='No' class='buttons' />
  </form>
</div>

If you want to see the quiz go to s504518.brunelweb.net/Creatures.php . I am not sure how to do this though as I haven't used sessions before, any help would be much appreciated.

4
  • $)$ (aka 404 while holding the SHIFT key) Commented May 31, 2012 at 15:29
  • I don't understand, where do I put that? Commented May 31, 2012 at 15:33
  • your link not exist link.if you want to save the data, need set them in session , also you can use cookie for your aim,add more info about what u really want to do? which html element you have in your form? Commented May 31, 2012 at 15:38
  • sorry the link is s504518.brunelweb.net/Creatures.php. The main aim I want to achieve is so that when the user closes their browser, when they re-open the browser they will be at the same as when they left it Commented May 31, 2012 at 15:51

2 Answers 2

3

You can't do this using PHP sessions because a session is destroyed when the browser is closed.

To achieve what you mention you should save the end-user's answers into a cookie and check for its existence when the page is initially loaded.

Take a look at http://php.net/manual/en/function.setcookie.php.

Sign up to request clarification or add additional context in comments.

Comments

0

If the users are registered, store it in a db. That's usually the best bet. Cookies can work but u have a chance of something going wrong.

Also - if i read correctly, answer $number doesn't concat in single quotes. would either be:

echo " name='answer$number' " (echo'd in double quotes) OR echo 'name="answer'.$number.' " ' (echo'd in single quotes). this is frugal, nonetheless could be misleading. I spaced the quotes to indicate the gap, these however should pref be deleted.

I believe a db is the SAFEST way. U can always use a cookie to link a user to their answers. Also - a db will give u stats later. This way even if a user loses their cookie somehow they can always enter their email address to continue.

And yes, sessions are ALWAYS killed when the browser closes (just to confirm).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.