Think Visualizer and expand your options
 
Visualizer Navigator 
   Home | Keep up with the news | The Photo FX tour | Photo FX Bookmark and Share

Go Back   Visualizer Forums > Visualizer Online Learning > Web design and Scripting
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread Display Modes
Old 10-27-2004, 02:53 AM   #1
Darren
VIG, Project Manager
 
Darren's Avatar
 
Join Date: Aug 2004
Location: US of A
Posts: 1,861

my photo blog, by Darren
Arrow How to block a referrer

In some cases it would be nice if one could block referers, like warez, adult and other sites which audience you might not find appropriate for your site policies.

But how is it done?

Well, there are two ways to handle this - either using a standard html page combined with a javascript, or you could use a dynamic page such as PHP.

You will quickly discover that PHP is by far the best, as you can log data from the visitor and the refering site. To give you a quick example on how it is done check the code below.
PHP Code:
 //Let's shorten the referer name...
$refurl parse_url($_SERVER['HTTP_REFERER']);    
$referer $refurl['host'];

//The referering url must not contain the word "spam"
$word "spam";

/* Now we can scan our string for the word "spam" by 
matching the two variables $referer and $word */

if (strpos($referer$word)!==false){
   
//Post comment
   
echo "We do not accept the refering url!";
   exit();



As you can see it doesn't require much coding. If you want to log the data using a flat file (*.txt) try using the following command.

PHP Code:
 /* We need a variable name for our text file which will 
contain the log information. I have named it $reflog,
but you can call it something else if you feel like it. */

$reflog "log.txt";

//Now, let's try another approach
if (strpos($referer$word)!==false){
   
//Post data and comment
   
$RefAdd fopen($reflog'a');
   
fputs($RefAdd"$refurl");
   
fclose($RefAdd);
   echo 
"We do not accept the refering url!";
   exit();



Now we have added the log information from the refering website and posted it into the file "log.txt". You can add all the information you want, this is just an example to show you how it is done.

I started by saying it could be done using a javascript as well, so I might as well show how that is done too. For this we need an Array where we will place the referers we do not accept.
HTML Code:
<head> <script language="javascript"> //Let's create the variable for the array var refarray = new Array(); //Now we will add the strings into our array refarray['somesite.com'] = "?blocked"; refarray['anothersite.com'] = "?blocked"; //Here we scan our array for the above strings for (var i in refarray) { if (document.referrer.indexOf(i) != -1) window.location.replace(refarray[i]); } </script> </head>


As you can see it can also be done very easy by using a Javascript, however if you have the option of using a dynamic page, I highly recommend using it.
Databases, like MySQL are great tools for storing data, but for now I will just show you the above feature, posting to a text file.

Good luck with your scripting!

Remember to join and post your comments or submit your own tips and tricks
__________________
Project manager
Visualizer Image Group


"Don't judge yourself on how far you have come,
It's better to get somewhere than nowhere"

Last edited by Darren : 10-27-2004 at 02:59 AM.
Darren is offline  Live RSS Feed from my photo blog   Reply With Quote
Old 02-10-2007, 08:35 PM   #2
raj5
Junior Member
 
Join Date: Feb 2007
Posts: 6

Default Referral URL

Hi

I just want to know whether it is similar to /?XYZ or not. In case it is then kindlypost the code in ASP

I am an ASP developer and cannot use that code

raj
raj5 is offline   Reply With Quote
Old 06-10-2007, 09:25 PM   #3
Hermit
Junior Member
 
Hermit's Avatar
 
Join Date: Feb 2006
Location: U.S.A., Ohio
Posts: 8

Default

Quote:

//The referering url must not contain the word "spam"
$word = "spam";


If you want to use more than 1 word do you use a comma or what?
Hermit is offline   Reply With Quote
Old 06-11-2007, 11:15 AM   #4
Darren
VIG, Project Manager
 
Darren's Avatar
 
Join Date: Aug 2004
Location: US of A
Posts: 1,861

my photo blog, by Darren
Default

you can loop the string contain one or several words

By using the PHP explode and then scanning the arrays of the strings.

Here's an example.
PHP Code:
 $word "spam#malware#spyware"

As you can see we have added several words into our string and we are splitting the words with the "#" symbol, and we can now use the explode function to split the string into arrays. Some will use white spaces to break the words into arrays, but as some words you may want to use can contain white spaces we will use a symbol to break up each word.

The explode function allows you to split a string into arrays by stopping and splitting at each symbol or word you tell it to do.
PHP Code:
 $pieces explode("#"$word ); 

Now that we have used the explode function on our $word string, we have now stored the arrays into our $pieces variable. The example below illustrates that you will get the same words as stored in your previous $word string, however, now you can call each word using an array.
PHP Code:
 echo $pieces[0];
echo 
$pieces[1];
echo 
$pieces[2]; 

0 will be the word "spam", 1 will be the word "malware", and 2 will be the word "spyware".

We can pull out all words that we have stored using a loop. This will speed up the process when checking if a word is allowed.

PHP Code:
 //Let's shorten the referer name...
$refurl parse_url($_SERVER['HTTP_REFERER']);    
$referer $refurl['host']; 

$word "spam#malware#spyware";
$pieces explode("#"$word );

for (
$i 0$i<=3$i++) { //We have 3 words in our string
      
if (strpos($referer$pieces[$i])!==false){ //$i will contain the array number of the loop
      //Post comment
      
echo "We do not accept the refering url!";
      exit();
   } 



What we have told the script to do is search the string you have created, and if it encounters any of these words, we have told it to stop and block the user.

Instead of echoing out the result, we could also refer the user to another page and this can easily be done like this.

PHP Code:
 //Let's shorten the referer name...
$refurl parse_url($_SERVER['HTTP_REFERER']);    
$referer $refurl['host']; 
$word "spam#malware#spyware";
$pieces explode("#"$word );

for (
$i 0$i<=3$i++) { //We have 3 words in our string
      
if (strpos($referer$pieces[$i])!==false){ //$i will contain the array number of the loop
      //Redirect the user
      
header("Location: http://www.your-redirected-page.com/");
      exit();
   } 



I hope this will get you started.
Good luck!
__________________
Project manager
Visualizer Image Group


"Don't judge yourself on how far you have come,
It's better to get somewhere than nowhere"
Darren is offline  Live RSS Feed from my photo blog   Reply With Quote
Old 06-11-2007, 01:22 PM   #5
Hermit
Junior Member
 
Hermit's Avatar
 
Join Date: Feb 2006
Location: U.S.A., Ohio
Posts: 8

Default

I'll give this a try. Thank you Darren...
Hermit is offline   Reply With Quote
Old 06-11-2007, 02:04 PM   #6
Hermit
Junior Member
 
Hermit's Avatar
 
Join Date: Feb 2006
Location: U.S.A., Ohio
Posts: 8

Default Oh Yea

Hey Darren or anybody else. When you add a word such as "insurance" will this code fish this word out of say "autoinsurance" or do you need to put the whole word "autoinsurance" in the list?
and
If I wanted to send them back home would the redirect be "http://127.0.0.1" ?
You know let them bother themselves.

Last edited by Hermit : 06-11-2007 at 02:11 PM. Reason: More Questions
Hermit is offline   Reply With Quote
Old 06-11-2007, 03:24 PM   #7
Darren
VIG, Project Manager
 
Darren's Avatar
 
Join Date: Aug 2004
Location: US of A
Posts: 1,861

my photo blog, by Darren
Default

The code above does not support wildcard words (ie. *insurance, insurance*), so you will need to add the exact words you want to use.

If you are running this script from your own computer (with PHP installed) you can use http://127.0.0.1 or any ip address connected to the router, or http://localhost

If you want to refer to a website or webpage, point the location to the address

Hope this helps.
__________________
Project manager
Visualizer Image Group


"Don't judge yourself on how far you have come,
It's better to get somewhere than nowhere"
Darren is offline  Live RSS Feed from my photo blog   Reply With Quote
Old 06-11-2007, 03:27 PM   #8
Hermit
Junior Member
 
Hermit's Avatar
 
Join Date: Feb 2006
Location: U.S.A., Ohio
Posts: 8

Default

Quote:
If you are running this script from your own computer (with PHP installed) you can use http://127.0.0.1 or any ip address connected to the router, or http://localhost

If you want to refer to a website or webpage, point the location to the address

I wanted to send them back to thier own computers...
Hermit is offline   Reply With Quote
Old 06-11-2007, 03:40 PM   #9
Darren
VIG, Project Manager
 
Darren's Avatar
 
Join Date: Aug 2004
Location: US of A
Posts: 1,861

my photo blog, by Darren
Default

PHP Code:
 header("Location: " $_SERVER['HTTP_REFERER']); 

__________________
Project manager
Visualizer Image Group


"Don't judge yourself on how far you have come,
It's better to get somewhere than nowhere"
Darren is offline  Live RSS Feed from my photo blog   Reply With Quote
Old 06-11-2007, 04:00 PM   #10
Hermit
Junior Member
 
Hermit's Avatar
 
Join Date: Feb 2006
Location: U.S.A., Ohio
Posts: 8

Default

Great... Thanks Again
Hermit is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT. The time now is 10:07 AM.



Copyright © 2002-2006 Visualizer Image Group.