How to prevent a page from being copied ?

2008-04-30


How to prevent a page from being copied ?  There are many ways but normally people use blocking the right mouse click function and block text selection function . If the visitor tries to right click mouse and would be ready to select text on your page , he/she will see a error message , or nothing will happen. However, visitor still can get your page content by choosing IE menu item "View \ Page Source" or other else menu item. for this, we can encrypt all page text content, so the visitor can not get the original text page content if he wanna copy via "View \ Page Source".

Here I found a example from http://www.bluehostforum.com/archive/index.php/t-2796.html. I used it for my this website, my steps are :

1: Create a seperate PHP file named preventCopy.php:

_ /////////////Encripting code

function fwk_filter_encryptt($content) { $table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN OPQRSTUVWXYZ@"; $xor = 165;

// Prepare encoding table $table = array_keys(count_chars($table, 1)); $i_min = min($table); $i_max = max($table); for ($c = count($table); $c > 0; $r = mt_rand(0, $c--)) array_splice($table, $r, $c - $r, array_reverse(array_slice($table, $r, $c - $r)));

// Encode sequence $len = strlen($content); $word = $shift = 0; for ($i = 0; $i < $len; $i++) { $ch = $xor ^ ord($content[$i]); $word |= ($ch << $shift); $shift = ($shift + 2) % 6; $enc .= chr($table[$word & 0x3F]); $word >>= 6; if (!$shift) { $enc .= chr($table[$word]); $word >>= 6; } } if ($shift) $enc .= chr($table[$word]);

// Decode sequence $tbl = array_fill($i_min, $i_max - $i_min + 1, 0); while (list($k,$v) = each($table)) $tbl[$v] = $k; $tbl = implode(",", $tbl);

$fi = ",p=0,s=0,w=0,t=Array({$tbl})"; $f = "w|=(t[x.charCodeAt(p++)-{$i_min}])<<s;"; $f .= "if(s){r+=String.fromCharCode({$xor}^w&255);w>>=8;s-=2}else";

// Generate page $r = "<script language=JavaScript>"; $r.= "function decrypt_p(x){"; $r.= "var l=x.length,b=1024,i,j,r{$fi};"; $r.= "for(j=Math.ceil(l/b);j>0;j--){r='';for(i=Math.min(l,b);i>0;i--,l--){{$f}}document.write(r)}"; $r.= "}decrypt_p("{$enc}")"; $r.= "</script>"; return $r; }

ob_start("_fwk_filter_encryptt");

/////COPYRIGHT (No right clic)

echo "<SCRIPT type="text/javascript">\n"; echo "<!--\n"; echo "\n"; echo "var message="Atempting to copy copyrighted content.";\n"; echo "\n"; echo "///////////////////////////////////\n"; echo "function clickIE4(){\n"; echo "if (event.button2){\n"; echo "alert(message);\n"; echo "return false;\n"; echo "}\n"; echo "}\n"; echo "\n"; echo "function clickNS4(e){\n"; echo "if (document.layers||document.getElementById&&!document.all){\n"; echo "if (e.which2||e.which==3){\n"; echo "alert(message);\n"; echo "return false;\n"; echo "}\n"; echo "}\n"; echo "}\n"; echo "\n"; echo "if (document.layers){\n"; echo "document.captureEvents(Event.MOUSEDOWN);\n"; echo "document.onmousedown=clickNS4;\n"; echo "}\n"; echo "else if (document.all&&!document.getElementById){\n"; echo "document.onmousedown=clickIE4;\n"; echo "}\n"; echo "\n"; echo "document.oncontextmenu=new Function("alert(message);return false")\n"; echo "\n"; echo "//-->\n"; echo "</SCRIPT>\n\n";

//////Dont allow to select text

echo "<SCRIPT type="text/javascript">\n"; echo "<!--\n"; echo "\n"; echo "function disableselect(e){\n"; echo "return false\n"; echo "}\n"; echo "\n"; echo "function reEnable(){\n"; echo "return true\n"; echo "}\n"; echo "\n"; echo "//if IE4+\n"; echo "document.onselectstart=new Function ("return false")\n"; echo "\n"; echo "//if NS6\n"; echo "if (window.sidebar){\n"; echo "document.onmousedown=disableselect\n"; echo "document.onclick=reEnable\n"; echo "}\n"; echo "//-->\n"; echo "</SCRIPT>\n\n";_

2: Open my template folder and open index.php:

insert in the index.php, add at the beginning:

_<?php include("preventCopy.php"); ?>

Actually my index.php :

<?php //写中文为了存utf8 defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); // needed to seperate the ISO number from the language file constant _ISO $iso = explode( '=', _ISO ); // xml prolog echo '<?xml version="1.0" encoding="'. $iso[1] .'"?' .'>';

include("preventCopy.php");

?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> .... _

But, since my website is using UTF-8 encoding, this preventCopy.php is just for ISO-8859-1, so I can not use  "Encripting code" part, I have to comment out this part just keep right click bloking and select function blocking.