Solved

Handling a "referer" page politely

Posted on 2008-10-28
4
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change Wording in Wordpress Plugin 4 37
Echo values after a query in php 5 49
Horizontal Full Calendar using php 5 42
SSL unsecure page mystery 17 45
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

737 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