Skip to content

Instantly share code, notes, and snippets.

View ThijsFeryn's full-sized avatar

Thijs Feryn ThijsFeryn

View GitHub Profile
@ThijsFeryn
ThijsFeryn / input_coerce.php
Created July 14, 2016 14:25
Coerce string into an integer. No type error thrown despite the type hint.
<?php
function double(int $argument):int {
return $argument*2;
}
var_dump(double("2"));
@ThijsFeryn
ThijsFeryn / catch_as_type_error_error_or_throwable.php
Created July 14, 2016 13:10
Catch a type error either as a type error, an error, or a throwable
<?php
function double(int $argument):int {
return $argument*2;
}
try {
var_dump(double([3]));
} catch (TypeError $e) {
echo "Caught as type error: ".$e->getMessage().PHP_EOL;
}
@ThijsFeryn
ThijsFeryn / catch_type_error.php
Created July 14, 2016 12:50
Catch type errors in PHP 7
<?php
declare(strict_types=1);
function double(int $argument):int {
return $argument*2;
}
try {
var_dump(double("3"));
} catch (TypeError $e) {
@ThijsFeryn
ThijsFeryn / update_find_and_replace_by_last_occurrence.sql
Last active June 9, 2016 10:14
Update statement that does a find and replace based on the last occurrence of a string
UPDATE `table` SET `somefield` = CONCAT(TRIM(TRAILING 'feryn.eu' FROM `name`),'thijs.be') WHERE 'otherField' = 'value'
@ThijsFeryn
ThijsFeryn / find_and_replace_by_last_occurrence.sql
Last active June 9, 2016 10:13
Find and replace in MySQL by last occurrence
SELECT CONCAT(TRIM(TRAILING 'feryn.eu' FROM 'feryn.eu.bla.feryn.eu'),'thijs.be')
@ThijsFeryn
ThijsFeryn / varnish_proxy_support_custom_headers.vcl
Created January 20, 2016 13:02
Send custom headers to the application to check client.ip, server.ip, local.ip & remote.ip
vcl 4.0;
backend default {
.host = "10.10.10.53";
.port = "80";
}
sub vcl_recv {
set req.http.x-clientip = client.ip;
set req.http.x-serverip = server.ip;
@ThijsFeryn
ThijsFeryn / varnish_proxy_support.php
Created January 20, 2016 13:00
Print custom headers set by Varnish to display the different IP's and print the X-Forwarded-For header
<?php
echo "Client IP: " . $_SERVER["HTTP_X_CLIENTIP"]. "<br />".PHP_EOL;
echo "Server IP: " . $_SERVER["HTTP_X_SERVERIP"]. "<br />".PHP_EOL;
echo "Local IP: " . $_SERVER["HTTP_X_LOCALIP"]. "<br />".PHP_EOL;
echo "Remote IP: " . $_SERVER["HTTP_X_REMOTEIP"]. "<br />".PHP_EOL;
echo "X-Forwarded-For: " . $_SERVER["HTTP_X_FORWARDED_FOR"]. "<br />".PHP_EOL;
@ThijsFeryn
ThijsFeryn / haproxy_proxy_protocol_support.cfg
Created January 20, 2016 12:52
Combine HTTP backends & PROXY backends in HAProxy
global
daemon
defaults
mode http
frontend http-in
bind *:80
default_backend servers
backend servers
@ThijsFeryn
ThijsFeryn / varnish_proxy_support_startup_options.txt
Created January 20, 2016 12:44
Adding PROXY protocol support by adding an extra listener on a separate port. Explictly add the PROXY keyword to enforce the protocol
/usr/sbin/varnishd -a :6081 -a :6083,PROXY -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,5g
@ThijsFeryn
ThijsFeryn / 11_int_div.php
Created November 30, 2015 14:18
PHP 7 integer division
<?php
/**
* Integer division
*/
var_dump(
intdiv(10, 3),
(10/3)
);