PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,574 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,198 people online right now. Registration is fast and FREE... Join Now!




PHP redirect based on referer

 

PHP redirect based on referer, allow access only if coming from two specific url's

midasxl

2 Jan, 2009 - 08:32 AM
Post #1

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 125



Thanked: 1 times
My Contributions
Hello and thanks for your time. I am attempting a PHP redirect using the referer as the determining factor. I want to redirect to a new page ONLY if the user is coming from a specific url. I have a working script using just one referer URL.

What I would like to do is add three more URL's, so that the redirect will work if the user is coming from any one of those four. I tried to add the url's using the "or" operator ( || ) but no go. Any ideas?

The following code is what I would like it to do, but the || operators are not doing the trick. Are they the right solution? Are they in the wrong place? etc. Thank you!!

CODE

<?
$referer = $_SERVER['HTTP_REFERER'];
if ( $referer != "http://first_url" || "http://second_url" || "http://third_url" || "http://fourth_url" ) {
  header('Location: http://desired_url);
  exit;
};
?>


User is offlineProfile CardPM
+Quote Post


William_Wilson

RE: PHP Redirect Based On Referer

2 Jan, 2009 - 08:49 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,637



Thanked: 88 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
it does work, but you need to specify the variable in each or:
if ( $referer != "http://first_url" || $referer != "http://second_url") {
as an example

you may be able to do something like:
if ( !($referer == "http://first_url" || "http://second_url")) {
but I have not tried this.

Personally I would use a loop with an array of urls, thus if the list gets longer you do not need to make your if statement longer.
A database would be the best option, but I do not know if you have one available.
User is offlineProfile CardPM
+Quote Post

snoj

RE: PHP Redirect Based On Referer

2 Jan, 2009 - 09:04 AM
Post #3

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
You could also use in_array() instead of a loop!

CODE

$fromURL = array("http://example.com","https://my.email.net","http://etc.al");

if(in_array($referer,$fromURL)) {
   do_something_awesome();
}


This post has been edited by snoj: 2 Jan, 2009 - 09:04 AM
User is offlineProfile CardPM
+Quote Post

brawnyman713

RE: PHP Redirect Based On Referer

2 Jan, 2009 - 09:23 PM
Post #4

D.I.C Head
Group Icon

Joined: 21 Oct, 2007
Posts: 139


Dream Kudos: 125
My Contributions
It looks like in your code you're using or '||'. Shouldn't you be using and '&&'? What your code is saying is 'If the referrer is not equal to this or the referrer is not equal to that'. Did you mean to say 'if the referrer is not equal to this AND the referrer is not equal to that'?

And one more thing. Because the referer is a header that comes from the client, it can be spoofed. So if you're planning on using this for something big, I'd suggest increasing the security, because this little measure can be bypassed
User is offlineProfile CardPM
+Quote Post

snoj

RE: PHP Redirect Based On Referer

2 Jan, 2009 - 11:53 PM
Post #5

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
Good catch brawny! However, if he did go that route, he would need to take away the !....if it worked.

Anyway, I did some testing with php 5.2.4 and here's my results.

CODE

echo ("a" === true || false || 2); //url not on the list
echo ("a" !== true || false || 2); //url not on the list
echo (bool) (true === true || false || 2); //url on the list
echo (bool) (true !== true || false || 2); //url on the list


All four result in 1, and so if we plugged in your referrer stuff, you'd see that no matter what, you'd be redirected to the "secure" location.
User is offlineProfile CardPM
+Quote Post

Rechocto

RE: PHP Redirect Based On Referer

4 Jan, 2009 - 04:10 AM
Post #6

D.I.C Head
**

Joined: 4 Jan, 2009
Posts: 50


My Contributions
I Had\have 3 sites running off of one computer (one IP address) with separate noip's.. this is basically what I do (names\links removed so it doesn't appear that I am advertising)
CODE

<?php
$dir = Index2.php;
$sn = strtolower($_SERVER['SERVER_NAME']);
if(strpos($sn, "firstsite") !== false) {
    $dir = "/firstdir/index.php";
} else if (strpos($sn, "secondsite") !== false) {
    $dir = "seconddir/index.php";
} else if (strpos($sn, "thirdsite") !== false) {
    $dir = "/thirddir/index.php";
}

header( 'Location: ' . $dir );

?>


This can be changed to use $_SERVER['HTTP_REFERRER'] and full URLs pretty easily.
You have the basic Idea down, but you are basically pitting
if ("something") { /*go here*/ }

"something" != 0, therefore is true. you NEED:
if($something == "something") { /* go here */ }

define your variable in EACH conditional statement... or make a bool function to handle that and use that for each condition.

for example:

CODE

function C($s) { if($s == $_SERVER['HTTP_REFERRER']) { return true; } else { return false; } }

if( C("first_url") || C("second_url") || C("third_url") ) {
    //goto the secure location!
}



which may work better in your situation smile.gif
User is offlineProfile CardPM
+Quote Post

Hary

RE: PHP Redirect Based On Referer

4 Jan, 2009 - 10:44 AM
Post #7

D.I.C Regular
Group Icon

Joined: 23 Sep, 2008
Posts: 411



Thanked: 40 times
My Contributions
If you just want to host multiple sites, why do you want to do the redirect of the hostname yourself? Isn't it easier to let your web server handle those things in a vhost?

@Rechocto:
That last function is an example of a bad coding habit. If your code grows, no one will ever know what function C() does. Even with small examples, you'd better use good describing variable names.
User is offlineProfile CardPM
+Quote Post

midasxl

RE: PHP Redirect Based On Referer

7 Jan, 2009 - 09:13 AM
Post #8

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 125



Thanked: 1 times
My Contributions
Wow, thanks for all the information. I have certainly learned alot from your posts. I ended up going this route

CODE

<?
$referer = $_SERVER['HTTP_REFERER'];
$fromURL = array("http://www.siteone.com","http://www.sitetwo.com","http://www.sitethree.com/","http://www.sitefour.com");
if(!in_array($referer,$fromURL)) {
  header('Location: http://www.desiredURL.com/errorpage.html);
  exit;
};
?>


This seems to be working fine. Anyone see anything wrong with it? I'm concerned about security issues.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:15AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month