June 26, 2006 at 3:37 pm by TwiRp Graphical Counter

What's Needed

All we need is PHP and MySQL.
Files We Will Make

  • count.php
  • image.php

We could do it with one file, but I think it's easier to understand with 2 different files. Plus, if you want to have different images, the count.php will log the stuff, and image.php will display the information.

The SQL Query

Creating the table. We are going to try to keep it as simple as posible.

SQL:
  1. CREATE TABLE counter(
  2. logging VARCHAR(5) NOT NULL,
  3. number INT(5) NOT NULL,
  4. ipaddr TEXT NOT NULL,
  5. daynum INT(2) NOT NULL,
  6. PRIMARY KEY(logging)
  7. );


This creates the table we will use. logging will store if it's storing information for today or total hits, and the ipaddr will store the IP of the visitors, number is the number of hits, and daynum is the number of the day.
Now we need to insert some more information in the database.

SQL:
  1. INSERT INTO counter (logging,number) VALUES ('today','0');

SQL:
  1. INSERT INTO counter (logging,number) VALUES ('total','0');

This just creates the 2 storage location in the table we will use.

Count.php

Now we need the file that will store the information. Hopefully you understand the internal documentation.

PHP:
  1. // Database Information, Host, User, Password, and Database
  2. $DBHost = 'localhost';
  3. $DBUser = 'user';
  4. $DBPass = 'pass';
  5. $DB = 'database';
  6.  
  7. // Now to connect
  8. mysql_connect($DBHost, $DBUser, $DBPass);
  9.  
  10. // Select today's information
  11. $q = mysql_query("SELECT * FROM counter WHERE logging='today'");
  12.  
  13. // Get the total inforamtion
  14. $tq = mysql_query("SELECT * FROM coutner WHERE logging='total'");
  15. $ti = mysql_fetch_array($tq);
  16. $total = $ti['number'];
  17.  
  18. // Check to see if it's storing todays information
  19. $date = date("d");
  20. if($i['daynum']==$date){
  21. // Since it is, we get the current information
  22. $today = $i['number'];
  23. $ips = explode("|",$i['ipaddr']);
  24. // Now to see if we need to increment the counter.
  25. if(!in_array($_SERVER['REMOTE_ADDR'],$ips)){
  26. $today++;
  27. $total++;
  28. // Add the IP to the IP List
  29. $nip = $i['ipaddr'].'|'.$_SERVER['REMOTE_ADDR'];
  30. // Update the Table
  31. $up = mysql_query("UPDATE counter SET number='".$today."', ipaddr='".$nip."' WHERE logging='today'");
  32. $up2 = mysql_query("UPDATE counter SET number='".$total."' WHERE logging='total'");
  33. }
  34. }else{
  35. // It's a new DAY!...?
  36. // So update the list, how many hits today, and the total.
  37. $nip = $_SERVER['REMOTE_ADDR'];
  38. $today = 1;
  39. $total++;
  40. $up = mysql_query("UPDATE counter SET number='".$today."', ipaddr='".$nip."', daynum='".$date."' WHERE logging='today'");
  41. $up2 = mysql_query("UPDATE counter SET number='".$total."' WHERE logging='total'");
  42. }
  43.  
  44. // End the count.php
  45. // Today and Total are set and done.

So now the $today and $total variables are set. We will need those when we include count.php in image.php

The Image

So, first off, we need to pick an image.

I chose not just because I didn't feel like making a new one for the tutorial, but becuase of it's coloration...?

So anyways, we open it up in our favorite image editor, I picked MS Paint...

Put the cursor where you want the text to be (the numbers or whatever). When I did it, the location was 30, 5. But then after coding it, you may have to change the number a bit. I finished with 30, 3.

Image.php

Image.php is rather easy to make. We have count.php and the numbers for positioning the text.

PHP:
  1. // Include count.php to log and set the variables
  2. include("count.php");
  3. // Set our header, mine was a png, so i set image/png
  4. // jpeg/jpg - image/jpeg
  5. // gif - image/gif
  6. header("Content-type: image/png");
  7. // Create our image from our image...
  8. // change png to whatever the image type is.
  9. $image = imagecreatefrompng("blue_orange.png");
  10. // Pick a color, 225 is the red, 154 is green, and 10 is blue
  11. $text = imagecolorallocate($image,255, 154, 10);
  12. // Put a string, notice the 30, 3.
  13. $write = imagestring($image, 1, 30,3, $today."/".$total,$text);
  14. // Output the image, mine is png so yeah...
  15. imagepng($image);

And we wind up with:



Post a Comment

*
* (hidden)

  Layout composed by a TwiRp.  Brushes from blindingLight.  Menu from andrewSellick.  Powered by wordPress.
  (close) (open)   RSS  Atom.