Jump to content

Recommended Posts

I have a php script that takes the input and inserts it into the mysql database. As soon as the entry in the table is completed, this script calls another script using shell_exec(). Here is the first php script:

 

    

if(isset($_POST['post_arg'])){
        $theme = $_POST['topic_theme'];
        $des = $_POST['detail'];
        $hrs_to_go = 36;
        $t = time();

        $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
        mysql_select_db('topics');
        $sql_query = "INSERT INTO `topics`.`theme`(heading, description, hrs_to_go, status, time) VALUES ('$theme', '$des', '$hrs_to_go', 'yes', '$t')";
        $ret_val = mysql_query($sql_query, $conn) or die(mysql_error());
        $row = mysql_fetch_assoc(mysql_query("SELECT id FROM `theme` where heading = '$theme'"));
        $themeID = $row['id'];
        shell_exec("php timer_script.php $themeID");
    }

I want my second script to fetch the time and hrs_to_go from my table and update the field hrs_to_go accordingly. But unfortunately the second script does not work properly and the browser keeps on loading without any redirect. Here is the second script:

ignore_user_abort(true);
    session_start();
    set_time_limit(0);
    $conn = mysql_connect('localhost', 'root', '');
    mysql_select_db('topics');
    $themeID = $_SERVER['argv'][1];
    $fetched_row = mysql_fetch_assoc(mysql_query("SELECT time, hrs_to_go FROM `theme` WHERE id = '$themeID'"));
    
    while(1){
        $htm = time() - (int)$fetched_row['time'];
        $htm = round($htm / 3600);
        $h = (int)$fetched_row['hrs_to_go'] - $htm;
        if($h == 0){
            mysql_query("UPDATE `theme` SET status = 'no' WHERE id = '$themeID'") or die(mysql_error());
            break;
        } else {
            mysql_query("UPDATE `theme` SET hrs_to_go = '$new' WHERE id = '$themeID'") or die(mysql_error());
        }


        sleep(10);
    }
    
 
 

 

Although my variables are hrs_togo but I've kept the iterval time smaller to see the changes.

Why does the second script need to keep updating the hrs_to_go column? What is the purpose of this?

 

 

browser keeps on loading without any redirect.

shell_exec does not redirect the browser, it will wait until the script timer_script.php has finished executing/returned a result. This is why the  browser is continually loading.

 

I feel continually updating the hrs_to_go column is unnecessary. You are better of storing the interval as a future date/time, eg you add a row the time is 2015-07-06 10am, adn the interval is 36 hours form now, then you stored a second timestamp as 2015-07-08 22:00:00 as the interval date

 

To calculate the hours to go you can use TIMESTAMPDIFF to subtract todays date with the date interval stored in your table. Example query

SELECT time, 
       TIMESTAMPDIFF(HOUR, NOW(), interval_date) as hrs_to_go           #substracts todays date, with the date stored in interval and returns the difference in hours
FROM theme 
WHERE interval < NOW()

You should not need to continually update your theme table when calculating differences between dates/times

Edited by Ch0cu3r

Thank you for your response. Actually I wanted my script to run every our from the time of posting a new post. Your approach seems to be a clean and nice approach. But just one thing. doesn't ignore_user_abort(true) tells the browser to work on the back end? Or is there any other way to run a script on the back end, on the server and redirects the user to some other page on the website?

Why does the second script need to keep updating the hrs_to_go column? What is the purpose of this?

 

shell_exec does not redirect the browser, it will wait until the script timer_script.php has finished executing/returned a result. This is why the  browser is continually loading.

 

I feel continually updating the hrs_to_go column is unnecessary. You are better of storing the interval as a future date/time, eg you add a row the time is 2015-07-06 10am, adn the interval is 36 hours form now, then you stored a second timestamp as 2015-07-08 22:00:00 as the interval date

 

To calculate the hours to go you can use TIMESTAMPDIFF to subtract todays date with the date interval stored in your table. Example query

SELECT time, 
       TIMESTAMPDIFF(HOUR, NOW(), interval_date) as hrs_to_go           #substracts todays date, with the date stored in interval and returns the difference in hours
FROM theme 
WHERE interval < NOW()

You should not need to continually update your theme table when calculating differences between dates/times

Thank you for your response. Actually I wanted my script to run every our from the time of posting a new post. Your approach seems to be a clean and nice approach. But just one thing. doesn't ignore_user_abort(true) tells the browser to work on the back end? Or is there any other way to run a script on the back end, on the server and redirects the user to some other page on the website?

If you want a background process being ran constantly, then you are better of setting up a cron job rather than have it being ran from the browser.

 

 

But just one thing. doesn't ignore_user_abort(true) tells the browser to work on the back end?

No, it has nothing to do with the browser. That function intended use is to allow the script to finish processing when a user aborts the script from the command line. PHP cannot detect when the browser (window/tab) has been closed.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.