Basic yet usefull codes!

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
iFlex
Forum Commoner
Posts: 41
Joined: Sat May 30, 2009 3:44 am

Basic yet usefull codes!

Post 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:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Basic yet usefull codes!

Post 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.
(#10850)
Post Reply