0

Hi
I would like display visitor ip address and location on my website and save it in ip.txt file in my webserver.
How can I do it?

<?php 
$ip=$_SERVER['REMOTE_ADDR'];
$file = fopen('ip.txt','w');
fwrite($file,$ip);
echo "Your IP Address is $ip"; 
 ?> 

 //When I run in my xampp localhost I got like this?why?

// Your IP Address is ::1 

I appreciate your answer

3
Contributors
19
Replies
132
Views
3 Years
Discussion Span
Last Post by tony75
Featured Replies
  • Let's go step by step. First of all, try and run this. <?php error_reporting(E_ALL); // set error level to show all errors echo $_SERVER['SCRIPT_FILENAME']."<br />"; # create ip.txt file if not exist $f= $hits = ""; $filename="ip.txt"; if (file_exists($filename)) { $hits = file_get_contents($filename); } else { if(file_put_contents($filename, '') === false) … Read More

0

It seems work in real server but I got also those Warning in line3 and line 4 ?

Warning: fopen(ip.txt): failed to open stream: Permission denied in
Warning: fwrite() expects parameter 1 to be resource, boolean given in
0

Does your hosts file look like this?

# localhost name resolution is handled within DNS itself.
127.0.0.1       localhost
::1             localhost
0

Thanks diafol for you answer.
The host file look like ::1 localhost.
I put my code on the real server it works.
But I got 2 warning in line3 and line 4 but I still see the ip.

Warning: fopen(ip.txt): failed to open stream: Permission denied in
Warning: fwrite() expects parameter 1 to be resource, boolean given in

I think the warnings was Im not created ip.txt file on my server and I didn't give it right permission like 0766 to the file.

Now I need to creat ip.txt via if statmen if ip.txt not exist it.

and give ip.txt right permissin somthing like this

chmod("/somedir/somefile", 0766);

When ip adress saved on my ip.txt for next visitor append his ip to ip.txt not overwrite the old ip.

Thanks for your help

0

How can I add 0766 permession to file ip.txt(via code)
without using ipt.txt properties manually on the server.

<?php 
$ip=$_SERVER['REMOTE_ADDR'];
$dt = new DateTime();
$file = fopen('ip.txt','a+') or die("can't open file");
fwrite($file, $ip . ' ' . $dt->format('Y-m-d H:i:s') ."\n");
echo "Your IP Address Is: $ip\n";

 ?> 

Edited by tony75

0

If your script doesn't have permission to create the file, you won't be able to set permissions. Most hosting services allow you to change permissions manually. If you want to create files with your script, create a directory with write permissions manually through your hosting panel and then your script will be able to create new files in that directory.

0

Thanks Sir
But I like to creat ip.txt via if statmen if ip.txt not exist it and give ip.txt 0766 permissin via code not change permissions manually.
About www.ip2country.net/ I don't like to pay for this!

0

As I said above, in order to be able to create the file you will need permissions set for the directory you want to write it in.
When I was looking for location information, I couldn't find anything free. Let me know if you find anything. The location database just for country has 96,000 records.

0

Thanks sir for your answer
It will be better if you explain with code so that I can Undersand it.

0

This is not a problem that can be fixed with code. For some reason, your script does not have permission to create a file in the location you are trying. If you haven't created the file, you can't change the file permissions.
1. Make sure that where you are trying to write the file is within the website file tree (website root or below). Usually scripts can write files anywhere under the web root. Post your code which writes ip.txt.
2. If not, go onto your hosting account control panel and manually create a folder, e.g. 'filefolder', then manually give it write permissions. Modify your script to open/write ip.txt inside this folder ('filefolder/ip.txt'). This should work. I can't tell you how to to use your control panel, hosting services have different control panels.
3. If this doesn't work, contact your hosting service support and ask why you are unable to write files into the website area with a simple php script. It could be a badly configured server.

0

Thanks Sir
My code is embedded in myPage.php
But the ip.txt file is located in webroot directory and I gave manually 0766 permession via file properties which is work very fine!but I need to give this permession via code instead.
I don't know if its right like this

<?php 
$path = "C:\xampp\htdocs\test\ip\ip.txt";
$ip=$_SERVER['REMOTE_ADDR'];
$dt = new DateTime();
$file = fopen('ip.txt','a+') or die("can't open file");
//fwrite($file,$ip."\n");
fwrite($file, $ip . ' ' . $dt->format('Y-m-d H:i:s') ."\n");
chmod($path, 0766)
echo "Your IP Address Saved: $ip\n";
//echo " Date: ",$dt->format('Y-m-d H:i:s'); 

 ?> 

Edited by tony75

0

Still not work!
Your IP Address Saved: ::1
Warning: chmod(): No such file or directory in C:\xampp\htdocs\test\ip\ip.php on line 20

If the file is not exist why is not create it???

<?php
# create ip.txt file if not exist
$f= "";
$filename="ip.txt";
if (file_exists($filename)) {
    $hits = file_get_contents($filename);
} else {
    file_put_contents($filename, '');
}
echo $f;
 #path to local host server 
$path = "C:\xampp\htdocs\test\ip\ip.txt";
$ip=$_SERVER['REMOTE_ADDR'];
$dt = new DateTime();
$file = fopen('ip.txt','a+') or die("can't open file");
//fwrite($file,$ip."\n");
fwrite($file, $ip . ' ' . $dt->format('Y-m-d H:i:s') ."\n");
#set 0766 permession
echo "Your IP Address Saved: $ip\n";
chmod($path, 0766)
 ?> 

Edited by tony75

0

You are trying to write a file to the directory:
C:\xampp\htdocs\test\ip\
What are the file permissions of this directory?

0

In fact it was read and write as I see in my windows ip folder,I don't know if its matter?

I wonder if my code wrong?

Edited by tony75

0

Then you need to manually change the directory permissions on your ip folder to add write permissions. Then your script should be able to create a file inside it.
Note, if a folder or file does not allow your script to write to it, it does not allow your script to chmod it. That wouldn't be very secure, would it?
If you are having trouble with this, read up about Linux or Unix file permissions (PHP mimics unix commands even when running on a Windows system).

0

Thanks sir for your answer.
I would like know if my code right is there any way to do it?

1

Let's go step by step.
First of all, try and run this.

<?php
error_reporting(E_ALL); // set error level to show all errors
echo $_SERVER['SCRIPT_FILENAME']."<br />";
# create ip.txt file if not exist
$f= $hits = "";
$filename="ip.txt";
if (file_exists($filename)) {
    $hits = file_get_contents($filename);
} else {
    if(file_put_contents($filename, '') === false)  {
            exit("<br />File put failure");
        }
}
echo ("<br />File Exists now: ".$hits);
/*echo $f;
 #path to local host server 
$path = "C:\xampp\htdocs\test\ip\ip.txt";
$ip=$_SERVER['REMOTE_ADDR'];
$dt = new DateTime();
$file = fopen('ip.txt','a+') or die("can't open file");
//fwrite($file,$ip."\n");
fwrite($file, $ip . ' ' . $dt->format('Y-m-d H:i:s') ."\n");
#set 0766 permession
echo "Your IP Address Saved: $ip\n";
chmod($path, 0766)
*/
 ?> 

If you do not have permissions to create the file, you will get a php error such as "Warning: file_put_contents(ip.txt): failed to open stream: Permission denied in ...." followed by "File put failure".
If you do have permissions, you will see "File Exists now:"
Report back results of what prints.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.