dongwan0574
2017-06-29 21:19
浏览 168
已采纳

在服务器上创建从jquery到txt文件的日志文件

I can't find on the web so I'm asking some help over here. I'd like to create a log file, when someone press on a button it will a text to a log.txt file. I'm just looking for the the base code based on Jquery ajax and PHP

Thanks

图片转代码服务由CSDN问答提供 功能建议

我在网上找不到,所以我在这里要求一些帮助。 我想要 创建一个日志文件,当有人按下按钮时,它会将文本发送到log.txt文件。 我只是在寻找基于Jquery ajax和PHP的基本代码

谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongluoheng3324 2017-06-29 21:31
    已采纳

    there is no base code for jquery logging to sever

    1. first of, you have to decide how many times you want to call the server,
      • each log entry will be too much load on server,
      • append all to js array and post on session ending? when doe's session ends? and what if user closes browser? all the log gone? (see my suggestion bellow)
    2. you'll have to make an endpoint on server to get log entries and append them to file, then you should think of storage, and how to organize log enries from multiple clients

    I would recomend using log4javascript library with AjaxAppender, see example here, you can then use setBatchSize to send in batches or setTimed to send with time intervals, see documentation here, also you can setSendAllOnUnload to don't lose messages on browser close (in most cases).

    in the server i would use log4php with LoggerAppenderRollingFile see here

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhuan1937 2017-06-29 21:39

    I don't have to time to code it for you but here is the basic logic. You can look up how to do each item individually.

    You need to add an event listener with jquery for the click event and bind it to the button.

    Inside that, have an ajax request to some endpoint or script on your server that will update the log file.

    When that script is executed (via the ajax call), you can have PHP open the file and append a line to it that has whatever data you want.

    JavaScript:

    $('button').on('click', function() {
      $.ajax({
        // do ajax request here
      });
    });
    

    PHP:

    $file = fopen("log.txt","rw");
    $fileContents = fread($file, filesize("log.txt"));
    fwrite($file, $fileContents."Someone clicked button
    ");
    fclose($file);
    

    Something like that. Sorry for not giving a full explanation but I hope it helps.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题