Hello,

I have a page that loads and saves any errors that happen to a log variable.

Once the page has finished loading I want to write the log variable to a database if it has been used.

To do this I call an include function at the end of the page if the log has been used.

The log is often used to store problems that happened with connecting to a database, so if this is the problem it's likely that it won't be possible to write to the database at that moment so I want it to keep trying every minute until it writes to it even if the user has closed the page.

Also, as the log could be be updated quicker than it is written (and as it is constantly added to as a session variable, not written fresh each time) I want to cancel any previously running copies of the function also.

To achieve this I'm using this code:

<?php

//make sure people can't abort this script

flush();

ob_end_flush();

ignore_user_abort(true);

//tell any other running copies of f_log to abort and then wait for them to do so

$_SESSION['abort'] = true;

sleep (35);

//make sure no longer needing to abort

unset($_SESSION['abort']);

//write to log

$v_log = @makeslashes($log);

$q_updlog = "UPDATE logs SET log = '$v_log', used = '1' WHERE chatid = '$v_chatid'";

while (!$qr_updlog && !$_SESSION['abort']) {

$qr_updlog = mysql_query($q_updlog,$dbconnect);

sleep(30);

}

//make sure that the script is no longer going to abort

if (isset($_SESSION['abort'])) {

unset($_SESSION['abort']);

}

$_SESSION['log'] = $log;

?>

This code has several problems. Mainly that it takes a long time to show the page to the user and it is always saying it's loading for as long as I've been bothered to leave it running (quite a few minutes!).

So does anyone know what I'm doing wrong.. I'm guessing quite a bit because I'm not entirely sure I'm using this ignore_user_abort function right. The PHP manual seems a little thin on the ground witih info about it!

Thanks in advance,

Matt

    too complicated for me

    I have used ignore_user_abort() only once.
    I restored the abort after my operation.

    When making sure nothing disturbs a writing to my database.
    Which could result in a corrupt data file.

    ignore_user_abort(true);
    
    $fp=@fopen($this->tbfile,'wb');
    flock($fp,LOCK_EX);
    fwrite($fp,$tbd);
    flock($fp,LOCK_UN);
    fclose($fp);
    
    ignore_user_abort(false);

    Regars 🙂
    halojoy

      I found another application
      that uses ignore_user_abort() in the context of logging ...!!!

      in
      function upgrade_log_start() {
      ignore is turned ON

      and in
      function upgrade_log_finish() {

      ignore is turned OFF again

      Link:
      http://xref.moodle.org/nav.html?lib/adminlib.php.source.html#l622

      622 /**
      623 Start logging of output into file (if not disabled) and
      624
      prevent aborting and concurrent execution of upgrade script.
      625
      626
      Please note that you can not write into session variables after calling this function!
      627
      628
      This function may be called repeatedly.
      629 */

      I think it is from the documentation of a version of Moodle, http://moodle.org/

      🙂

        Thanks for the replies.

        I had a look at the moodle page you linked to but it was far too complicated for me, and the other example you gave still had the problem of not loading the page until the query had completed.

        I was hoping that there would be a simpler solution to this problem but maybe it's going to be too complicated.

          Write a Reply...