Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Store IP into a .txt file

Status
Not open for further replies.
Experienced Elementalist
Joined
Jul 19, 2008
Messages
270
Reaction score
7
Simple function, now I want a function to store $ip into a text file on a new line, without overwriting.

Code:
<?
$ip = $_SERVER['REMOTE_ADDR'];
$opslagip = "$ip \r\n";

Echo "$ip";

?>
 
Newbie Spellweaver
Joined
May 1, 2008
Messages
8
Reaction score
0
PHP:
<?php
$file = fopen ('ip.log', 'a+');
fwrite($file, $_SERVER['REMOTE_ADDR']."\r\n")
?>
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
Code:
Echo "$ip";

I have often wondered why people prefer to put variables inside strings, rather then concatenating them, but this is ridiculous. Remove the quotes please, since they have no single purpose but to slow down the code.
 
Experienced Elementalist
Joined
Jul 19, 2008
Messages
270
Reaction score
7
I'll use quotes whenever I want?

Anyway, I made a better one with a new function I found on php.net, it's a new php 5 ability, it combines open, write and close.

Code:
<?
$ip = $_SERVER['REMOTE_ADDR'] . PHP_EOL;
file_put_contents("logger.txt", "$ip", FILE_APPEND);
echo "Your ip $ip has been logged."
?>

Just a short description of the file_put_contents.
use it as
Code:
file_put_contents("storage file", "storage variable", Extra's);
Also use FILE_APPEND if you don't want the function to overwrite the file everytime somebody activates it again.
 
Banned
Banned
Joined
Jun 24, 2008
Messages
723
Reaction score
1
This is how I do mine:

PHP:
$ip = $_SERVER['REMOTE_ADDR'];
$file = "ips.txt"; //Select file
$file = fopen($file, "a"); //Appened file
$data = "<b>IP</b>: $ip<br>";
fwrite($file, $data); //Write data to file
fclose($file); //Close the file
echo "Your IP adddress <b>$ip</b> has been logged :D";
?>

Create a file called "ips.txt" and upload it to your host, then set the CHMOD file Attributes to 777 which will allow all things to be done.

Then create a file called "view.php"

And put this code in:

PHP:
include "ips.txt";

And upload it to your webhost.
 
---
Loyal Member
Joined
Aug 18, 2004
Messages
641
Reaction score
3
This is a better way to get IP address

PHP:
    if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
    else $ip = "0.0.0.0";

it will also check if user is under a proxy.
 
Initiate Mage
Joined
Dec 21, 2014
Messages
3
Reaction score
0
please can tel me the code to check against a log file for allready entered ip address below is the code i have to log the ip

<?php
$iplogfile = '../logs/ip-address-contactform.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('m/d/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n");
fclose($fp);
?>
 
◝(⁰▿⁰)◜Smile◝ (⁰▿⁰)◜
Developer
Joined
May 29, 2007
Messages
2,167
Reaction score
899
You should implement some checks and then you're good to go with the code provided by tim.
 
Initiate Mage
Joined
Dec 21, 2014
Messages
3
Reaction score
0
i do not know how to check against a log file please help it took me so long just to do that bit lol espcialy the part to add to a new line in txt file lol was so simple but hard if you dont know what u doing lol
 
Last edited:
Initiate Mage
Joined
Dec 21, 2014
Messages
3
Reaction score
0
this is what i ended up using as dont know what code is to chmod so have to manually make file ip-address-mainsite.txt and its chmod is 0777 it works mint thank you heaps

<!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To logs/ip-address-mainsite.txt -->

<?php
$iplogfile = 'logs/ip-address-mainsite.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];

//load the file
$file = file_get_contents($iplogfile);

//check to see if the ipaddress is already in the file
if ( ! preg_match("/$ipaddress/", $file )) {
//nope, log it!
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('d/m/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n");
fclose($fp);
}
?>

thank CroNiX for the code

this was for my site
 
Status
Not open for further replies.
Back
Top