Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2005, 05:14 AM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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 Code:
<?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 Code:
<?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.

Last edited by Element; 12-01-2005 at 05:29 AM..
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:37 PM.


Advertisement
Log in to turn off these ads.