Forum Moderators: coopster

Message Too Old, No Replies

If referral NOT from same domain THEN ...?

Whats the syntax for this?

         

JAB Creations

2:37 pm on Apr 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



file1.php on www.example.com
If file1.php was not referred by file of same domain (www.example.com) then include php from this file2.php.

I basically would like to print out a message box with html and stuff if the referred isn't from my own domain (so I can have the user specify some quick preferences).

Reffer code is this....

$_SERVER['HTTP_REFERER']

So here I am trying to put this together but I'm short on some of the sytax! :-D

<?php
if $_SERVER['HTTP_REFERER'] ....

How do I say "Does not start with!" Lets say I want to say if the referrer does not start with "http://www.example.com" THEN...

gliff

3:38 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



You want Regular Expressions, either the preg_ or ereg_ functions. Standard Disclaimer: If you're going to be doing any kind of web programming, it's a Very Good Idea to learn regular expressions, the O'reilly "Mastering Regular Expressions" book is good.

You specific case could be handled like this, if your domain was example.com


//throwing referer in an array for clarity
$ref = $_SERVER['HTTP_REFERER'];
if(preg_match('/^example\.com/', $ref) ){
//do matched stuff
}
else{
//do unmatched stuff
}

The important part is the /^example\.com/. The two forward slashes say (more or less) the text in-between here is our regular expressions. The ^ says "any string starting with the following. The \. is needed because the period is a special character in regular expressions, and needs to be escaped (like double quotes in strings.

If you've followed this so far, you probably realize the above will only match example.com, and NOT www.example.com. To handle that, you could do it the obvious way with two calls to preg_match


if(preg_match('/^example\.com/', $ref) ¦¦ preg_match('/^www\.example\.com/', $ref)){
//do matched stuff
}
else{
//do unmatched stuff
}

or you could do it the "better" way, with a single regular expresion.

if(preg_match('/^(www\.example¦example)\.com/', $ref) ){
//do matched stuff
}
else{
//do unmatched stuff
}

See if you can understand the last one after you've read up a bit on regular expressions.