PHP - Catching session timeout issues

I've been working on a bug where an application is logging out unexpectedly.

To identify the piece of code that is destroying the session namespace I've been using the code snippet:

 ob_start();
 debug_print_backtrace();
 $trace = ob_get_contents();
 ob_end_clean(); 
 file_put_contents(basename(__FILE__).'.out', var_export($trace,true).PHP_EOL.var_export($_SERVER, ,true).PHP_EOL);

What it does is dumps out the stack that called the function it is placed in.

You can add additional information like a timestamp or FILE_APPEND.

You may also want to use Zend's logging. The reason I don't use it is that every instruction adds overhead. Once this issue is resolved, I'll remove all the debug code. I hope.

Credit to the link. :)

Sifting Through Spam

If you are setting up email filters for an account, some useful tactics are:

Display the headers of the emails in the account:

grep -iE "^(subject|from|reply-to|X-Spam-Level):" *

Once you identify messages of interest, you can use more to view them.

If you're using cPanel's filter interface with a RegEx, you can use this to exclude all .eu and .us (and any other) TLDs.

\.(eu|us)>?$

Type it in exactly as displayed. I put it on both the From and Reply-To headers.

Another good rule is to match on the spam score in the X-Spam-Status header, like so:

score=(3|4)

Emails with a spam score of 3 or 4 are rejected with a message that the sender should use the contact form on the site. Almost all of these will be spam, but for the few that aren't, the sender will have a way to resubmit their message.

Be sure to test to make sure it works the way you want it to.

If you find domains that are clearly just spammers, block them explicitly.

Report spam to spam@uce.gov, and scams to the organization that's being misrepresented.

Linux Command Line - Convert XML to JSON

This was the answer to a question that came in an email - "Do you know of a good XML to JSON converter?"

Obviously you need php-cli, JSON, and SimpleXML. :)

The "xml" is the name of the file that contains the XML, the output can be piped to a different file.

php -r 'echo json_encode(simplexml_load_file("xml"));'

If you would like it pretty printed, this page is really nice: http://jsonprettyprint.com/

Upgrading PHP 5.3 to 5.4 - Checking your code (Linux)

If you are upgrading PHP from 5.3 to 5.4 for an existing application, you can use the following commands to check for parse errors.

tree -fi application | grep -E '.php|.phtml' | sed "s/^/php -l /" > files
source files > results

The output is really what comes out of stderr, which will be the parse errors.

Be sure to check out the short_open_tag directive (http://www.php.net/manual/en/ini.core.php#ini.short-open-tag).

By default <?= is enabled, to use <? for statements other than echo, you must enable the short_open_tag option in /etc/php.ini.

HTML5 Tag Challenge

For years I've enjoyed http://www.oneplusyou.com/bb/html_quiz. I have never named all the tags, but I return periodically to see if I can.

For fun, I created and HTML5 version.

I still can't name all the tags.