...

View Full Version : log file system question.



Anishgiri
07-01-2010, 10:46 AM
I created a code in which each time a user login successfully, php will write to a text file the activity of the user (login). But I can't figure it out on how it will create a new text file to write once the size of the text file exceeded 100 kb.

Here is the piece of code.




// insert to log file

$datasize= filesize("logs.txt");
if ($datasize <= 100)

{
$user= $_POST['username'];
$timendate= gmdate("l dS \of F Y h:i:s A");

$data= " User $user login at $timendate \r\n";
$file = fopen("logs.txt","a");
fwrite($file,"$data ");
fclose($file);
}


I can add something like this


if ($datasize >= 100)
{
$datasize= filesize("log2.txt");
$user= $_POST['username'];
$timendate= gmdate("l dS \of F Y h:i:s A");
if ($datasize <= 100) {
$data= " User $user login at $timendate \r\n";
$file = fopen("log2.txt","a");
fwrite($file,"$data ");
fclose($file);
}
}


But what if log2 exceeded 100 kb. It's not appropriate to create another condition for log3.txt. I think looping is the solution, in which you will also increment a number and concatenate it with text file name. But I don't know where to start any ideas? Thanks

bennyy007
07-01-2010, 10:54 AM
If I'm honest that I would never do is have a file created that holds the user's activity unless it's stored outside of the public folder so it can't be access from the web and only you can access it.

Have you considered storing this type of information into a database?

Anishgiri
07-02-2010, 04:46 AM
Thanks for the reply, I decided to use session to address my problem. Is there any risk with that?

Sorry for a noob question, but how can you make a web page not accesible from the web?
Use the include path?

rfresh
07-02-2010, 06:53 AM
How are you using a session for that? You're still writing a text file?

Anishgiri
07-02-2010, 09:33 AM
Yeah, I still write on a text file. Why? Is there a risk?

bennyy007
07-02-2010, 01:23 PM
I would say yes to that because anyone could access that and know more than they should, yet this is only user's activity but still it is your duty to keep users information private even activity. It's one of those things every webmaster should do myself I've always handled users information in a secure manner either SSL or securing it somewhere out of reach from the public hands.

But as you asked how to access a file outside of the public folder, well this is simple but requires a little bit of work.

Now if your using cPanel then it would be before the "public_html" folder (unless that has changed, haven't used cPanel for a few years now.) For Plesk it would be before "httpdocs" or "httpsdocs" (SSL Only).

Now for including such files that reside outside of the public folders as most people use PHP to conger this. By simply adding this as soon as you start a PHP file:



<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/../thefile.txt');

// Rest of code goes here

?>


Remember to add more /../ to it if it's further above the 'public_html' or 'httpdocs' folder so for example if I placed 'thefile.txt' into a subdomain (http://subdomain.example.org/) you would then do this:



<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/../../../../private/thefile.txt');

// Rest of code goes here

?>


As said above you just keep adding more '/../' the higher you go up in folders this should then include the data no matter what. So hopefully this answers your question...Need anymore help then just ask!

kbluhm
07-02-2010, 05:32 PM
Untested, but it should accomplish what you're looking for:


/* - - - - - - - - - -
* The log file
*/

$log_file = './logs.txt';

/* - - - - - - - - - -
* The data to be logged
*/

$log_data = 'This is to be logged.';

/* - - - - - - - - - -
* Does the log file already exist?
*/

$log_exists = is_file( $log_file );

/* - - - - - - - - - -
* Log the data
* If the file already exists, append it
*/

file_put_contents(
$log_file,
( $log_exists ? "\n" : '' ) . $log_data,
( $log_exists ? FILE_APPEND : 0 )
);

/* - - - - - - - - - -
* If the filesize is over 100kb, rename it
* - This will loop logs1.txt, logs2.txt, logs3.txt, etc,
* till it comes to a file name that does not exist
*/

if ( filesize( $log_file ) > 102400 )
{
$inc = 1;
do {
$log_file_bak = './logs' . $inc++ . '.txt';
} while( is_file( $log_file_bak ) );
rename( $log_file, $log_file_bak );
}

Anishgiri
07-05-2010, 02:27 AM
Sorry for the late reply.

@Benny thanks for this usefull information.


@kbluhm, thanks for the help, I will try your code. I did try something, but I think your code is more effeficient so I will try that. Below is the solution that I did.





// the value of counter is from a mysql fetch array

$_SESSION['increment'] = $info['counter'];

if (!file_exists("log1.txt"))

{


mysql_query("UPDATE user_table SET counter= 1 ")
or die(mysql_error());
}


// insert to log file


$a = $_SESSION['increment'];
$filename= "log" .$a ."." ."txt";
$file = fopen("$filename","a");
$datasize= filesize("$filename");

$user= $_POST['username'];
$timendate= gmdate("l dS \of F Y h:i:s A");

$data= " User $user login at $timendate \r\n";
fwrite($file,"$data ");
fclose($file);


if ($datasize >= 100000)
{

mysql_query("UPDATE user_table SET counter = counter + 1 ")
or die(mysql_error());

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum