❯~/pixelastic

Parsing error in cakePHP view for empty file

As a convention, I never write the closing PHP tag at the end of my php files. It helps in avoiding the "header already sent" error when dealing with cookie/sessions.

However, today cake complains about

Parse error: syntax error, unexpected $end in /var/www/website/app/views/players/default.ctp on line 1

My default.ctp file only contain the following content:

<?php

It took me a couple of minutes to fix it. I only added an empty line after the opening tag, so it now read:

<?php

The first statement was a perfectly valid php file, but somehow it makes cakePHP fail.

I didn't have time to investigate on it further, nor submit a bug report (because it may well be coming from my custom app, not cake itself) but I plan to.

Losing session on each request with cakePHP and Chrome

I finally found solution for one of the more tenacious bug I ever encountered. Share the joy!

I had a website working perfectly under Firefox but when browsing using Chrome, I noticed that my Session gets regenerated on each page load. Constantly. Creating hundred and hundred of useless session files.

And only with Chrome.

Since when using a browser should change the server behavior? Well I don't exactly know what Chrome is doing with the referer but it seems that it is altering it in some ways.

And cakePHP forces the setting of session.referer_check to true, thus checking that multiple requests with the same PHPSESSID comes from the same url.

As one posted on php.net:

If you have a value specified for session.referer_check you may run into difficulty when someone accesses your site and attempts to log in with a mis- capitalized URL.  The logon will fail because any calls to session_start() will result in the existing session being trashed and a new one being created. This becomes a bigger problem when the logon is followed by a header("Location: ...") redirect, because the session_start() at the top of the page will fail.

Those two settings combined, and you got a hell of a mess. I first found a quick fix by forcing the setting of session_start() in app/webroot/index.php. But after more [reading](http://www.nirvaat.com/blog /web-development/session-issue-in-iis-with-cakephp/) and debugging I finally found the culprit.

Hacking your way through the fix

There is no easy way to prevent cake from setting this setting, but you can define your own session handler in the Session.save configure key.

Just create file named session_custom.php in app/config/ and set Configure::write('Session.save', 'session_custom'); in your core.php file.

And in that file, just drop the following lines (copy/paste from cake_session.php)

ini_set('session.referer_check', '');                    // Killing this f***ing config that was causing so much trouble with Chrome
ini_set('session.use_trans_sid', 0);                    // No session id in url
ini_set('session.name', Configure::read('Session.cookie'));    // Using custom cookie name instead of PHPSESSID
ini_set('session.cookie_lifetime', $this->cookieLifeTime);    // Cookie like time, depending on security level
ini_set('session.cookie_path', $this->path);                // Cookie path

Memcache keys not getting saved correctly

Update: We were initially blaming Memcache not correctly setting/getting keys. We should instead have blamed him for being over-zealous in clearing its list. See bottom of post for details.

Today we were having a bit of an issue with memcache. We had to write a thousand keys into it, reading them from a JSON file.

This code had been running happily on our server for the past two weeks. But as the number of keys increased, we came to spot that some keys seemed to be randomly cleared.

After further investigation (and a lot of hours), it appears that the keys were simply not set at all, or set to an empty/false value. This was really strange because it occured inside a loop on our JSON file, and never occurs for the same keys, nor everytime.

We took our server under high stress, forcing it to reload data very often, to trigger the bug once more. After a lot of test, we gather some more data, but nothing really came out. The missed keys were really random.

Finally, we decided to code a fast and dirty fix, until we found a better solution. Basically, we simply checks that the value is correctly saved after saving it, and if not, we retry. And we do so recursively.

function set($key, $val, $recursionLevel = 0) {
  Cache::write($key, $val);
  if (Cache::read($key)===false) {
    if ($recursionLevel>10) die('Possible infinite recursion);
    $this->set($key, $val, $recursionLevel);
  }
}

Note, that this will not work if one of your values is defined as false, otherwise you'll end up in an infinite loop.

Update

Well, the problem kept sporadically popping. And we finally found the REAL culprit.

Cake provides multiple Cache config, using different settings. One would use a special config to set different cache duration. Each setting would use a different key prefix.

Unfortunatly, memcache has no way of finding keys starting with a special prefix. So when we do a Cache::clear(), it just wipes clear the whole memcache keys, no matter what config you specify.

We were running two different websites using the same memcache server, and when doing a clear on one website, it cleared keys on the second one as well.

We finally started one memcache server per site.

Multiple foreach() with references in php

It took me a couple of hours to debug: I had an array with values that shouldn't be here, values from another part of the application, and I couldn't figure out where that came from.

Well, it turns it was part of a documented feature of PHP:

Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it. Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().php.net

My code was as simple as:

$newArray = array();
$newArray2 = array();
foreach($list as $key => &$data) {
  $newArray[] = $data;
}
foreach($list2 as $key => &$data) {
  $newArray2[] = $data;
}

This resulted in the latest index of $newArray being set to one of $list2 values (didn't know exactly which). This is really counter intuitive.

Firebug 1.7 moving headers to raw POST source

Edit: The issue discussed here has been fixed in Firebug 1.9a2

It took me a whole day to track this bug down. At first I thought something was wrong in my request, I tried to replay it using Firebug and I kept hitting a "malformed request" error.

Debugging it in reverse order, using Wireshark and other tools I finally came to the conclusion that my request was indeed valid.

So what?

The guilty part was indeed Firebug. When I ran a simple POST request from Flash, and inspect it from Firebug, I could spot that no Content-Length nor Content-Type headers were displayed in the "Headers" part. I then saw that the "source" part of the POST tab contains my missing headers. What was that ?

HTTP POST Request are supposed to be split into two parts. First part are the headers, each separated by a \r\n. Then, after and empty line was the POST raw content. Seeing that my headers gets mixed up with thePOSTdata I thought that the request was malformed and a \r\n was added too early.

I checked with Wireshark and with PHP on the back end, and no, the request was indeed absolutly valid. Wireshark did not show any \r\n that shouldn't be there and PHP correctly parsed my request. The only issue was the details displayed by Firebug.

So it's a display bug? I can live with it.

Well, actually, it's a little more than just a display bug. It kind of blocks my usual debugging workflow.

To debug an XHR request, I don't usually restart the whole page request just to debug one of the inner request.

The Firebug ability to "open request in new tab" is a time saver for me. I can play and replay the same request over and over until it's debuggued.

But with this bug, I can't use this feature. Opening the request in new tab will not send the slipped headers, and will instead send them in the POST source, resulting in a corrupted posted data.

I've posted an issue on the Firebug bug tracker. I hope it will get resolved soon. Until then, I'm using Live HTTP Headers to capture and replay the requests I need to debug.

Update

Seems like the bug come from Firefox internals and not Firebug. More details [on this post.](http://groups.google.com/group/mozilla.dev.platform/browse_thr ead/thread/1faabb0d0a8d26f7?hl=en)

Edit: Seems to be resolved in Firefox, will be available in next release (8.0 I guess).