-4

Ok on my site i got access to a specific file secured by only allowing whitelisted ips allowed to access the file (all done in php not .htaccess)

else die, it displays an error saying access denied, how do i get it to save the users ip and date + time in a text file, the variable for the users ip is $host

it is not a duplicate because i need to know how to log as a text file. thanks

user5675649
  • 117
  • 1
  • 2
  • 10
  • 2
    This is the sort of thing where a Google search for something like "PHP write to text file" would be a much better option. – David Dec 18 '15 at 12:21
  • Possible duplicate of [How to get the client IP address in PHP?](http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php) – z0nam Dec 18 '15 at 12:22
  • but how i log to .txt file? – user5675649 Dec 18 '15 at 12:25

1 Answers1

0

STEP1. Get your ip. For more information, How to get the current date and time in PHP? and How to get the client IP address in PHP?

$host = $_SERVER['REMOTE_ADDR'];
$timezone = date_default_timezone_get();

STEP2. To determine whether it is allowed, find if $host is in your whitelist (in a file, or MySQL)

STEP3. For writing to file, just open your custom log file and append $host.$timezone to it.

Community
  • 1
  • 1
z0nam
  • 525
  • 7
  • 19
  • i already got that, but how do i save it to a text file when someone is not allowed tries to access it? – user5675649 Dec 18 '15 at 12:27
  • @user5675649 Above STEP2 is doing it. You should insert code to check whether the user is allowed or not. – z0nam Dec 21 '15 at 02:26