Jump to content

Prevent random navigation in PHP


kid_drew

Recommended Posts

Hey guys,

I'm trying to code up a simple game where the user passes from one page to the next through a series of links.  How can I prevent a user from cheating by typing in the URL of a page that is not in the logical flow of the game?  Some kind of cookie trick, I'm sure, but I can't seem to wrap my head around it.

 

-Drew

Link to comment
https://forums.phpfreaks.com/topic/61743-prevent-random-navigation-in-php/
Share on other sites

a session containing the page number

 

so... if the person is on page 2 it would look like this:

http://mysite.com/mypage.php?page=2

 

<?php
if($_SESSION['page'] < $_GET['page']){
    header('Location: /mypage.php?page='.$_SESSION['page']);
    exit;
}else{
    //display the page
}
?>

 

then when they advance, you need to incrament the session page number by 1

<?php
$_SESSION['page']++;
?>

Basically what redarrow means is, use sessions. :-)

 

<?php

  session_start();

    if($_SESSION['step'] != $_GET['page']) {
      header("location: index.php?page=".$_SESSION['step']."");exit;
    }

      else 
      {
        $_SESSION['step']++;
      }

?>

 

But the approach and code depends on how you're moving them from page to page (Or step...wtvr)

 

Edit: Wow...typo :-P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.