Solved

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

Posted on 2004-08-17
16
294 Views
Last Modified: 2006-11-17
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!
0
Comment
Question by:cberger
16 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11827077
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....


0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 125 total points
ID: 11827086
the HTTP_REFERER is not a very reliable method of doing this for vairous reasons such as it is dependant on the browser, some browsers allow the user to block or change it as do some external applications etc.

another method is on every page have (at the top again)

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


basically if the session isnt set then the var will be set, then the page will be redirected to the index and then when it runs through that code again it will skip it because the session var will be set next time around
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11827094
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.
0
 
LVL 5

Expert Comment

by:richswyatt
ID: 11827166
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.
0
 

Author Comment

by:cberger
ID: 11827204
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.
0
 
LVL 4

Expert Comment

by:Skonen
ID: 11827923
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.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11829366
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
0
 

Author Comment

by:cberger
ID: 11837059
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....
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:cberger
ID: 11837093
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.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11837153
>> 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.
0
 

Author Comment

by:cberger
ID: 11837234
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.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11837277
>> 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
0
 

Author Comment

by:cberger
ID: 11837425
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.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11839627
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;
0
 

Author Comment

by:cberger
ID: 11848234
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
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11858464
no problem, and you too :)

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

|)iablo
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now