With PHP 5.4 you can now easily add a callback function, using header_register_callback, to be called just before the headers are sent to the browser. For example: 1 2 3 4 5 6 header(’Content-Type: text/plain’); header(’X-Test: test-header’); header_register_callback( ‘remove_XPoweredBy_header’ ); echo ‘Testing’; This allows you to set a callback function, in this case, remove_XPoweredBy_header. An example of something… more
Getting and Setting HTTP Response codes in PHP 5.4
A new function were added to PHP 5.4 that make getting and setting HTTP response code easy. To get the current HTTP code, all you have to do is call the http_response_code function 1 echo http_response_code() //If all is ok, this should echo 200 To set the HTTP response code, add a parameter to the http_response_code function 1 2 http_response_code(404)… more
Class Member Access on Instantiation with PHP 5.4
Class members can now be accessed on instantiation in PHP 5.4. This is a very handy addition. Before PHP 5.4 you had to create an object into a variable, and then access the class member/function: 1 2 3 //The old way $object = new SomeClass(); $object->someFunction(); With PHP 5.4 you can access the class member directly on instantiation: 1 2… more
Function Array Dereferencing in PHP 5.4
PHP now supports array dereferencing directly from a function call. Before PHP 5.4 you had to store the returning value from a function into a variable, and then use the variable. Eg: 1 2 $cars = $explode(",", "ferrari,lamborghini,maserati,bugatti"); echo $cars[3]; //bugatti Now with PHP 5.4 you can do it like this: 1 echo $explode(",", "ferrari,lamborghini,maserati,bugatti")[3]; //bugatti Another example with your… more
The Shorter Array Syntax in PHP 5.4
PHP 5.4 introduced a new shorter array syntax, similar to the syntax used in JavaScript. The old, longer syntax: 1 2 //The old way $cars = array("ferrari", "lamborghini", "maserati", "bugatti"); The new syntax is an alternative to the old syntax. 1 2 //The short way $cars = ["ferrari", "lamborghini", "maserati", "bugatti"];
Check if PHP is using HTTPS (SSL)
I had to come up with a way to check if PHP is running in SSL mode today. This will work on both Apache, Nginx and IIS 1 2 3 4 5 6 7 8 9 10 11 12 function is_ssl() { $secure_connection = false; if (!empty($_SERVER[’HTTPS’]) && $_SERVER[’HTTPS’] !== ‘off’ || $_SERVER[’SERVER_PORT’] == 443) { $secure_connection = true;… more
Detecting a Multi-Dimensional Array in PHP
I recently had to find a quick way to check if an array is a multi-dimensional array, only to find that there are no PHP built-in function to do this. Here is a quick and easy way to do it: 1 2 3 4 function is_multi_array( array $arr ) { return is_array($arr[key($arr)]); } This can easily be used in the… more
Making a Radio Group Tab-able Using jQuery
Recently I had to come up with a solution to make radio boxes tab-able. Mozilla Firefox allows you to tab over each radio box in a group, however, all other browsers do not. The solution is actually quite simple. First, in your HTML make sure to name your radio boxes with the same name, but also add a .1, .2,… more
PHP Safe Redirect Function
I see PHP code every day making use of the header function. Its the standard way to make redirects in PHP, but will fail if ANY output was sent to the browser before its called. There is a safer way to do this, which use JavaScript and meta redirects as a fail safe: 1 2 3 4 5 6 7… more
PHP use Backticks as Execution Operators
PHP use backticks as execution operators, which will return the output of to be assigned to a variable. Any code between backticks will be executed as a shell command – the backtick operator is identical to shell_exec() One example where I found this useful was to get the fully qualified domain name of a server. I could do something like:… more