Skip to content

Instantly share code, notes, and snippets.

View HendrikEduard's full-sized avatar

Hendrik Eduard Kuiper HendrikEduard

View GitHub Profile
@HendrikEduard
HendrikEduard / lat-lon.js
Last active August 15, 2018 15:47
This script [in Javascript] calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
@HendrikEduard
HendrikEduard / an_ordinal.php
Last active September 10, 2018 07:34
PHP function to create ordinal suffixes.
<?php
// Disclaimer: This is not the most modern method of doing this.
function ordinal($num) {
$test = abs($num) % 10;
$suffix = ((abs($num) %100 < 21 && abs($num) %100 > 4) ? 'th'
: (($test < 4) ? ($test < 3) ? ($test < 2) ? ($test < 1)
? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $num.$suffix;
}
@HendrikEduard
HendrikEduard / ini_set.php
Last active August 15, 2018 15:45
PHP ini_set error log options
<?php
ini_set('log_errors', TRUE); // Error logging
ini_set('error_log', 'your/path/to/errors.log'); // Logging file
ini_set('log_errors_max_len', 1024); // Logging file size
@HendrikEduard
HendrikEduard / Database.php
Last active August 15, 2018 15:44
PHP PDO Database class
<?php
class Database extends PDO {
// These variable should be in a separate file outside of your root directory
private $host = '127.0.0.1';
private $db = 'database_name';
private $user = 'anything_but_root';
private $pass = 'anything_but_password';
private $charset = 'utf8mb4';
@HendrikEduard
HendrikEduard / functions.php
Last active August 15, 2018 15:43
PHP database data cleansing functions for some of your form fields.
function in($data) {
$data = strip_tags($data);
$data = stripslashes($data);
return trim($data);
}
function out($data) {
return htmlentities($data, ENT_QUOTES, 'UTF-8');
}
@HendrikEduard
HendrikEduard / functions.php
Last active August 15, 2018 15:42
Simple troubleshooting function to check the contents of an array or variables.
// var_Dump and Die
function dnd($data) {
echo '<pre>';
var_dump($data);
echo '</pre>';
die();
}
@HendrikEduard
HendrikEduard / greeting.php
Last active August 16, 2018 09:05
PHP function to greet visitors with the name and the segment of the day.
<?php
date_default_timezone_set ('Europe/Amsterdam');
function greeting() {
$day = date("l");
$hour = date("H");
if($hour < "12") {
$time = " morning ";
} elseif ($hour >= "12" && $hour < "18") {
@HendrikEduard
HendrikEduard / form_token.php
Last active August 15, 2018 15:40
PHP function to generate a secure token to use in a hidden field in forms.
<?php
function form_token() {
$token = bin2hex(random_bytes(33));
return $token;
} // Usage
// $form_token = form_token();
// echo "$form_token";
@HendrikEduard
HendrikEduard / functions.php
Last active August 28, 2018 10:34
A PHP function to test for non alpha-numeric characters in a form field.
<?php
function check_for_non_alpha_numeric($str) {
$reg = '/[^[:alnum:]$]/';
preg_match_all($reg, $str, $matches, PREG_SET_ORDER, 0);
if($matches) {
print "<hr>oops, try again.<pre>";
var_dump($matches);
} else {
@HendrikEduard
HendrikEduard / fibonacci.php
Created September 4, 2018 09:57
Create the Fibonacci Sequence in PHP
<?php
function fibonacci($n) {
$current = 1;
$previous = 0;
for ($i = 0; $i < $n; $i++) {
yield $current;
$temporary = $current;
$current = $previous + $current;
$previous = $temporary;