Jragon Posted December 12, 2010 Share Posted December 12, 2010 Hello, My hit counter doesnt work, at all. It is ment to update hits.txt(add 1 to the number in there.), and desplay how many hits it has using gd2. Here is my code: <?php /** * @author Jragon * @copyright 2010 */ getHits() function getHits() { $file = "hits.txt"; $fh = fopen($file, "r"); $hits = fread($fh, filesize("$file")); fclose($fh); ++$hits; $fh = fopen($file, "w+"); fwrite($fh, "$hits"); fclose($fh); getImage("$hits"); } function getImage($string) { //Make $string to to a string $string = "$string"; //Define fontsize $font = 5; //use imagefontwidth to find out the widht of each charicter, //then times it by how many charicters theere are, //and add some padding. $width = imagefontwidth($font) * strlen($string) + 10; //height of a charicter, and add some padding. $height = imagefontheight($font) + 10; //random color thingy. $r = rand(25, 250); $g = rand(25, 250); $b = rand(25, 250); //create a blank image using the dimsions set above. $image = imagecreate($width, $height); //set the background color $background_color = imagecolorallocate($image, 0, 0, 0); //set the text color using the random integers made above. $text_color = imagecolorallocate($image, $r, $g, $b); //Put the text into the image. imagestring($image, $font, 6, 6, $string, $text_color); //Change the type of webpage so you see the image header("Content-type: image/png"); //put everything together and make the image imagepng($image); //free up some space on the temp drive imagedestroy($image); } ?> I have created hits.txt with the value of 0 inside of it. Thanks Jragon Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/ Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 Hi there, it looks like you have a missing ";" after the first getHits() function call. <?php /** * @author Jragon * @copyright 2010 */ getHits(); function getHits() { $file = "hits.txt"; $fh = fopen($file, "r"); $hits = fread($fh, filesize("$file")); fclose($fh); ++$hits; $fh = fopen($file, "w+"); fwrite($fh, "$hits"); fclose($fh); getImage("$hits"); } function getImage($string) { //Make $string to to a string $string = "$string"; //Define fontsize $font = 5; //use imagefontwidth to find out the widht of each charicter, //then times it by how many charicters theere are, //and add some padding. $width = imagefontwidth($font) * strlen($string) + 10; //height of a charicter, and add some padding. $height = imagefontheight($font) + 10; //random color thingy. $r = rand(25, 250); $g = rand(25, 250); $b = rand(25, 250); //create a blank image using the dimsions set above. $image = imagecreate($width, $height); //set the background color $background_color = imagecolorallocate($image, 0, 0, 0); //set the text color using the random integers made above. $text_color = imagecolorallocate($image, $r, $g, $b); //Put the text into the image. imagestring($image, $font, 6, 6, $string, $text_color); //Change the type of webpage so you see the image header("Content-type: image/png"); //put everything together and make the image imagepng($image); //free up some space on the temp drive imagedestroy($image); } ?> Do you have the same thing in your file? or is it a typo-mistake when you posted the code here? Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146117 Share on other sites More sharing options...
Jragon Posted December 12, 2010 Author Share Posted December 12, 2010 Why doesnt it show it on the error thingy, I use ubuntu now, and in XAMPP it showed all the errors. =S Also now it desplays a 0 then a strange symbolything. Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146136 Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 maybe your error reporting is set to off. Can you post an example, i tried your code and it is working on my PC. Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146286 Share on other sites More sharing options...
Jragon Posted December 12, 2010 Author Share Posted December 12, 2010 What sort of example? Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146339 Share on other sites More sharing options...
QuickOldCar Posted December 12, 2010 Share Posted December 12, 2010 I tried the code and works fine. But you had if was no value of at least 1 it would not work, so I added a small rule to be at least 1 hit. working example here: http://dynaindex.com/counter/ <?php /** * @author Jragon * @copyright 2010 */ getHits(); function getHits() { $file = "hits.txt"; $fh = fopen($file, "r"); $hits = fread($fh, filesize("$file")); if ($hits <= 1) { $hits =1; } fclose($fh); ++$hits; $fh = fopen($file, "w+"); fwrite($fh, "$hits"); fclose($fh); getImage("$hits"); } function getImage($string) { //Make $string to to a string $string = "$string"; //Define fontsize $font = 5; //use imagefontwidth to find out the widht of each charicter, //then times it by how many charicters theere are, //and add some padding. $width = imagefontwidth($font) * strlen($string) + 10; //height of a charicter, and add some padding. $height = imagefontheight($font) + 10; //random color thingy. $r = rand(25, 250); $g = rand(25, 250); $b = rand(25, 250); //create a blank image using the dimsions set above. $image = imagecreate($width, $height); //set the background color $background_color = imagecolorallocate($image, 0, 0, 0); //set the text color using the random integers made above. $text_color = imagecolorallocate($image, $r, $g, $b); //Put the text into the image. imagestring($image, $font, 6, 6, $string, $text_color); //Change the type of webpage so you see the image header("Content-type: image/png"); //put everything together and make the image imagepng($image); //free up some space on the temp drive imagedestroy($image); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146368 Share on other sites More sharing options...
QuickOldCar Posted December 12, 2010 Share Posted December 12, 2010 I changed the code more, this one def works even if is no value in hits.txt <?php /** * @author Jragon * @copyright 2010 */ getHits(); function getHits() { $file = "hits.txt"; $fh = fopen($file, "r"); $hits = @fread($fh, filesize("$file")); fclose($fh); if ($hits <= 0){ $hits =1; } else { ++$hits; } $fh = fopen($file, "w+"); fwrite($fh, "$hits"); fclose($fh); getImage("$hits"); } function getImage($string) { //Make $string to to a string $string = "$string"; //Define fontsize $font = 5; //use imagefontwidth to find out the widht of each charicter, //then times it by how many charicters theere are, //and add some padding. $width = imagefontwidth($font) * strlen($string) + 10; //height of a charicter, and add some padding. $height = imagefontheight($font) + 10; //random color thingy. $r = rand(25, 250); $g = rand(25, 250); $b = rand(25, 250); //create a blank image using the dimsions set above. $image = imagecreate($width, $height); //set the background color $background_color = imagecolorallocate($image, 0, 0, 0); //set the text color using the random integers made above. $text_color = imagecolorallocate($image, $r, $g, $b); //Put the text into the image. imagestring($image, $font, 6, 6, $string, $text_color); //Change the type of webpage so you see the image header("Content-type: image/png"); //put everything together and make the image imagepng($image); //free up some space on the temp drive imagedestroy($image); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146388 Share on other sites More sharing options...
Jragon Posted December 13, 2010 Author Share Posted December 13, 2010 Is that my code on the website? Thanks=P Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146798 Share on other sites More sharing options...
QuickOldCar Posted December 14, 2010 Share Posted December 14, 2010 Well actually I use this, I also have another post counter that uses a mysql db and different, if needed that just ask, because can call for the highest votes and all that jazz. Your code was just basic file read, and a little gd, very common stuff. <?php /* name this file index.php and place it into a folder called counters the function will create a unique text file for each unique item, good for posts or pages and store all the files in the counters folder For the total website views,add this to your header file or top of your page <?php include('counters/index.php'); $website_view_url = "http://".$_SERVER['HTTP_HOST']; } $website_views = getViews("$website_view_url"); echo "<br />".$website_views."<br />"; ?> For the total pages or scripts views,add this to your header file or top of your page <?php include('counters/index.php'); $total_page_view_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; } $total_page_views = getViews("$total_page_view_url"); echo "<br />".$total_page_views."<br />"; ?> For entire urls including queries,add this to your header file or top of your page <?php include('counters/index.php'); $page_queries_view_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; if (!empty($_SERVER["QUERY_STRING"])) { $page_queries_view_url .= "?".$_SERVER['QUERY_STRING']; } $page_queries_views = getViews("$page_queries_view_url"); echo "<br />".$page_queries_views."<br />"; ?> for usage in posts like by id: if do not want the pages counted and did not do the above code,include the below somewhere near the beginning of your page <?php include('counters/index.php');?> Then in the posts loop, you can associate your $row['id']; or some other unique value <?php $post_views = $row['id']; $post_views = getViews("$post_views"); echo "<br />Post Views ".$post_views."<br />"; ?> if would like to combine any the above or use them all, just be sure to just include include('counters/index.php'); only one time */ function getViews($views_count_value) { $views_count_value = md5("$views_count_value"); $views_count_file_name = "counters/$views_count_value.txt"; if (!file_exists($views_count_file_name)) { $views_count =0; $file_handle = fopen($views_count_file_name, 'w') or die("can't open file"); fwrite($file_handle, "$views_count"); fclose($file_handle); } $file_handle = fopen($views_count_file_name, "r"); $views_count = fread($file_handle, filesize("$views_count_file_name")); fclose($file_handle); if ($views_count <= 0){ $views_count =1; } else { ++$views_count; } $file_handle = fopen($views_count_file_name, "w+"); fwrite($file_handle, "$views_count"); fclose($file_handle); return("$views_count"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221381-hit-counter-not-desplying-anything-s/#findComment-1146970 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.