Skip to content

Instantly share code, notes, and snippets.

View Danack's full-sized avatar

Danack

  • Bristol, England
View GitHub Profile
@Danack
Danack / setCookie.php
Created May 30, 2013 18:16
Set cookie function - part of old codebase.
function setCookieVariable($cookieName, $value, $secureOnly = false){
$timeSeconds = 60 * 60 * 24 * 30; //30 days in the future.
// if($GLOBALS['outputStarted'] == true){
// logToFileFatal("Trying to set cookie '$cookieName' but page output has already started. That's bad.");
// header('X-OUTPUT-STARTED: 12345');
// return;
// }
if($secureOnly == true){
@Danack
Danack / composerRunner.php
Created June 21, 2013 22:44
Composer Runner - calls composer update from a PHP script. Not recommended practice at all but may be necessary for people using web servers where they don't have shell access.
//<?php
//
//Yes you can run Composer with a little PHP wrapper. All of the Composer source code is available in the Phar file, so it can be extracted and then you can run it after setting up an InputInterface to replace Composer expecting the commands to be passed in via the command line.
//
//If you setup your directory structure like this:
//
//./project
//./project/composer.json
//./project/composer.lock
//./project/webroot/composerExtractor.php
@Danack
Danack / gist:5841738
Created June 22, 2013 17:36
emitCSV fields
function emitCSV(array $fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) {
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$output = array();
foreach ($fields as $field) {
if ($field === null && $mysql_null) {
$output[] = 'NULL';
continue;
}
@Danack
Danack / AutoConstructor
Created September 23, 2013 17:33
An example of using reflection to avoid typing.
<?php
trait Auto{
function auto($args) {
@Danack
Danack / Index.html
Created November 17, 2013 17:58
Example of session behaviour. Default PHP locks the session object so that only one process can modify it at once, i.e. the numbers in the two frames will always be different. In APCu PS they can be duplicated.
<html>
<body>
<iframe src='frame1.php' width='200px' height='400px'></iframe>
<iframe src='frame2.php' width='200px' height='400px' ></iframe>
</body>
@Danack
Danack / gist:7534959
Created November 18, 2013 20:43
Crontab setup.
#!/bin/bash
#m: minute of the day, from 0 to 59
#h: hour of the day, from 0 to 23
#dom: day of month, from 1 to 31
#Month
#dow: day of week, from 1 to 7
#user: user your cron job should run as
#command: command or script that should run
@Danack
Danack / Maker.php
Created December 25, 2013 14:20
Example of object returned from factory not being return type hinted.
<?php
class Maker {
function make($className) {
return new $className;
}
}
@Danack
Danack / gist:8220375
Created January 2, 2014 14:55
parsing class names
function getClassName($namespaceClass) {
$lastSlashPosition = mb_strrpos($namespaceClass, '\\');
if ($lastSlashPosition !== false) {
return mb_substr($namespaceClass, $lastSlashPosition + 1);
}
return $namespaceClass;
}
@Danack
Danack / closure factoru
Created January 6, 2014 15:35
Closure based factory.
<?php
function createTimerProxyXMySQLiStatementFactory(\Intahwebz\Timer $timer) {
$closure = function ($statement, $queryString, $logger) use ($timer) {
$object = new \Intahwebz\DB\TimerProxyXMySQLiStatement(
$statement, $queryString, $logger, $timer
);
return $object;
@Danack
Danack / gist:8292673
Created January 7, 2014 00:35
Timezone crap.
function getOffsetTime(timezoneString){
var start = timezoneString.indexOf("(");
var end = timezoneString.indexOf(")");
var length = end - start - 1;
var offsetString = timezoneString.substr(start + 1, length);
return offsetString;
}