/    Sign up×
Community /Pin to ProfileBookmark

log file system question.

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.

[code=php]

// 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 rn”;
$file = fopen(“logs.txt”,”a”);
fwrite($file,”$data “);
fclose($file);
}
[/code]

Any Ideas?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJul 01.2010 — I would just use

[CODE]
$file = fopen("newFileName.txt","a");
[/CODE]


to open a file with a different filename.
Copy linkTweet thisAlerts:
@anishgiriauthorJul 01.2010 — Yeah, But what if newfilename.txt exceeded 100 kb again? I think it's not appropriate to create another condition for newfilename.txt as will make the code longer like this,
[code=php]
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 rn";
$file = fopen("log2.txt","a");
fwrite($file,"$data ");
fclose($file);
}
}

[/code]


I think looping is the solution, in which you will also increment a number and concatenate it with text file name. Does it make sense? Or maybe I should try to make it a database?
Copy linkTweet thisAlerts:
@tirnaJul 01.2010 — Yeah, But what if newfilename.txt exceeded 100 kb again? [/quote]

You didn't say in your first post you wanted to create a new file [B]every time[/B] the current file exceeds 100kb. You only asked how to create a new file when the first file exceeded 100kb.

Sorry, but I skipped school the day they taught mind reading ?.

There are a few ways to do what you want. Whether you use a loop to create a new file name or some other method is totally up to you.
Copy linkTweet thisAlerts:
@criterion9Jul 01.2010 — Generally this is where most people would use a log rotator. The concept there is to store the previous week/day/month/100kb/whatever in each file in a number sequence. Then when the "main" file reaches the age or size limit cycle them all down the line.

I usually rotate in a slightly more "advanced" algorithm based on time for application logs. I keep each day in a new file for one week. Then I keep 4 weeks (combined 7 day files). Then I keep 2 months (combined 4 week files). Then I discard the rest.
Copy linkTweet thisAlerts:
@anishgiriauthorJul 02.2010 — @tirna - sorry if I did not explain the problem cleary. So I decided just to use session, as I can't think of a possible solution with loops.

@criterion - thanks for some information
×

Success!

Help @anishgiri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...