Solved

Handling a "referer" page politely

Posted on 2008-10-28
4
248 Views
Last Modified: 2013-11-05
I am trying to restrict access to a web page unless access is referred from a specific web site using:

if($_SERVER['HTTP_REFERER'] !="http://localhost/test/xrefer.php")
      {
            header( 'Location: http://10.50.1.139:8080');
      }

This snippet works (afaik) but it simply redisplays the page they came from which makes it appear as though the link isn't working. When I try to add an "echo" line above the header line to say " you can only get here from  xrefer.php" then the error message displays saying the headers have already been sent. I get that. But how can I indicate to a user that he's been redirected?

Thanks in advance
Steve
0
Comment
Question by:SteveJ
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:DarkFish
DarkFish earned 100 total points
ID: 22825043
It is possible to do this, however you would need to use something other than a server side PHP script; or additional JavaScript.

You could use the PHP script to write a message and some JavaScript code, and execute the JavaScript when the page has loaded.

This gives the user X amount of time to read the message before being redirected.

In order to echo something while also using header(), you would have to add the echo() after the header() call; but you probably wouldn't see the output as the browser would be redirected almost instantly. Because of the way header redirects are handled; you are better of using a meta-refresh or some JavaScript to load the new page after X seconds. :)
0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 150 total points
ID: 22825200
darkfish is right, I'd prefer the meta refresh so you aren't relying on javascript support...


if($_SERVER['HTTP_REFERER'] != "http://localhost/test/xrefer.php"){
      echo 'You are being redirected <meta http-equiv="refresh" content="3">';
      //header( 'Location: http://10.50.1.139:8080');
}
0
 
LVL 16

Author Closing Comment

by:SteveJ
ID: 31510893
Thanks. I gave more points to MMD because he included a code snippet. If you think that's unfair, let me know, I'm not trying to be a jerk.

Thanks
0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22826970
well it was his idea
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question