Link to home
Start Free TrialLog in
Avatar of cberger
cberger

asked on

Using PHP to redirect external visitors (outside of my domain) to a central index.htm page...

I'm trying to figure out how to redirect visitors to my web site who may get referred thru a search engine to my main index.htm page first, and then they can launch the page they want from there. I intend on running this check on each of my pages.  (A very small site with only 9 pages, so they won't have a hard time finding the page they are looking for.)

I tried using the "getenv("HTTP_REFERER"); function, but didn't have much luck - I am sure it was my syntax.  Here is what I need to be able to do:

1.  Check referring page
2.  If referring page is from any page on my domain, allow the target page to load.
3.  If referring page is external from my domain, automatically redirect the visitor to index.htm
4.  It must work on all current browsers.
5.  Any additional info on how this may effect a search enging crawling my site would be appreciated.

Working sample code will get the points.  Thanks as always, experts!
Avatar of Diablo84
Diablo84

The HTTP_REFERER method would look something like this, at the top of your page:

<?php
if (!strstr($_SERVER['HTTP_REFERER'],"yourdomain.com")) {
 header ("location: index.htm");
}
?>

if the referring domain isnt yours then the index page will load, however....


ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
with regards to spiders/crawlers it will just mean that whatever page they access first they will be redirected to the index and then will follow the links as per normal from that page. It shouldnt have any bad effect on their efficency or ability to properly crawl your site. As with a normal user first time around it will be directed because the session wont be set and after that it will have been set so it can crawl as normal.
You could also use the $_SERVER[] Globals to grab an IP and if it isn't equal to the domain IP - then redirect...

if($_SERVER['REMOTE_ADDR'] != 'mydomain.com'){
     header("location: somepage.php");
}

I think that would be a good solution.
Avatar of cberger

ASKER

Did I do something wrong?  I put the above code at the top of "links.htm".  I cleared my browser's cache, rebooted , and went to www.aaa.com/links.htm directly and it did not redirect me to the index.htm page.

I have read your profile, and have seen what you are capable of, so I humbly assume I'm screwed up.

Here is the top snippet of my page's code:

<?php
session_start();
if (!isset($_SESSION['ref'])) {
 $_SESSION['ref'] == true;
 header ("location: index.htm");
}
?>

<html>
<head>

What am I doing wrong?  Could it be a config setting on my web server (apache)?  I have never had a .htm page with embedded php.  I usually stick all php into a .php page.  Just trying to think of anything....

Thank you in advance for the answers.  I appreciate your help.
You must rename the file as a php file. links.html should be renamed to links.php, otherwise your server won't interpret the php.
yes, cberger unless you wish to configure your server to parse htm files you must change the extension of your file to .php

if you do wish to maintain the .htm extension (presuming you are using apache) open your httpd.conf file (in the conf folder within the apache program files) and find the line:

AddType application/x-httpd-php .php

after it add

AddType application/x-httpd-php .htm
Avatar of cberger

ASKER

Diablo,

The above code is working now, however, only in Firefox.  When I tested it in IE, I am always referred back to index.htm.  It almost seems like there is no session being set.

Any ideas?  I would like to use the code, as it is elegant....
Avatar of cberger

ASKER

richswyatt,

I tried your code, and it also worked in Firefox, but not in IE...  When I tested it in IE, I am always referred back to index.htm.   Here is a sample:

<?php
if($_SERVER['REMOTE_ADDR'] != '208.186.x.x'){
     header("location: index.htm");
}
?>

x's were replaced with my IP.
>> Any ideas?  I would like to use the code, as it is elegant....

Remember the sessions are handled server side so its not a problem with a code, i suspect that it may be a problem with your IE configuration, the session id is passed in a cookie by default so prehaps you have your security setting too high in IE and its blocking the cookie.
Avatar of cberger

ASKER

Hmmm..  I thought of that earlier and checked IE.  (V6.0sp1)  I had cookie wide open, and security low.

What I forgot was that I had PestPatrol installed to try to cut down on hijacks, and it has a built in cookie blocker.

I swaer sometimes I should have gotten interested in painting ceramics instead of computers ;-)

The points are all yours Diablo.  You are the best, and any further php questions will hopefully be answered by you.

Thank you again, and best of luck to you.
>> I swaer sometimes I should have gotten interested in painting ceramics instead of computers ;-)

I know the feeling :)


Glad to help, good luck with the site.

|)iablo
Avatar of cberger

ASKER

I'm not sure what happened, but now it will not work in either browser.  It just keeps looping back to index.htm. Cookies enabled and no Pest Patrol.  I wrote before thinking that was it as soon as I found Pest Patrol blocking cookies.  How frustrating.

I give up.
ive just noticed a very obvious error in the code, really should have seen this first time around, sorry

$_SESSION['ref'] == true;

should be

$_SESSION['ref'] = true;
Avatar of cberger

ASKER

Diablo,

Thanks for checking back.  OK - so I didn't give up.  I hate when I can't do something.  Makes me want to do it more.  I went back to my site last night and did a little redesigning, and reworked my navigation to provide more options for users to get back to the index.htm.

I'm still going to use the script above, but I am going to the "Little League World Series" in Williamsport PA to watch the kids play (and the men they send from taiwan) this weekend.

I will give it a shot first thing Monday night after work.

Thanks again, and have a great weekend!

Cberger
no problem, and you too :)

if you have any more trouble with it on Monday please don't hesitate to post back

|)iablo