Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] How to grab IP adress with PHP
#1
Hello Support Forums,

In this tutorial I will show you how to grab someone's IP Adress when they visit a website.


The variable to use is $_SERVER['REMOTE_ADDR'] - It's that simple. You can use it for just about anything, here are a few examples.

Printing the Users IP Address:

PHP Code:
<?php 
print ($_SERVER['REMOTE_ADDR'], "I'm Watching You!"); 
?>

Printing it to a File:

PHP Code:
<?php 
$ip 
$_SERVER['REMOTE_ADDR']; 
$handle fopen('ipaddresses.txt'', 'a+); 
fwrite($handle$ip); fwrite($handle"\n"); 
fclose($handle); 
?>

Now, you have to make a new text file and insert any of these codes or your own and save it as "anything.php".

Good luck. Smile

-Risto
Reply
#2
yes this could save a lot of hassle - i was looking for something similar - thanks!
Verum quaere et insaniam inveni
Reply
#3
Good and easy....
But you should really remove the use of the second fwrite()

PHP Code:
fwrite($handle"{$ip}\n"); 
Reply
#4
(01-03-2010, 09:40 AM)Master of The Universe Wrote: Good and easy....
But you should really remove the use of the second fwrite()

PHP Code:
fwrite($handle"{$ip}\n"); 

does the {} get printed also? i mean wouldnt the best way just be fwrite($handle, $ip . "\n");
[Image: sig.php]
Reply
#5
(01-03-2010, 09:31 PM)Nyx- Wrote: does the {} get printed also? i mean wouldnt the best way just be fwrite($handle, $ip . "\n");

No...
That's useful when you want to use a variable within a string....

PHP Code:
echo "This will print {$var}s and add s on the end :D..."

But your way is also fine, I just like the looks of {}..
Reply
#6
nice and easy tutorial, I use a similar one to check logs on my website.
Reply
#7
Code:
$handle = fopen('ipaddresses.txt'', 'a+);

should be

Code:
$handle = fopen('ipaddresses.txt', 'a+');
<Aoi-chan> everyone's first vi session. ^C^C^X^X^X^XquitqQ!qdammit[esc]qwertyuiopasdfghjkl;:xwhat
Reply
#8
(01-12-2010, 07:02 AM)Psycho Wrote:
Code:
$handle = fopen('ipaddresses.txt'', 'a+);

should be

Code:
$handle = fopen('ipaddresses.txt', 'a+');

I can't edit my thread anymore. Anyway, thank you. Smile
Reply
#9
How would you exclude a specific IP from being detected?
GamerCraft

Visit it, and tell me what you think. Tongue
Reply
#10
Good TUT bro
thanks
If you are willing to join SF Webmasters.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 773 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,262 10-10-2011, 01:00 PM
Last Post: Greyersting
  Tim's PHP 101 tutorial Tim 22 5,180 10-19-2009, 02:12 PM
Last Post: Tim
  PHP mysql Login Tutorial By MAcar V1.0 MAcar 8 3,462 10-08-2009, 04:57 PM
Last Post: MAcar
  PHP site backup tutorial andrewjs18 6 1,819 10-07-2009, 11:27 PM
Last Post: andrewjs18

Forum Jump:


Users browsing this thread: 1 Guest(s)