DrakaSAN
DrakaSAN

Reputation: 7853

Creating a log of redirected connection on a page

I have a system which go get a file on my server. To be able to easily change the file the system will get, I made a simple redirect on a php page.

But now I want to keep tracks of which and when system connect. Each system now send their ID on GET when connecting to my page, but I m struggling to even write the file.

The link called is http://mysite.com/smtg/get.php?i=XX:XX:XX:XX:XX:XX

Which contain:

<?
$fp = fopen('http://mysite.com/smtg/log.txt','a+');
    fseek($fp,SEEK_END);
    $w=$_GET['i']."\r\n";
    fputs($fp,$w);
    fclose($fp);

    header('Location: http://mysite.com/smtg/file.txt', true, 302);

exit();
?>

I ve even tried creating a empty file and putting it on the server, permission are 664, but it don t get updated.

The redirect work from browser and system thought.

EDIT:

When removing the redirect, I get:

test 
Warning: fopen(/smtg/log.txt): failed to open stream: No such file or directory 
    in /var/www/mysite.com/smtg/get.php on line 5 
Warning: fseek() expects parameter 1 to be resource, boolean given 
    in /var/www/mysite.com/smtg/get.php on line 6 
Warning: fputs() expects parameter 1 to be resource, boolean given 
    in /var/www/mysite.com/smtg/get.php on line 8 
Warning: fclose() expects parameter 1 to be resource, boolean given 
    in /var/www/mysite.com/smtg/get.php on line 9 

It seems it don t find the log file, but isn t it supposed to be created if missing?

The file is on the server, empty but here (http://mysite.com/smtg/log.txt).

Upvotes: 0

Views: 137

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157910

The problem is quite simple, and coming directly from misunderstanding of the concepts related to filesystem hierarchy.

Here I tried to write an answer explaining the matters, quite awkward though, but hope it will help to comprehend these matters. Especially why you can't use neither 'http://...' nor '/smtg/'

So, for the log you have to use

file_put_contents($_SERVER['DOCUMENT_ROOT'].'/smtg/log.txt', $_GET['i'], FILE_APPEND);

Upvotes: 1

rernesto
rernesto

Reputation: 552

This work perfectly for me (trying http://localhost/stackoverflow/logger.php?i=xxxx):

logger.php

<?php
ini_set("display_errors",1);
error_reporting(E_ALL);

if(isset($_GET['i']))
{
    $text = date('H:m:s') . "\t \t \t" . $_GET['i'];
    file_put_contents(__DIR__ . '/redirection-' . date('Y-m-d'). '.log', $text."\n", FILE_APPEND);
    header('Location: http://localhost/stackoverflow/file.txt', true, 302);
}

if you're using Linux be sure the apache user has writing privileges on directory where puts log file.

Upvotes: 1

user399666
user399666

Reputation: 19889

Instead of opening a file handle on a HTTP resource:

fopen('http://mysite.com/smtg/log.txt','a+');

Try using something like this:

fopen('/smtg/log.txt','a+')

I'm pretty sure that you'll have problems using fputs on a file handle from a HTTP resource.

Upvotes: 1

Related Questions