ZoEM
ZoEM

Reputation: 852

A way to redirect user back to specific page when URL is typed in PHP

I have a PHP website of 6 pages and I want to have a functionality such as this:

The website is a little quiz game where you need to get through 5 trivia questions per page and the final page displays the highscore. The score is based on how fast you got there as the pages have a timer.

But I found out about a cheat I want to fix. If you simply type in the URL highscore.php or question5.php, you can get there faster without having gotten through the first few pages.

Is there some way to fix this?

Upvotes: 1

Views: 70

Answers (2)

Gordon
Gordon

Reputation: 316999

Track the state of the quiz on the backend, e.g. track which questions have been answered yet. When the user tries to access a page that would require a previous page to be completed first, redirect the user to that page instead.

You can achieve this with a Session.

An even better solution than having six pages would be to have one page instead, e.g. quiz.php and then funnel all access through this page. This will make it easier to track progress because you don't need to copy and paste the code to the individual pages.

On a side note: you also want to track the time the quiz was started on the backend.

Upvotes: 3

DASH
DASH

Reputation: 191

It's not that hard, just a careful coding is all you require.

Firstly, start a session and set it to an initial value, say 1, that means the user is in page 1. If he submits the answer, and then the user loads any other page in your website, create a script to call the same session value and use header("Location:page2.php")to force a redirect to page 2 or the page he is supposed to be in.

If the session is removed somehow then use isset() to check if it exists, if it doesn't start from the beginning.

Upvotes: 0

Related Questions