Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 01:34 PM   PM User | #1
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
ignore_user_abort()

Could you give me an example with ignore_user_abort() ?
Thank you!
milanello72 is offline   Reply With Quote
Old 01-20-2013, 01:45 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
PHP Code:
<?php
ignore_user_abort
();

while (
$Something_is_true)
   {
   
//Do something here - check a socket connection, monitor a database etc.
   
}
?>

That will keep cycling until your condition becomes false. The user can call the page via their browser, click stop and the script will continue to run. It will however stop after the max_execution_time setting (often 30 - 60 seconds) so you may need to use set_time_limit(0) to override that (but some hosts disable that).

You use this function at your own risk and peril. Its dangerous in the wrong unenlightened hands because if you don't put in a check that will terminate the script, it can carry on running and eat up all the CPU resources of your server. If you're on a shared machine then your host will almost certainly shut you down. You MUST write in a clause that will stop the script - EG just checking if a file called stop.txt exists (or to avoid any path issues and the file never being fouind, check a database table or session instead).

Also in your loop be sure to use a delay such as sleep(1) to make the script sleep for a second. Why? Because if you don't the loop will try to run as fast as the processor will allow - eating the CPU usage again as described above. It will take over the CPU and lock out other processes that need it. You'll get shut down fast.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 01-20-2013 at 01:54 PM..
tangoforce is offline   Reply With Quote
Old 01-20-2013, 04:50 PM   PM User | #3
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
I have read the php manual, but I have some problems. For exemple in this script from here
http://php.net/manual/en/function.ig...ser-abort.php:
Code:
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);

echo 'Testing connection handling in PHP';

// Run a pointless loop that sometime
// hopefully will make us click away from
// page or click the "Stop" button.
while(1)
{
// Did the connection fail?
if(connection_status() != CONNECTION_NORMAL)
{
     break;
}

// Sleep for 10 seconds
sleep(10);
}

// If this is reached, then the 'break'
// was triggered from inside the while loop

// So here we can log, or perform any other tasks
// we need without actually being dependent on the
// browser.
?>
If I run this script I can't see the message 'Testing connection handling in PHP'. in fact the script is running without ptrint any message. You can see here:
http://ionutspot.uhostall.com/1.php

And if i delete
Code:
ignore_user_abort(true);
set_time_limit(0);
the script will make the same thing like on the page http://ionutspot.uhostall.com/1.php

So i hope that you understood me, i can't understand how could i use ignore_user_abort....
milanello72 is offline   Reply With Quote
Old 01-20-2013, 05:06 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
This is a complex issue.

I will answer your points below in RED.

Quote:
Originally Posted by milanello72 View Post
I have read the php manual, but I have some problems. For exemple in this script from here
http://php.net/manual/en/function.ig...ser-abort.php:
Code:
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);

echo 'Testing connection handling in PHP';
You will not see this printed to your browser unless the script ends while your browser is still connected. You would be better putting this comment into a logfile or database table. You could attempt to flush() but this rarely works.

// Run a pointless loop that sometime
// hopefully will make us click away from
// page or click the "Stop" button.
while(1)
{
// Did the connection fail?
if(connection_status() != CONNECTION_NORMAL)
{
You need to log this action so that you know the script has stopped. Otherwise you could end up with anything from 10 - 100 versions of this script running and you wouldn't even realise.
     break;
}

// Sleep for 10 seconds
sleep(10);
Slight overkill at 10 seconds - you'll be waiting forever to know what the script is doing. Bring this down to 1 second - much easier for debugging.
}

// If this is reached, then the 'break'
// was triggered from inside the while loop

// So here we can log, or perform any other tasks
// we need without actually being dependent on the
// browser.
You could also use register_shutdown_function instead.
?>
If I run this script I can't see the message 'Testing connection handling in PHP'. in fact the script is running without ptrint any message. You can see here:
http://ionutspot.uhostall.com/1.php

You won't see anything because the script is still running. Therefore the webserver doesn't know how much or what type the output content is and won't flush the reply headers. Even if you force a flush the browser may not consider an incomplete page worth renderring / displaying while its still connected because it will expect the server to send more data.

And if i delete
Code:
ignore_user_abort(true);
set_time_limit(0);
the script will make the same thing like on the page http://ionutspot.uhostall.com/1.php

As above. While in a loop you can forget about getting any reliable form of output displayed in your browser.

So i hope that you understood me, i can't understand how could i use ignore_user_abort....
You can only really use ignore_user_abort on scripts that do things on the server side that your visitor doesn't need to see. For instance you might want a visitor to trigger a maintenance script to optimize your database.

It does nothing for your visitor. It simply tells php that the script can carry on running after the visitors browser has closed the connection.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 01-20-2013 at 05:11 PM..
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:54 PM.


Advertisement
Log in to turn off these ads.