0

I would like to clear a log file every X hours with javascript/php? Is this possible, if so can you help devise the code necessary to run this? Thank You.

user1516274
  • 11
  • 1
  • 2
  • Well, which is it? JavaScript or PHP? –  Jul 10 '12 at 23:51
  • 1
    `file_put_contents($logfile, '');` or `var logfile= ''`. The JS one is a var, since JS itself has no file functions. – Marc B Jul 10 '12 at 23:53
  • 1
    I'd recommend rotating your log files instead so you can at least go back a couple days to look for something. – Brad Jul 10 '12 at 23:54
  • I guess you can mess around with the file system with node.js? :) probably not what's being asked though, but hey, client side JS is increasingly becoming common. – Mahn Jul 11 '12 at 00:13
  • Also related: https://stackoverflow.com/questions/29110399/how-to-edit-the-contents-of-a-file-on-button-click-php – Jesse Nickles Jul 01 '22 at 13:42

2 Answers2

4

I will assume that you would prefer an answer involving PHP, as logs are not often kept with JavaScript, which I am assuming you are referring to here in the client-side context.

A simple file_put_contents will do.

file_put_contents('/the/path/of/your/log/file.log', '');

For the sake of completeness, you would accomplish the same in node.js using fs.writeFile.

fs.writeFile("/the/path/of/your/log/file.log", "");
  • i am slightly confused since I am not well versed with javascript or php. the source is at saabhirgill.com/chat/index.php i would just like to clear saabhirgill.com/chat/log.html every x hours – user1516274 Jul 11 '12 at 00:10
  • @user1516274 look into cronjobs, that will let you set up a script that runs every x hours, minutes, or whatever you tell it to. – Mahn Jul 11 '12 at 00:16
  • @user1516274: Make that the file parameter to clear and create a scheduled task that runs your cleanup script every *x* hours. Also, if this is not simply an experimental project, I would recommend using a database and including a timestamp field so none of this is necessary. –  Jul 11 '12 at 00:19
1

You can't do it with javascript directly if the logfile is on the webserver, you have to do it with php, although you can call the corresponding php-site from javascript.

Just delete the file in php using:

unlink('your_file_name.log');

http://www.php.net/manual/de/function.unlink.php

The easiest thing to do would be to setup a cronjob that does just that, if there's no way to run a cronjob on your webspace, however, the next best option would be to check the age of the file on each site access and delete it if it's older than $x hours. This can be done by getting the file creation timestamp via filectime() and comparing it to the current timestamp returned by time(). If the difference is more than $x hours, unlink.

$filename = '/path/to/your/file.log';
if(time() - filectime($filename) > $x * 3600) unlink($filename); // multiply by 3600 'cause times are in seconds
inVader
  • 1,504
  • 14
  • 25
  • @user: should this always happen every X hours or only if the specific php site is currently in use/displayed? An important question to provide a specifig answer might also be, what rights do you have on the server? Is it your own machine or did you rent some webspace? – inVader Jul 11 '12 at 00:48
  • Okay, the server, is it your own machine or did you rent some webspace? Is it Windows/Linux/Whatever? – inVader Jul 11 '12 at 00:51
  • i rent it and i believe its linux – user1516274 Jul 11 '12 at 00:53
  • Then you will probably have a web interface like Confixx or Plesk where you can configure your stuff? Write a php-script that does nothing more than unlinking as above. Then go to your configuration and look for something called "cronjob(s)". If you find it, just add the script as a cronjob with a period of X hours and it will automatically be executed as you wish. If you do not have the rights to run cronjob you might want to switch to a better server package because otherwise you'll have a hard time to achieve what you're looking for. – inVader Jul 11 '12 at 00:57
  • thank you, is there a way where i could run a chat without a log – user1516274 Jul 11 '12 at 01:00
  • Another option (if cronjobs are unavailable) would be to check the age of the file every time a user connects to the site using filectime(), comparing it with the current time() and delete it if it's older than X hours. – inVader Jul 11 '12 at 01:02
  • Well, if the connection between two users is going over the server, the messages have to be stored in one way or another. A database would be viable option. You could also try to establish a direct connection between the two peers using sockets in javascript but that again, is a totally different story. Have a look at for a start. – inVader Jul 11 '12 at 01:05
  • could you show me how to use the filectime() function and how to compare it and use the comparison to delete the other file – user1516274 Jul 11 '12 at 01:50
  • You can find a good description if of all php functions over at http://www.php.net, I'll edit my answer above to do what you're looking for. – inVader Jul 11 '12 at 01:55