PDA

View Full Version : PHP Snippets



Steven.
05-06-2005, 02:26 AM
Hello!

this is a Simple guide and list of PHP snippets, Sorry if it has been Posted Before.

Hit Counter:
Create a file and name it count.dat, CHMOD it to 777
<?php
$counterfile = "count.dat";
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");
$count = (int) fread($fp, 20);
fclose($fp);

Display users IP address:
<?php echo $_SERVER['REMOTE_ADDR']; ?>

Lines in a file:
<?php
$filename = "yourfile.txt";
$lines = file($filename);
$count = count($lines);
echo("There are $count lines in the file.");
?>

Show referer:
<?php echo $_SERVER['HTTP_REFERER']; ?>

Server path:
<?php echo getcwd(); ?>

IP Logger:
Create and upload a file called "ips.txt" CHMOD the file to 777
<?php
$v_ip = $REMOTE_ADDR;
$v_date = date("l d F H:i:s");
$fp = fopen("ips.txt", "a");
fputs($fp, "IP: $v_ip - DATE: $v_date\n\n");
fclose($fp);
?>

24hr Clock:
<?php echo date("H:i:s"); ?>

Display page load time:
<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
for ($i=0; $i <1000; $i++){
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
$time = round($time,4);
echo "This page loaded in $time seconds.";
?>

24Hr clock and Day:
<?php echo date("l g:ia"); ?>

Display users browser info:
<?php echo $HTTP_USER_AGENT ?>

Full date and time:
<?php echo date("l d F Y"); ?>

Multi ban IP Address's:
<?php $ip = getenv('REMOTE_ADDR');
$ip1 = "xxx.xxx.xxx";
$ip2 = "xxx.xxx.xxx";
if($ip == $ip1 || $ip == $ip2)
{
echo"You are banned from this site.";
exit ();
}
?>

Page include:
<?php include "PAGEHERE.php"; ?>

I Hope this will help. Any Questions feel free to Post them here or PM Me.

Sorry again if this has already been done.

iRoss
05-06-2005, 08:25 AM
This is rather good. :) +Rep For You :P

Anderman
05-06-2005, 08:35 AM
Pfft,
Theyve just been copied + pasted off a site

Mentor
05-06-2005, 10:03 AM
Pfft,
Theyve just been copied + pasted off a site
oviosly. if he wrote them he may have noticed some of the ovios errors in some of the code?
how is a counter supposed to count when it just reads the file? plus it doesnt even display the result "/

Steven.
05-06-2005, 11:41 AM
Thank's simplesimon12 :)

Mentor: This is just a general guide to the php script'ing language, if you didnt like it, maybe help me? im new to it myself, and that's what i have picked up, if you dont like the way i trained in it.. Fine.. couldnt affect me.

Mentor
05-06-2005, 02:01 PM
you didnt even close it ?> so its ovioly copy and paste becuse u missed half of it "/
Copy and pasteing scripts is not what i call trained in somthing "/

anyway a hit counter would look more like this


<?php
$document = ("hitpage.txt");
$hits = file($document);
$hits[0] ++;
$fp = fopen($document , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>

then hitpage or what evr u want to go there. woudl need to be chmoded to 777.
a "working" flat file counter.

(this is the perfect opitunity for me to have made a mistake in my codeing)

iRoss
05-06-2005, 02:19 PM
Rofl. Obviously he didn't write them :I

Luckyrare
05-06-2005, 03:25 PM
I wouldnt try fight against Mentors php skills :P

BlueTails
05-06-2005, 04:27 PM
No-One In Thier Right Minds Would

:Blob
05-06-2005, 04:29 PM
No-One In Thier Right Minds Would
In there right minds, but how about their lefts?

Want to hide these adverts? Register an account for free!