~ overflow ~

Archive for May 20th, 2010

Multi threaded php without EXEC

by z3n on May.20, 2010, under Coding, Tips & Hints

Problem:

How to do a multi threaded php execution without using exec or any other less secure method?

Solution:

function _load_thread() { // v1.0
	global $_SESSION,$_POST,$_GET,$_REQUEST,$_COOKIE,$_SERVER;
	if (isset($_SESSION)) {
		if (isset($_SESSION['is_thread']) && $_SESSION['is_thread']) {
			ignore_user_abort(true);
			$_SESSION['is_thread']=0;
			$x=unserialize($_SESSION['thread']);
			$_SESSION['thread']="";
			$_POST=$x['POST'];
			$_GET=$x['GET'];
			$_REQUEST=$x['REQUEST'];
			@ $_SERVER=$x['SERVER'];
			$_COOKIE=$x['COOKIE'];
			define("_is_thread",1);
		}
	}
}
function _save_thread() { // v1.0
	global $_SESSION,$_POST,$_GET,$_REQUEST,$_COOKIE,$_SERVER;
	if (isset($_SESSION)) {
		$_SESSION['is_thread']=1;
		$_SESSION['thread']=serialize(array(
			"POST" => $_POST,
			"GET" => $_GET,
			"REQUEST" => $_REQUEST,
			"SERVER" => $_SERVER,
			"COOKIE" => $_COOKIE
		));
	}
}
function _go_thread() { // v1.0
	global $_SERVER,$post;
	_save_thread();
	$post="thread_sessid=".session_id();
	@ cget(slash_fix(site_full_uri.$_SERVER['PHP_SELF']),"none",1,0,0,1,0,1);
}
function cget($url,$ref="none",$is_post=0,$is_cookie=0,$headers=0,$follow=1,$verbose=0,$timeout=30) { // v1.05-lite
	global $post,$cookie;
	$r=$tail="";
	$ch=curl_init($url);
	curl_setopt($ch,CURLOPT_VERBOSE,$verbose);
	curl_setopt($ch,CURLOPT_HEADER,$headers);
	curl_setopt($ch,CURLOPT_HTTPHEADER,array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)","Accept-Encoding:deflate"));
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$follow);
	curl_setopt($ch,CURLOPT_MAXREDIRS,5);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch,CURLOPT_REFERER,$ref);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); // default 30
	curl_setopt($ch,CURLOPT_TIMEOUT,$timeout); // default 30
	if ($is_post) {
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$post); // post must be a string, if you set it as a array it will use a non standard multi-part post
		$tail.=" ".$post;
		unset($post);
	}
	if ($is_cookie) {
		curl_setopt($ch,CURLOPT_COOKIE,$cookie);
		$tail.=" ".$cookie;
		unset($cookie);
	}
	$r=curl_exec($ch);
	curl_close($ch);
	return $r;
}

How it works:

When you want to send a long php job to background, assuming you have this very same job into the same php user is on but delimited by _is_thread define, you just need to call _go_thread(); , it will save ALL vars into the session_id, do a cURL call the PHP_SELF (you need to define site_full_uri as the full address of the request, like http://192.168.1.65/) this cURL have a timeout of 1s on propouse so the caller script is able to send the reply to the user fast, while the real job is done in background.

Leave a Comment :, , , more...

Pixel by pixel html table to build an image

by z3n on May.20, 2010, under lol

I was looking around for curl timeout issues and found this movie of a guy doing pixel by pixel html table to build an anime

Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!