Jump to content

Could someone help review this code & process?


RecklessArcher

Recommended Posts

I came across a site that showed how to record the IP address of images requests on a server.  It is a fairly simple process with a .htaccess file, the logging process file (iplogger.php) the log file (logger.html) and obviously the image.  Now I'm pretty new to PHP so I'm not sure if this process is sound, but I haven't gotten it to work on three different servers so if someone could look over the process I'd be delighted.

 

.htaccess file  **the "image.jpg" must be renamed to your image file name**

RewriteEngine on
RewriteRule ^image.jpg$ iplogger.php

iplogger.php file

<?php
$log = 'logger.html';
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['REQUEST_URI'];
$refer = $_SERVER['HTTP_REFERER'];
$date_time = date("l j F Y  g:ia", time() - date("Z")) ;
$agent = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen("logger.html", "a");
fputs($fp, "
<b>$date_time</b> <br> <b>IP: </b>$ip<br><b>Page: </b>$page<br><b>Refer: </b>$refer<br><b>Useragent:

</b>$agent <br><br>
");
flock($fp, 3);
fclose($fp);
?>

The logger.html file was blank and is supposed to be amended by the server as the image files are accessed.

 

Now all these files are to be uploaded to the same directory along with the image file. 

 

Now supposedly when the image file is accessed, the IP address of the requesting party is supposed to be logged in the logger.html file.

 

I can't see a way for the image file to trigger the logger.php file. 

 

-Is there a way to make the image link work where when the image is viewed it will be recorded in the HTML file?

Edited by RecklessArcher
Link to comment
Share on other sites

What version of PHP are you using? When you lock the file, you didn't unlock it. Why are you locking it in the first place, and after you write it?

PHP Version 5.3.10-1ubuntu3.6

I didn't know I was locking it.  How is that happening and how do I not do it?  As I said, I'm just learning PHP.  Thanks for pointing that out!

Link to comment
Share on other sites

flock($fp, 3);

 

If you don't know what your code does, you're not really learning, are ya ;)

 

Look up every function you use and make sure you understand it. Type in php.net/function here and read.

 

flock

Changelog:

5.3.2 The automatic unlocking when the file's resource handle is closed was removed. Unlocking now always has to be done manually.

Edited by Jessica
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.