ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

if else based on HTTP_REFERER

March 26, 2010 7:59pm

Subscribe [4]
  • #1 / Mar 26, 2010 7:59pm

    jwmatlock

    14 posts

    I’m trying to show content based on the referring url. I tried some php code in my template that looks like this:

    <?php 
    if( $_SERVER['HTTP_REFERER'] == "http://www.mysite.com/download" ) {
      ?>
         <h1>You have access.</h1>
      <?
    } else {
      ?>
        <h1>Sorry, you don't have access.</h1>
      <?
    }
     
    ?>

    I’m wanting people to have access to certain content on a page, but only if they link to that page from the http://www.mysite.com/download page.

    This isn’t working. Anyone have some great ideas on how to do this?

  • #2 / Mar 26, 2010 8:55pm

    mark186282

    290 posts

    in the template preferences, do you have the “allow PHP” and the other setting for input or output set?

    if you do… you can do some inspection of the $_SERVER variable:

    <?php
    print_r($_SERVER);
    ?>

    to see what its returning… and making sure its matching your desired outcome

  • #3 / Mar 27, 2010 12:06am

    jwmatlock

    14 posts

    I do have allow PHP for this template. I’m have PHP Parsing Stage set to input.

    I added the code you gave me, and it’s returning the HTTP_REFERER as expected. The problem is, if I link to this page from the http://www.mysite.com/download page, my result is the “Sorry, you don’t have access” line, and not the “You have asscess” that I want.

  • #4 / Mar 27, 2010 6:11am

    Gareth Davies

    491 posts

    There doesn’t look to be anything wrong here so are you sure your referrer is exactly the same as http://www.mysite.com/download.

    Maybe be a little more flexible using strstr or another string comparison function rather than the direct match?

    <?php
    if (strstr($_SERVER['HTTP_REFERER'],"mysite.com/download"))
    {
    ?>
    <h1>You have access.</h1>
    <?php
    {
    else
    {
    ?>
    <h1>Sorry, you don't have access.</h1>
    }
    ?>
  • #5 / Mar 27, 2010 1:36pm

    jwmatlock

    14 posts

    Thanks Gareth, I looked at my referrer and realized I was missing a need forward slash after ‘/download’. Everything thing seems to be working now as it should.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases