Very Basic Counter
Posted on April 18th, 2007 at 9:46 pm by Andrew
I think everyone starts PHP this way and I remember the satisfaction it gave me when I first used this (back on AlienActivity.com), it's just a nice nifty little tool to get you started on PHP and it's something many sites wouldn't be without!
To keep it simple I've chosen just to use text files rather than databases, you'll need to CHMOD the file you store your counts in to 777. Here goes, once again it's a fairly self-explanatory script that I'll just annotate in-line.
PHP:
-
<?
-
$file = "counts.inc"; // Where we store the counting
-
$open=
fopen($file,
'r');
// Open $file for reading
-
-
$counts=$counts+1; // Add 1 to the current counter value
-
echo $counts;
// Print this integer.
-
fclose($open);
// Close the file (saves memory, stops eventual lagging).
-
$open2=
fopen($file,
'w');
// Open $file for writing
-
fwrite($open2,
$counts);
// Save the new counter value
-
fclose($open2);
// Close the file (saves memory, stops eventual lagging).
-
-
/* It's worth noting that it is in fact necessery to close the file and re-open it in order to write the incremented count, I originally wrote this script without doing so and discovered an error where it counted up in unuaul integers of 1, 2, 13, 1214, 12131215, for more info please read here: http://www.moon-hosting.co.uk/php/tutorials/PHP_Lecture4 _Files.doc */
-
?>
How easy was that?!
Demo.
Simple PHP text only image
Posted on April 18th, 2007 at 7:33 pm by Andrew
In this tutorial I will be teaching you to make a really basic text on plain/no background system. I'll be using the GD library so check you have that installed ;).
Simple enough idea, you can allow users to edit the text appearing on the image with GET details. The idea is that you can adapt this simple script into your own site. It's been coded for Bozebo so he can use it to put emails in profiles into images and stop bots from grabbing. It can be adapted to take data from a database and display it, you could even make an image verification system with it!
I'm not going to go through it line by line, instead I'll just annotate it as I go. Enjoy...
PHP:
-
<?
-
-
header("Content-type: image/png");
// Tell the browser it's an image, not a .php file.
-
-
$text =
stripslashes($_GET['text']);
// Take the text and stop it from messing up with '
-
-
$number =
strlen($text);
// Count the chars in the text.
-
-
$number = ($number*7)+9; // Times this by 7 and add 9 to make it look the right size.
-
-
$im = imagecreate($number, 24); // Create the basic size, $number x 24
-
-
$colour1 = imagecolorallocate($im, 0, 0, 0); // Background (RGB) black.
-
-
$colour2 = imagecolorallocate($im, 255, 255, 255); // Background white.
-
-
imagestring($im, 3, 5, 5, $text, $colour2); // Add the text to the image.
-
-
imagepng($im); // Create and display the image.
-
-
imagedestroy($im); // Cast the image from the server (will cause lag eventually if you don't).
-
-
?>
Demo.