Helpful Information
 
 
Category: Post a PHP snippet
FUNCTIONS: Dynamic URL Linking with Hit Counter

This code is just for links you want hit counters on like, maybe your image gallery link, or code gallery, forum, wallpapers, multimedia, etc. Even affiliates if you don't care to use a affiliate program.

Running Example: http://urlmask.com/links/

You can also change jump.php to link to the same page they're on since the jump.php is included at the top and can forward them if the url and name is found in the URI.

jump.php


<?php

function count_url($name, $url) {
$dbpath = "/home/user/public_html/links/folder/";
if(file_exists($dbpath . strtolower($name) . ".dat")) {
$url_data = file($dbpath . strtolower($name) . ".dat");
$explode = explode("|", array_pop($url_data));
$count = $explode[2]+1;
$data = $url . "|" . $name . "|" . $count;
if(!($handle = fopen($dbpath . strtolower($name) . ".dat", "w"))) {
die("Could not open file for writing!");
}
if(!(fwrite($handle, $data))) {
die("Could not write to file!");
}
fclose($handle);
@header("Location: $url");
} else {
die("This user is not in our database! Use <b>show_url()</b> to create and display a URL!");
}
}


function show_url($name, $url, $break) {
$dbpath = "/home/user/public_html/links/folder/";
if($break == 0) { $break = "<br />\n"; } // URL format 1
elseif($break == 1) { $break = ",&nbsp;&nbsp;"; } // URL format 2
elseif($break == 2) { $break = ""; } // URL format 3
else { $break = ""; } // Default (basically format 3)
if(file_exists($dbpath . strtolower($name) . ".dat")) {
$url_data = file($dbpath . strtolower($name) . ".dat");
$explode = explode("|", array_pop($url_data));
if($explode[0] !== null || $explode[1] || $explode[2]) {
echo ("<a href=\"http://example.com/links/jump.php?url=" . $explode[0] . "&name=" . $explode[1] . "\" target=\"_blank\" title=\"" . $explode[1] . " Hit(s): " . $explode[2] . "\">" . $explode[1] . "</a> Hit(s): <b>" . number_format($explode[2]) . "</b>" . $break);
} else {
return false;
}
} else {
if(!($handle = fopen($dbpath . strtolower($name) . ".dat", "a+"))) {
die("Could not create file!");
}
$data = $url . "|" . $name . "|0";
if(!(fwrite($handle, $data))) {
die("Could not right to file!");
}
fclose($handle);
echo ("<a href=\"http://example.com/links/jump.php?url=" . $url . "&name=" . $name ."\" target=\"_blank\" title=\"" . $name . " Hit(s): 0\">" . $name . "</a> Hit(s): <b>0</b>" . $break);
}
}

if(isset($_GET['name']) || isset($_GET['url'])) {

count_url($_GET['name'], $_GET['url']);

}

?>


index.php


<?php

include_once("jump.php");


echo "<p>Dynamic URL Linking and Counting Example: Break is set to 0.</p>";
echo "<p>";
show_url("URLmask", "http://urlmask.com/", 0); // Adds a <br> and \n
show_url("Coding Forums", "http://codingforums.com/", 0);
show_url("Shack Cafe", "http://shack-cafe.com/",0);
echo "</p>";
echo "<p>Break is set to 1</p>";
echo "<p>";
show_url("URLmask", "http://urlmask.com/", 1); // Add a comma and spaces.
show_url("Coding Forums", "http://codingforums.com/", 1);
show_url("Shack Cafe", "http://shack-cafe.com/", 2); // Default, used for the end of format 2 or just for a plain link and hit count without linebreaks or commas.
echo "</p>";
echo "<p>Break is set to 2</p>";
echo "<p>";
show_url("URLmask", "http://urlmask.com/", 2); // Adds nothing.
show_url("Coding Forums", "http://codingforums.com/", 2);
show_url("Shack Cafe", "http://shack-cafe.com/", 2);
echo "</p>";


?>


I hope it handy to someone.










privacy (GDPR)