hits counter

September 2009


Personal20 Sep 2009 02:58 pm

So I’m hear to get over the jetlag before I head to the RealBasic Summit. It’s nearly 9pm, so I should be right to get some sleep soon. I’m definately glad I didn’t arrive on the day of the summit, and expect myself to stay awake. Landed and had a quick walk around town – from Union Square to Haight Ashbury and back through Mission Dolores.

Written by MIlton Lai

Tech Talk14 Sep 2009 02:57 pm

I ran into an issue at the end of last week regarding sessions in PHP. The problem was the locking that happens over the sessions. This isn’t what’s now coded in place, but it was a thought I had (bad idea). So I had a PHP file which did a POST to another file – using the following code

function do_post_request($url, $data, $optional_headers = null) {
	$params = array('http' => array('method' => 'POST','content' => $data));
	if ($optional_headers !== null) {
		$params['http']['header'] = $optional_headers;
	}
	$ctx = stream_context_create($params);
	$fp = fopen($url, 'rb', false, $ctx);
	if (!$fp) {
		throw new Exception("Problem with $url, $php_errormsg");
	}
	$response = stream_get_contents($fp);
	if ($response === false) {
		throw new Exception("Problem reading data from $url, $php_errormsg");
	}
	return $response;
}

Original Source

The receiving page will start off its own session and to try to access the globals in the original session, I tried assigning the sessionid to the new page before calling session_start();.

session_id($_GET['SESSIONID']);
session_start();

What happens is the first page will take a long while before timing out. Behind the scenes, the first PHP page is WAITING for the POST to return, whilst having locked the session variables as already being accessed. The receiving page then tries to “resume” this session and starts to wait for the session variables to be unlocked before so they can be read.
There is the ability to call session_write_close() but this will set the session variables to read only and you will not be able to write to them.

PHP.net user comment

Written by Milton Lai

Tech Talk03 Sep 2009 07:05 pm

I’ve implemented a web service for work which allows you to queue a job, and also to retrieve the job. I also implemented a simple login header which looks after your session through the database. The long term issue that we’ve had in the past is that Safari drops the session (thus losing all global session variables) and also Safari dropping the ability to kill the session in PHP. So I’ve made a simple module which is included on all PHP pages and it sorts out the authentication for you.

The problem came when I tried to introduce this login header to the top of my webpage which “GET”‘s the job. The normal process of forcing a user to download the contents is to use the following code:

<\?php
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=test.txt");
     header("Content-Type: text/plain");
     echo($streamOfText);
     //or
     //readfile("test");
?>

The problem with doing that is if you have other text sitting on that page (or just include ‘login.php’;), since the header forces ALL content to be saved into the file test.txt, and not just $streamOfText.

What I ended up doing to get around this issue, might be a long way to solve the issue, but it works just fine. I created a file in the same disc space and filled it in with the header information as well as reading in another file on the disc. I created the other file with the contents of $streamOfText. At the end of the first file, I unlink the content file as well as itself. The original GET file does a simple header redirect. This leaves no traces of those files ever being created or existing.

JobHandler
	$fp = fopen($JobID . ".php", 'w');
	fwrite($fp, "<\?php " . "\n");
	fwrite($fp, 'header("Cache-Control: public");' . "\n");
	...
	fwrite($fp, "\$filename='" . $JobID . "';" . "\n");
	fwrite($fp, "readfile(\$filename);" . "\n");
	fwrite($fp, "unlink(\$filename);" . "\n");
	fwrite($fp, "unlink(\$JobID . '.php');" . "\n");
	fwrite($fp, "?>" . "\n");
	fclose($fp);

	header('Location:'.$JobID . ".php");

Note: Just clicked on “preformatted” as a markup in MarsEdit and it lets me write code! Very handy, since I tried doing it last time, with all sorts of funny things happening!

Written by Milton Lai

« Previous Page