using php and HTTP_REFERER to create a condition for loading js
I'm trying to create a splash page effect on a site I'm working on (I know splash pages are bad etc but I have my reasons), and basically I want to call the script that runs the splash image overlay only if the visitor is coming to the index from an external website. That way if a visitor clicks "home" from an internal page the splash won't launch. I've been searching about and it seems like I can do this with php using $_SERVER['HTTP_REFERER'], but I'm brand new to php and after playing with it all afternoon can't seem to make it work.
The following code loads the script but it doesn't seem to care if the referring URL contains "mysite" and an error message appears at the top of the page reading:
"A PHP Error was encountered Severity: Notice Message: Undefined index: HTTP_REFERER Filename:..."<?php
$referrer=$_SERVER['HTTP_REFERER'];
if(stristr($referrer, "mysite") == FALSE) {
echo '
<script type="text/javascript">
$(document).ready(function() {
$("#wrapper").hide();
$("#imgContainer").npFullBgImg("/imgs/splash_image.jpg", {fadeInSpeed: 2000, center: true});
$("#logoContainer").fullLogoImg("/imgs/splash_logo.png", {fadeInSpeed: 2000, center: true});
$("#logoContainer").click(function(){
$("#wrapper").show("fast");
开发者_如何学JAVA $("#splash_kill").remove();
$(this).remove();
});
});
</script>';
}
?>
Any help would be much appreciated. Thanks!
The notice is telling you that a HTTP_REFERER isn't set.
'HTTP_REFERER' The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
http://php.net/manual/en/reserved.variables.server.php
A quick search led to this, which provides a little more detail: http://www.electrictoolbox.com/php-http-referer-variable/
Update:
You could try using a session variable. The variable could be a simple boolean, which is set to true if the splash page has been displayed, false otherwise. This would require some session handling on every page that may be an entry point to the website and/or every page where you'd want to display the splash.
http://php.net/manual/en/function.session-start.php
Of course, this would only be viable for the duration of the session.
So, you could use a cookie instead or with this approach.
Personally, I would probably opt for the simple cookie approach, as this is just a splash. No need to add the overhead of sessions for something this simple.
This is what I am trying. I am not sure about the wildcard:
<?php
//echo $ref
$ref = $_SERVER["HTTP_REFERER"];
//if is NOT coming from inside linking
if ( $ref != '*www.example*' )
// send to index and exit
{header("Location:http://www.example.com/index.php");
Exit;
//
}
?>
While you can't completely rely on $_SERVER['HTTP_REFERER'] to be set, I think you will find that it is reliable enough for a splash page. When it isn't set, there is no referer and that will be blank, as it isn't in the header.
You should first test to see if empty($_SERVER['HTTP_REFERER']) and then check to see if it has your own domain in it.
On another note, I was working on an old IIS server that changed the names of some of those $_SERVER variables, and generally messed them up. I'd do a print_r($_SERVER) to see what's up.
if(isset($_SERVER['HTTP_REFERER'])&&!empty($_SERVER['HTTP_REFERER'])) {
$http_referer=$_SERVER['HTTP_REFERER'];
}
精彩评论