Question about a PHP Varible and Array

Discussion in 'PHP' started by infopost, Jul 12, 2011.

  1. #1
    Hi,

    I have ran into a problem with a script I am using that shows content to a visitor based upon their browser referer. Here is the relevant portion of the script:

    <?php
    $allowed_referers[0] = "allowedwebsite.com";
    $ok_mode = 0;
    
    if((in_array(parse_url($_SERVER['HTTP_REFERER'],PHP_URL_HOST), $allowed_referers) && $_SERVER['HTTP_REFERER'] != "") || $ok_mode)
    {
    }
    ?>
    PHP:
    It works fine until I want to add additional allowed referers and that is where I encounter a problem. I would like to either add the list (which includes about 15 allowed referers) into the script or add a function that will retrieve the allowed referers from a text document.

    Any help would be greatly appreciated.
     
    infopost, Jul 12, 2011 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    $allowed_referers[] = 'address.com';
    PHP:
    Just use this line inside a loop or anything, php will automatically append the item to the array.
     
    ads2help, Jul 12, 2011 IP
  3. infopost

    infopost Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I appreciate your response but could you please use your code in an example with the code I posted? One more thing is that the script must keep the 0 in brackets as shown here
    $allowed_referers[0]
    PHP:
    since it designates whether it is in ok_mode (which is setup mode by the way).

    Again I appreciate any help in this matter.
     
    infopost, Jul 12, 2011 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    No you can't. Keeping the 0 in bracket will replace the array value of item at index 0, so your array is forever having only 1 item.

    in my previous post i assumed you know how to do a loop. So forget it now. Here's how without using a loop, add manually into your code.

    Method 1:
    $allowed_referers[0] = "allowedwebsite.com";
    $allowed_referers[1] = "allowedwebsite1.com";
    $allowed_referers[2] = "allowedwebsite2.com";
    .....
    
    PHP:
    Method 2:
    $allowed_referers = array("allowedwebsite2.com" , "allowedwebsite1.com", "allowedwebsite2.com", );
    PHP:
    Method 3:
    $allowed_referers[0] = "allowedwebsite.com";
    $allowed_referers[] = "allowedwebsite1.com";
    $allowed_referers[] = "allowedwebsite2.com";
    .....
    
    PHP:
    All 3 methods works the same.
     
    ads2help, Jul 12, 2011 IP
  5. infopost

    infopost Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks ads2help. The first and third examples you provided work perfectly. For some reason the second example will not recognize the referers at all. This is the one I had been trying to get working before I made a post here.

    Again I appreciate you taking the time to help me figure this out.
     
    infopost, Jul 12, 2011 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    This is weird, second example should be the best way to do it.

    No problem, cheers :)
     
    ads2help, Jul 12, 2011 IP