Trailing Whitespace Causes a Session to Be Destroyed in CakePHP

While working on a new project using CakePHP, I’ve come across a weird problem. In one of the controllers, the session always came out as invalid, as

$this->Session->valid()

always returned false. I tried debugging this weird stuff, and it looked like all the session variables were unset. Using

$this->Session->error()

to get the last error returned “Config doesn’t exist”. After further debugging, “Config” turned out to be an internal array that is saved by CakePHP to the session and holds various internal data (some of it is used for validation, like a user-agent hash). I kept printing more and more debugging data, as well as looking at CakePHP’s trac system.

I found an interesting bug (ticket #4217), and it looked very promising, as it almost fully described my problem. Unfortunately, the solution offered didn’t seem to work for me. But it inspired me to try starting the session manually using session_start() instead of using Cake’s startup and activate methods of the Session Component.

I found out that session_id() returned an empty string. Luckily, calling session_start() directly from the controller gave me a lead. The session seemed to work well, but a nasty error about headers already being sent showed up.

A little more investigation led me to realize that I had a trailing newline after my closing PHP tag in that controller file. Deleting this trailing whitespace completely solved the problem. There was no need anymore to manually start the session. It’s pretty annoying that such a small thing as a trailing newline can cause such seemingly unrelated problems in CakePHP’s session handling.

Maybe CakePHP should add a little debug notice when the session doesn’t start because headers were already sent. This can be done by modifying the else statement in the __startSession() method in cake/lib/session.php (line 557 in version 1.2.0.6311). I wonder what the reason was for not informing the developer when such an event happens, as I don’t see why someone would deliberately try to start the session after sending the headers. I think it only happens by mistake, at least most of the time.

Setting Up OmniComplete (Autocompletion) for wxWidgets in Vim

I use Vim as my main IDE for C/C++-related development (as well as for almost all other development). If you use (or are thinking about using) Vim as an IDE, you better get some good autocompletion functionality. This kind of autocompletion is provided by OmniComplete, which has been available since Vim 7.0. Just having OmniComplete is a nice thing, but it’s much more helpful if it’s configured properly to work with the libraries you use, such as wxWidgets. In this post, I will show you how to get OmniComplete working for wxWidgets. However, the procedure I will show can be easily adapted to almost all libraries.
Continue reading Setting Up OmniComplete (Autocompletion) for wxWidgets in Vim

Activating Guarddog Settings on Startup

Like many Linux users, I use Guarddog as a frontend to my iptables firewall. At some point, I noticed that Guarddog started acting strangely. Every time I restarted my computer, all internet traffic was blocked (both incoming and outgoing). The only way to fix this situation was to open Guarddog and press “Apply” (without making any changes). While it was annoying, it didn’t bother me much because I used to restart my computer about once a month. But a few days ago, I decided to solve this problem once and for all.
Continue reading Activating Guarddog Settings on Startup

Delete Unversioned Files Under SVN

Sometimes svn switch fails because unversioned files exist in the working copy, and the need to erase them comes up. This is also true when updating to an old revision (usually one that misses directories that exist in the head revision), doing some work (like compiling), and then trying to update back to the head revision. Subversion (SVN) will fail, complaining that directories/files already exist.
Continue reading Delete Unversioned Files Under SVN

Vim Macros for Wrapping Strings for Gettext

I’m working on a website, and we decided to localize it using GNU gettext. Soon enough, I found it tiring to wrap each string manually in _( and ), and also to do it in Smarty (using {t}string{/t}). So I decided that I needed a macro that would let me highlight the string that needs translation, and the macro would wrap it for me.

I ended up writing two macros: one for PHP files (but it’s also good for C/C++, etc.) and one for Smarty.

:vmap tg di_(<ESC>pa)<ESC>
:vmap ts di{t}<ESC>pa{/t}<ESC>

To use these macros, just highlight the string for translation in Vim’s visual mode and press tg (or ts), and your string will be wrapped for translation.

Multibyte String Truncate Modifier for Smarty – mb_truncate

When working with Smarty, a PHP templating engine, I discovered that while the regular truncate modifier works great on ASCII strings, it doesn’t work with multibyte strings, i.e., UTF-8-encoded strings. This leads to problems in internationalization (i18n), as UTF-8 is the popular encoding for non-Latin alphabets nowadays. The problem can be solved by modifying the built-in truncate modifier and creating a new one that takes an additional argument, the charset of the string, and acts accordingly. The new modifier, mb_truncate, is implemented below.
Continue reading Multibyte String Truncate Modifier for Smarty – mb_truncate

C’s “Goes To” Operator

Well, it isn’t really an operator, but this is a nice C code construct I ran into. It doesn’t seem to have any benefit except as a nice way to create a reverse loop:

int count = 100;
while (count-->0) {
        //some work
}

As I said, I don’t think I’ll find any performance benefit in using this code snippet. On the other hand, it is always fun to see the puzzled face other programmers have the first time they see the code.

Optimizing for Loops: Reverse Loops

for loops are basic language constructs in many languages. One of the first things to look at when optimizing code is loops, as they do considerable amounts of work (like going through a very large amount of data) in very little code.

If you use a for loop, but you don’t really care about the order in which the loop is executed – to be more precise, if you can afford reversing the loop – you can save quite some time. By reversing the loop, I mean that instead of giving the index values from 0 to 10, for example, you go from 10 downward to zero. This doesn’t seem like a big change, but when carefully implemented, this can easily improve the performance of your for loops.
Continue reading Optimizing for Loops: Reverse Loops