Advertisement
chrishajer

Page template to check referring page and redirect

Aug 9th, 2012
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: Check Referer
  4. */
  5. /*
  6.    http://www.gravityhelp.com/forums/topic/lock-down-a-page-unless-you-complete-a-form
  7. */
  8. // if the current user cannot edit posts, they might not be logged in, or might be logged in as subscriber
  9. // which is the only role which does not have edit_posts capability.  This is so editors and administrators
  10. // can directly access the page to make edits, without having to submit the form to set the HTTP_REFERER
  11. if(!current_user_can('edit_posts')) {
  12.     // define the URL where we want to send people instead of showing them the page they requested
  13.     $url = trailingslashit(get_bloginfo('url'));    // can be any URL.  This is the URL for the home page of the site
  14.     // define the URL you want to make sure people come from before gaining access to this protected page
  15.     $check_url = 'http://www.example.com/page-on-your-site/that-people-must-come-from/';
  16.     // if the current user is not logged in with more than a subscriber role, check to see if HTTP_REFERER is set
  17.     // make sure HTTP_REFERER is set before trying to read the value; avoid PHP warnings and notices
  18.     if(isset($_SERVER['HTTP_REFERER'])) {
  19.         $referring = $_SERVER['HTTP_REFERER'];
  20.         if ($check_url <> $referring) {
  21.             header("Location: $url");
  22.             // don't process any more code
  23.             exit;
  24.         }
  25.     }
  26.     // if HTTP_REFERER is not set, redirect to $url as well.  This may be overly aggressive
  27.     else {
  28.         header("Location: $url");
  29.         // don't process any more code
  30.         exit;
  31.     }
  32. }
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement