Well, first off, i am php, newcomer at T-I. Thought i would start my life here with my first tutorial on making your own php page views image counter.
So, basically, two files are needed :
one - views.php
two - show_user_an_image.php
usage - put this in html page : <img src="views.php?page=page1" alt="counter" >
now, here is the code (comments in php fle explain the code) :
views.php
PHP Code:
<?php
/*
* Name : views.php
* Usage : <img src="views.php?page=pagenamehere" alt="Views">
* Require: show_user_an_image.php
* Author : php
* Mail : phpl0v3r@gmail.com
* Note : Feel free to email me any issues you got.
*/
// image display part
require("show_user_an_image.php");
// the page views counter part
if(isset($_GET["page"])){
$page = $_GET["page"]; // $_GET["page"] actually is webpage name
$filename = urlencode($page).".txt"; // lets choose a filename to store hits in
if(file_exists($filename)){
$views = file_get_contents($filename); // get views count
$views++; // increment it
file_put_contents($filename, $views); // put the new count back in the file
show_user_an_image("Page Views : ".$views); // return user the image showing the page views
}else{ // there is no file
$views = 1;
file_put_contents($filename, $views); // store 1 in new file
show_user_an_image("Page Views : ".$views); // return user the image showing page has 1 view
}
}
?>
show_user_an_image.php
PHP Code:
<?php
/**
* Name : show_user_an_image.php
* Usage : show_user_an_image("Data here");
* Author : php
* Mail : phpl0v3r@gmail.com
* Note : Feel free to email me any issues you got.
*/
function show_user_an_image($parameter){
// configure variables for image creation
$imagewidth = '200';
$imageheight = '100';
$image = imagecreate($imagewidth,$imageheight);
$background = imagecolorallocate($image,255,255,255); // here, (255,255,255) refers to (r,g,b) value of white
$wantborder = true;
if($wantborder){
$borderthickness = 1;
$border = @imagecolorallocate($image,0,0,0); // here, (0,0,0) refers to (r,g,b) value of black
@imagerectangle($image,0,0,$imagewidth-$borderthickness,$imageheight-$borderthickness,$border);
}
$textcolor = imagecolorallocate($image,0,0,255); // here, (0,255,0) refers to (r,g,b) value of blue
$text = $parameter;
$fontsize = 3;
$fontwidth = imagefontwidth($fontsize);
$fontheight = imagefontheight($fontsize);
$textwidth = $fontwidth * strlen($text);
$textheight = $fontheight;
$horizontalcenter = ceil(($imagewidth - $textwidth) / 2);
$verticalcenter = ceil(($imageheight - $textheight) / 2);
$pastestringonimage = imagestring($image,$fontsize,$horizontalcenter,$verticalcenter,$text,$textcolor);
header("Content-type: image/png");
imagepng($image);
}
?>
Any doubt in the above code, please feel free to email at my email.
I will always be there to help you out with php ;)