Display & Save Visitor’s Ip – PHP Code
found a blog post which tested and working very good
PHP-Hypertext Preprocessor.
PHP is a server side script language.Using php you can create a dynamic websites.
This is the first lesson and on this lesson you will learn how to get visitors ip and save it on your server.(i wil learn you how add this to your blog or any web sites that haven’t php.)
-Display User’s informations-
<?php
$ip = $_SERVER[‘REMOTE_ADDR’];
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER[‘HTTP_USER_AGENT’];
$referred = $_SERVER[‘HTTP_REFERER’]; // a quirky spelling mistake that stuck in php
print “<strong>Display IP address:</strong><br />\n”;
print “$ip<br /><br />\n”;
print “<strong>More detailed host address:</strong><br />\n”;
print “$hostaddress<br /><br />\n”;
print “<strong>Display browser info</strong>:<br />\n”;
print “$browser<br /><br />\n”;
print “<strong>Where you came from (if you clicked on a link to get here</strong>:<br />\n”;
if ($referred == “”) {
print “Page was directly requested”;
}
else {
print “$referred”;
}
?>
-Code Descriptions-
<?php -to start php file.
$ip – give a variable name to $_SERVER[‘REMOTE_ADDR’] (like y=mx+c).
$_SERVER[‘REMOTE_ADDR’]-this is the code that get user ip.
gethostbyaddr($ip) -to get host name.
$_SERVER[‘HTTP_USER_AGENT’]-get browser information.
$_SERVER[‘HTTP_REFERER’]-get where user come from.
//-we use this to put a comment in our php file.// for 1 line comment.
/*-this is for long comment and end with */
print”what you want to display on the page“;-we use this to display something on the page.
<br>-this is for line break .when we use this code.the text after this code will display in next line.
How i save user’s Informations ??
<?php
$myFile = “countip.mic”;
$fh = fopen($myFile, ‘a+’) or die(“can’t open file”);
$stringData =”Date:”.date(‘Y-m-d H:i:s’).”<br/>Ip:”.$_SERVER[‘REMOTE_ADDR’].”<br/>Browser:”.$_SERVER[‘HTTP_USER_AGENT’].”<br/>\n” ;
fwrite($fh, $stringData);
fclose($fh);
?>
-Code Descriptions-
$myfile=”countip.mic”; -$myfile is the variable that for “countip.mic”.
fwrite($fh,$stringData); -do that discribe on $fh variable and $stringData .(create countip.mic file and save data).
fclose($fh);-close the file that we open.
we get the date time to countip file using .date(‘Y-m-d H:i:s).code.
Code for Display user’s information and Save(broth in one file) .
<?php
$ip = $_SERVER[‘REMOTE_ADDR’];
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER[‘HTTP_USER_AGENT’];
$referred = $_SERVER[‘HTTP_REFERER’]; // a quirky spelling mistake that stuck in php
print “<strong>Display IP address:</strong><br />\n”;
print “$ip<br /><br />\n”;
print “<strong>More detailed host address:</strong><br />\n”;
print “$hostaddress<br /><br />\n”;
print “<strong>Display browser info</strong>:<br />\n”;
print “$browser<br /><br />\n”;
print “<strong>Where you came from (if you clicked on a link to get here</strong>:<br />\n”;
if ($referred == “”) {
print “Page was directly requested”;
}
else {
print “$referred”;
}
?>
<?php
$myFile = “countip.mic”;
$fh = fopen($myFile, ‘a+’) or die(“can’t open file”);
$stringData =Date:”.date(‘Y-m-d H:i:s’).”<br/>Ip:”.$_SERVER[‘REMOTE_ADDR’].”<br/>Browser:”.$_SERVER[‘HTTP_USER_AGENT’].”<br/>\n” ;
fwrite($fh, $stringData);
fclose($fh);
?>
How I put this code to my website ??
First i will learn you how to add this code to your hosting that php working.
When you add this php codes to your blog or web site you must have another hosting that php working.Many hosting providers give free php working accounts.
First you must get a free hosting account.
(click here to google search about free accounts.then select one and Register on it(eg:www.000webhost.com )).
1.Create file on your desktop with .php extension (eg:countip.php) .
2.Copy the code and paste it on your file.
3.Save it and Upload it(your file) to you hosting account.
To download php zip file click here.(extract zip fle and upload it)
(To Upload Your file to your hosting account .you can use Filezilla software).
Then go to your file (after you upload it) using a browser.You can see your informations on your page.
Click here to see preview. And click here to see the save file on the server.
Coutesy: http://webdevelopstepbystep.blogspot.com/2011/08/display-save-visitors-ip-php-code-1.html