Twitter ist… php-Bashing

https://twitter.com/gr4y/status/296543206361948161

php comparison explained

php is an awful language and we all know it. It has a very silly comparison algorithm for loose comparison (==) which I am going to try to explain.

If we compare variables of different types (bool, string, int, float, array, object) with each other, php tries to cast them into a common type. But some types have no “common ground” and can never under any value assignment, be considered equal. This Venn-Diagram shows you, which stuff can be compared with each other to, in any way, get variables that are considered equal.

Weiterlesen

The php comparison game

Recently, I had a discussion with EnnoBigBenno, one of the friends over at senaeh.de about the merits of php (or lack thereof). I believe that a good and simple programming language can easily be parsed and understood by a human. This brought me to the idea, to create a small game of “True or False” where you have to decide, what the php-interpreter thinks, is true or false. Simple as that. Count your correct answers and check you php-prowess.

Warning: This game is not for the faint of heart. And feel free to share your score in the comments.
Weiterlesen

reCaptcha inside a with jQuery ajax() or load() dynamically loaded object

For the German version of this article, please look here.

The Problem: you want to use reCaptcha with the JS (JavaScript) or php-Api in in a with jQuery ajax() or load() dynamically loaded area. But: the reCaptcha doesn’t appear.

My use case looked something like this:

contakt.html (abridged):

<div id="form"></div>
<script>$("#form").load("mail.php?ajax");</script>

mail.php (abridged): 

<?php
// here is other code
echo '<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script><div id="recaptcha"></div>';
echo '<script>Recaptcha.create("my_public_key", "recaptcha", {theme: "red"});</script>';
// other code
?>

And this, like I said, doesn’t work.

The problem:
google’s reCaptcha API uses the document.write() function for inserting reCaptcha in to the document tree. This function is not available in this case, when the content is loaded dynamically with jQuery. document.write() therefore has to be replaced with its jQuery equivalent.

This means you either have to maintain your own version of the api or you work this out dynamically. Our solution looked something like this:

mail.php (abridged): 

echo '<script type="text/javascript" src="jsapi.php"></script><div id="recaptcha"></div>';

jsapi.php (complete):

<?php
$api = file_get_contents('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
$api = str_replace('document.write','$("body").append',$api);
echo $api;
?>

The replacing could be made manually and is highly trivial. Therefore I did not attach a changed version of the recaptcha_ajax.js. You probably need to do something about the header to make the api cacheable for the browser again, but this was not our focus.

reCaptcha innerhalb eines mit jQuery ajax() oder load() dynamisch geladenen Bereichs

For the English version of this article, please look here.

Das Problem: man möchte reCaptcha über die JS (JavaScript) oder php-Api in einen durch den jQuery-Befehl ajax() eingefügten Bereich verwenden. Aber: das reCaptcha taucht nicht auf.

Mein Use-Case sah so aus:

kontakt.html (auszug):

<div id="form"></div>
<script>$("#form").load("mail.php?ajax");</script>

mail.php (auszug): 

<?php
// here is other code
echo '<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script><div id="recaptcha"></div>';
echo '<script>Recaptcha.create("my_public_key", "recaptcha", {theme: "red"});</script>';
// other code
?>

 Und das funktioniert, wie bereits erwähnt, nicht.

Das Problem:
googles reCaptcha-Api verwendet zum Einfügen des Objekts das document.write(), was an dieser Stelle im Dokumentenbaum durch die Verwendung von jQuery nicht verfügbar ist. Die Lösung ist “denkbar” einfach. Googles document.write muss durch einen jQuery-Befehl ersetzt werden.

Das heißt, mann muss entweder eine eigene Version der Api maintainen, oder man arbeitet dynamisch. Bei uns sah die Lösung konzeptionell so aus:

mail.php (auszug): 

echo '<script type="text/javascript" src="jsapi.php"></script><div id="recaptcha"></div>';

jsapi.php (vollständig):

<?php
$api = file_get_contents('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
$api = str_replace('document.write','$("body").append',$api);
echo $api;
?>

Die Ersetzung manuell vorzunehmen ist höchst trivial, darum habe ich jetzt keine geänderte recaptcha_ajax.js angehängt. Insgesamt könnte man an den headern noch etwas machen, dass das Caching-Verhalten des Browsers korrekt wird, das war aber nicht Fokus.