Have you ever wanted to secure links on your website (for example hide the real source of a file)? Here is a quick and easy way to do this.
<?php # File created on 11th February 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
## Start defining constants ##
define(RUN_ERRORS, TRUE); // Do you want the script to display errors? TRUE = yes you do.
define(redirect_or_echo, 'redirect'); // Do you want to redirect the user to another website, or just echo the other other webpages' content. 'rediect' will redirect, 'echo' will return the web pages constents. I recommend redirect.
## End defining constants ##
/* Start the link codes. The code is the ?code=123 part of the URL. The array should be fotmatted like:
$link['code'] = 'http://URL';
You may find it easier to do this with MySQL or including this as a seperate file. Too many links could lower performance, but for a small website just trying to cloak a few links this is good
*/
$link['1'] = 'http://www.site.com/';
// Start the system.
function external_url($url){
if($return = @file_get_contents($url)){
return $return;
}elseif(function_exists("curl_init")){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}elseif($return = @implode("", @file($url))){
return $return;
} else {
return NULL;
}
}
// Checks if the code is a number
if(is_numeric($_GET['code']) && is_array($link)){
if(isset($link[$_GET['code']])){
if(redirect_or_echo === 'redirect'){
header('location: '.$link[$_GET['code']]);
} elseif(redirect_or_echo === 'echo'){
echo external_url($link[$_GET['code']]);
}else{
if(RUN_ERRORS === TRUE){
echo 'Sorry, an internal error has occoured.';
}
}
} else {
if(RUN_ERRORS === TRUE){
echo 'Sorry, the code you have provided is incorrect.';
}
}
}else{
if(RUN_ERRORS === TRUE){
echo 'Sorry, the code you have provided is incorrect.';
}
}
/*
You are free to share, modify and use this code for commercial uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
You use this at your own risk.
*/
?>
Download the Code
Useful Links
PHP’s Offical Website – It has a fantastic documentation section. Well worth a look.
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
– The book I learnt PHP from, it’s really good for beginners and reference guide.
Posted February 14th, 2009
This is a really quick & easy piece of PHP code to count the page views. It’s not fantastic (It lacks any real tracking abilities), but for some things it does the trick.
The Theory
Every time someone views a page, PHP will increase a number stored in a file by 1. If we want to see how many people have view that page, we just check the file.
The Code
<?php # File created on 11th February 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
## Start defining constants ##
define(RUN_ERRORS, TRUE); // Do you want the script to display errors? TRUE = yes you do.
define(LOG_URL, '/home/user/logs/'); // Put the location of where you want to put the logs. Make sure this is absolute
$filename = rawurlencode(base64_encode($_SERVER['PHP_SELF'])); // Takes the filename then makes it network safe.
## End defining constants ##
## Parse filename ##
$filename = LOG_URL.$filename.'.log.txt';
##
// Check that the file exists, encase of the logs being removed or being invalid.
if(is_writable($filename) && is_readable($filename)){
// We should be able to read and write it
$file_contents = file_get_contents($filename);
// Check its a number, the logs could have been hacked at some point.
if(is_numeric($file_contents)){
$file_contents = $file_contents + 1; // Adds 1 to the current number in the log. $file_contents++; would also work.
file_put_contents($filename, $file_contents);
}else{
if(RUN_ERRORS === TRUE){
echo 'File ('.$filename.') contents is not numeric';
}
}
} else {
// We should create the file if we can.
if(is_dir(LOG_URL) && is_writable(LOG_URL) && !file_exists($filename)){ // Checks that the LOG_URL is a wriable directory, and the file does not already exist.
// lets create the file.
file_put_contents($filename, '1');
}else{
if(RUN_ERRORS === TRUE){
echo 'Cannot/write find directory/file ('.$filename.')';
}
}
}
/* Here is how to show how many people have view the file */
echo ('This file has been view by '.file_get_contents($filename).' people');
/*
You are free to share, modify and use this code for commerical uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
I claim no liability for this code, you use it at your own risk.
*/
?>
Download the Code
Useful Links
PHP’s Offical Website – It has a fantastic documentation section. Well worth a look.
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
– The book I learnt PHP from, it’s really good for beginners and reference guide.
Posted February 11th, 2009