Page 1 of 1

Basic yet usefull codes!

Posted: Sat May 30, 2009 4:18 am
by iFlex
Ok theese might not be the most complex codes arround but as they say less is more... Please post improvments if needed

Time

Code: Select all

<?php echo date("H:i"); ?>
Date

Code: Select all

<?php echo gmdate("M d Y"); ?>
Hit Counter
Ok put this code into your PHP document:

Code: Select all

<?php
$counter_file = "count.dat";
if(!($fp = fopen($counter_file, "r"))) die ("Cannot open $counter_file.");
$counter = (int) fread($fp, 20);
fclose($fp);
 
$counter++;
 
echo "You're visitor No. $counter.";
 
$fp = fopen($counter_file, "w");
fwrite($fp, $counter);
fclose($fp);
 
?>
Now make a file called 'count.dat' and upload it to the same directory as the file with the php code above. Then CHMOD the counter file to 777.

Now this REALY basic but its usefull if you need to find info on your host but they dont tell you it...

Code: Select all

<?php phpinfo(); ?>
There you go :wink:

Re: Basic yet usefull codes!

Posted: Sat May 30, 2009 3:43 pm
by Christopher
Thanks. These are great basic examples that people may find useful.

You might want to edit your post to have the Time and Date examples to show what the output would be. It also might be handy to give links to the online documentation for date() and gmdate(). You don't explain why you don't use date() for both?

Also for the hit counter example, it would probably better to put the code in a function to keep the namespace clear because someone may be using the variables $counter or $fp. Those are common names.