exit without flushing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

exit without flushing

Post by shawngoldw »

I have a script and with certain conditions I call exit(). How can I set the script up so that the output does not get flushed to the user when the script terminates?

A simple solution would be to use ob_start() before print and then clear it before exiting. This will not work for me. I have stuff echo'd and print'd and I need to clear something on or before exit.

Thank you,
Shawn
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: exit without flushing

Post by AbraCadaver »

You'll have to be more specific about why ob_start() and ob_end_clean() won't work.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: exit without flushing

Post by pickle »

If you have stuff output before your script runs, then there's nothing you can do to stop it. If your script generates absolutely all output, then there's no reason why ob_start() and ob_end_clean() shouldn't work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: exit without flushing

Post by shawngoldw »

They will work, but they will make everything much more complicated to write.
This is a problem arising from a parallel processing script I wrote,

Code: Select all

class Railway
{
	// pids of running processes
	var $pids;
	
	// shm references
	var $shm;
	
	// pointers to return variables
	var $returns;
	
	
	// dispatch a new thread
	function dispatch($file, &$params = null, &$return = null)
	{
		// start new thread
		$pid = pcntl_fork();
		if ($pid == -1)		// fork failed
		{
			return false;
		}
		
		$this->returns[$pid] =& $return;
		$this->_start_shm($pid);
		
		if ($pid > 0)	// parent	
		{
			return true;
		}
		else				// child
		{	
			// include requested file
			include($file);
			exit;
		}
	}
	
	// private
	function _start_shm($pid)
	{	
		$this->pids[] = $pid;
		if ($pid == 0)
		{
			$pid = getmypid();
		}
		$this->shm[$pid] = shm_attach($pid);
	}
	
	// only for use by children
	function set_return($value)
	{
		shm_put_var(end($this->shm), 1, $value);
	}
	
	// wait for all threads to finish
	function wait(&$status = null)
	{
		//wait for each thread to finish
		for ($i=0; $i < count($this->pids); $i++) 
		{ 
			pcntl_wait($status);
		}
		
		foreach ($this->pids as $key => $value) 
		{
			$shm =& $this->shm[$value];
			if (isset($this->returns[$value])) 
			{
				if (shm_has_var($shm, 1))
				{
					$this->returns[$value] = shm_get_var($shm, 1);
				}
			}
			shm_remove($shm);
			shm_detach($shm);
		}
		$this->pids = null;
		$this->shm = null;
		$this->returns = null;
	}
}
I'm using pcntl_fork and with pcntl_fork the child process get the output of the parent, so when the child finishes, the output in the parent prints in the child too. echo's and print's are really just being used for debugging purposes, actual content gets printed after the parallel processing so there is not a problem with the actual content.

Talking stuff through helps, I may just be able to flush before forking. That may work.


edit: yup, flush() before pcntl_fork() did the trick. Waste of a thread, thanks anyways
Post Reply