dragunoff

[WP] Template Name: Check HTTP Referrer

Mar 15th, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: Check HTTP Referrer
  4. Description: Page template with referrer tracking by hostname. Allowed hostnames are defined in a custom field named 'allowed-hosts'. Use full hostnames (e.g. www.facebook.com vs facebook.com) and separate multiple values with a space. The template works as a normal page template if there's no custom field set or if 'track-referrer' is not set.
  5. */
  6.  
  7.  
  8. // set vars
  9. // HTTP referrer
  10. $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false;
  11.  
  12. // get array of allowed hosts from custom field
  13. $allowed_hosts = get_post_meta( $post->ID, 'allowed-hosts', true );
  14. $allowed_hosts = !empty($allowed_hosts) ? explode( ' ', $allowed_hosts ) : false;
  15.  
  16. // custom field switch to turn tracking on/off
  17. $tracking = get_post_meta( $post->ID, 'track-referrer', true );
  18. $tracking = ($tracking == '1') ? true : false;
  19.  
  20. // error message and status
  21. $error_msg = 'Not allowed.';
  22.  
  23. // debug
  24. if(is_user_logged_in()) {
  25.     $debug = '<pre>';
  26.     $debug .= '<br />Enabled: ' . var_export($tracking, true);
  27.     $debug .= '<br />Referrer: ' . var_export($referrer, true);
  28.     $debug .= '<br />Host: ' . var_export(parse_url( $referrer, PHP_URL_HOST ), true);
  29.     $debug .= '<br />Allowed hosts: ' . var_export($allowed_hosts, true);
  30.     $debug .= '</pre>';
  31.    
  32.     $error_msg .= $debug;
  33.    
  34.     echo $debug;
  35.    
  36. }
  37.  
  38.  
  39. // if referrer is set and custom fields exist
  40. if( $referrer && $allowed_hosts && $tracking ) {
  41.    
  42.     // get hostname from referrer
  43.     $host = parse_url( $referrer, PHP_URL_HOST );
  44.  
  45.     // add current page to array of allowed hosts because of form validation/submission
  46.     $allowed_hosts[] = parse_url(get_permalink(), PHP_URL_HOST);
  47.    
  48.     // compare referrer to array of allowed hosts
  49.     if ( in_array ( $host, $allowed_hosts, true ) ) {
  50.    
  51.         // load page template
  52.         locate_template( array('page.php'), true, true);
  53.    
  54.     // referrer is not allowed
  55.     } else {
  56.    
  57.         // error message and status
  58.         wp_die($error_msg, '', array( 'response' => 403 ) );
  59.        
  60.     }
  61.    
  62. // no referrer but custom fields exist
  63. } elseif ( !$referrer && $allowed_hosts && $tracking ) {
  64.    
  65.     // error message and status
  66.     wp_die($error_msg, '', array( 'response' => 403 ) );
  67.    
  68. // no custom field (disable referrer tracking)
  69. } else {
  70.  
  71.     // load page template
  72.     locate_template( array('page.php'), true, true);
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment