display error if webpage is accessed directly

Discussion in 'PHP' started by zenite, May 11, 2008.

  1. #1
    I would like a php script that disallows users to access a webpage directly. I seen a website whereby if you access its webpage from the address bar or other websites, the content will not be displayed. instead, it will say something like "You did not reach this page from www.domain.com/blah.html"

    Basically, it checks the referer site and make sure its from their own domain, and then display the info.

    can anyone show me where to get the script, or point me in the right direction. I am starting to learn PHP, so maybe if it is a simple script, I can do it myself. Thanks.
     
    zenite, May 11, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    To get the referer, you can use the $_SERVER['HTTP_REFERER'] variable with contains it. Then you can use parse_url (or more simply, strstr) to check the domain. Note: the referrer can easily be faked.

    Jay
     
    jayshah, May 11, 2008 IP
  3. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Referrer is not always set on browsers, so this method will only cause you headaches. Best bet is to have them click a button to go to the next page (which can look like a link through CSS) and then pass a POST variable which you then check.
     
    TeraTask, May 11, 2008 IP