![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
OK here's the code:
<?php
error_reporting(E_ALL);
function cleanup($filename){
unlink($filename);
}
$filename = tempnam(getcwd(), 'tmp');
register_shutdown_function('cleanup', $filename);
set_time_limit(3);
sleep(4);
?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Re: register_shutdown_function() not behaving as expected
Just use a real database. Your square wheel is a waste of time.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Re: register_shutdown_function() not behaving as expected
I'm curious: have you tested your diagnosis in alternate browsers?
Dameon, I'm of the notion that there's no such thing as a waste of time, if your time consists of doing something that interests and challenges you. I imagine it's the same idea for grimpirate. Last edited by Sane; Jan 17th, 2008 at 12:43 PM. |
|
|
|
|
|
#4 |
|
King of Portal
|
Re: register_shutdown_function() not behaving as expected
@Dameon: As a PF veteran I would hope that you could post something relevant to the thread and not just discourage others. I figure the intent of the forum is to help people, not discourage them from what they're attempting to do (however useless it may seem to you).
@Sane: Hmm... no I haven't Sane. I've only tested on Opera. I'll see what happens on Firefox and IE.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#5 |
|
King of Portal
|
Re: register_shutdown_function() not behaving as expected
Tried it on IE and Firefox and it produced the same results. I actually added a line to the code
<?php
error_reporting(E_ALL);
ignore_user_abort(false);
function cleanup($filename){
unlink($filename);
}
$filename = tempnam(getcwd(), 'tmp');
register_shutdown_function('cleanup', $filename);
set_time_limit(20);
sleep(30);
?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
Re: register_shutdown_function() not behaving as expected
I'm having permissions trouble with the server space I haven't yet been able to run PHP scripts on. I'll get back to you when I resolve that.
I don't know much about what you're trying to accomplish. I'm not sure if many do. But are you sure it's something standard? Seems unlikely that a user abort could trigger a function consistently. I'm also wondering... maybe it only knows that the user aborted when it attempts to send a packet of data to the client, and sees that it's disconnected. When you've got the client hanging on a 30 second timeout, there's no data being transferred, right? So how would it know that the user clicked stop, unless there's some hidden communication going on? Maybe you could try outputting an infinite loop of numbers or something, and set a max running time on the script. Replace this with your sleep timeout. |
|
|
|
|
|
#7 |
|
King of Portal
|
Re: register_shutdown_function() not behaving as expected
Attempted the following and it appears to have worked.
<?php
error_reporting(E_ALL);
ignore_user_abort(false);
function cleanup($filename){
switch(connection_status()){
case 1:
case 2:
case 3:
unlink($filename);
break;
}
}
$filename = tempnam(getcwd(), 'tmp');
register_shutdown_function('cleanup', $filename);
set_time_limit(5);
do{
echo 'foo';
}while(true);
?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis Last edited by grimpirate; Jan 18th, 2008 at 9:51 AM. |
|
|
|
|
|
#8 |
|
King of Portal
|
Re: register_shutdown_function() not behaving as expected
Well I couldn't output the connection_status() result to a file, buuuuuut I modded the code to
<?php
// Running on a Windows XP machine with an Apache server
error_reporting(E_ALL);
ignore_user_abort(false); // Does indeed affect behavior now
function cleanup($filename){
switch(connection_status()){
case 1:
unlink($filename);
case 2:
case 3:
break;
}
}
$filename = tempnam(getcwd(), 'tmp');
register_shutdown_function('cleanup', $filename);
set_time_limit(5);
// Purposely causes a CONNECTION_TIMEOUT
do{
echo 'foo';
}while(true);
?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#9 |
|
King of Portal
|
Since you were curious what I was trying to accomplish Sane here it is:
<?php
error_reporting(E_ALL);
ignore_user_abort(false); // Need to ensure behavior is false so in case of abort the mutex will be removed
/* Reference Material
*
* http://en.wikipedia.org/wiki/ACID
*
*/
/*************/
/* Test Code */
/*************/
$mutex = new Mutex(getcwd() . '\tmp000.tmp', 10, 0755, true);
$mutex->acquireLock();
// Purposely causes a CONNECTION_TIMEOUT
do{
echo 'foo';
}while(true);
/*************/
class Mutex {
/* Private variables */
var $filename; // The file to be locked
var $timeout = 60; // The timeout value of the lock
var $permission = 0755; // The permission value of the locked file
/* Constructor */
function Mutex($filename, $timeout = 1, $permission = null, $override = false){
// Append '.lck' extension to filename for the locking mechanism
$this->filename = $filename . '.lck';
// Timeout should be some factor greater than the maximum script execution time
$cfgtime = @get_cfg_var('max_execution_time');
if($override === true){
if($timeout >= 1) $this->timeout = 2* $timeout;
set_time_limit($this->timeout / 2);
}elseif($cfgtime !== false){
if($timeout >= 1) $this->timeout = $timeout * (2 * $cfgtime);
}
// Should some other permission value be necessary
if(isset($permission)) $this->permission = $permission;
register_shutdown_function(array($this, "lockAbort"));
}
/* Methods */
function acquireLock(){
// Create the locked file, the 'x' parameter is used to detect a preexisting lock
$fp = @fopen($this->filename, 'x');
// If an error occurs fail lock
if($fp === false) return false;
// If the permission set is unsuccessful fail lock
if(!@chmod($this->filename, $this->permission)) return false;
// If unable to write the timeout value fail lock
if(false === @fwrite($fp, time() + intval($this->timeout))) return false;
// If lock is successfully closed validate lock
return fclose($fp);
}
function releaseLock(){
// Delete the file with the extension '.lck'
return @unlink($this->filename);
}
function lockTime(){
// Retrieve the contents of the lock file
$timeout = @file_get_contents($this->filename);
// If no contents retrieved return error
if($timeout === false) return false;
// Return the timeout value
return intval($timeout);
}
function lockFile(){
return $this->filename;
}
function lockAbort(){
switch(connection_status()){
case 0:
break;
default:
@unlink($this->filename);
break;
}
}
}
?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
Re: register_shutdown_function() not behaving as expected
Ah, yes. So it only checked for disconnection after it was done sleeping. So I was close.
![]() Very interesting. You should post this on a PHP guru website and see what they have to say about it. I'm sure you could get lots of great contributions, and people trying to bust holes in your algorithm. What happens when multiple scripts try to request a lock for the same file at the same time? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unqualified ID Expected? | SonicReducer | C++ | 3 | Mar 28th, 2007 5:29 PM |
| error 3 Expected end-of-statement | m0rb1d | Other Scripting Languages | 0 | Dec 12th, 2006 10:05 AM |
| error: expected primary-expression... | rup | C++ | 18 | Apr 17th, 2006 2:24 PM |
| realloc not functioning as expected | nnxion | C | 24 | Nov 29th, 2005 6:20 PM |
| Total noob, trouble expected? | Mudanie | Python | 18 | Oct 27th, 2005 10:03 AM |