Skip to content

Instantly share code, notes, and snippets.

View brianlmoon's full-sized avatar

Brian Moon brianlmoon

View GitHub Profile
@brianlmoon
brianlmoon / gist:1622095
Created January 16, 2012 18:11
Given two timestamps and a integer, returns the Y-m-d range, separated by days of that integer
<?php
/**
* Given two timestamps and a integer, returns the Y-m-d range, separated by days
* of that integer
*
* @param int $start_ts Starting timestamp
* @param int $end_ts Ending timestamp
* @return array
*
* @test
@brianlmoon
brianlmoon / gist:1622938
Created January 16, 2012 20:43
Get weeks from a range
<?php
/**
* Given two timestamps returns the Y-m-d ranges for each week within the range.
*
* @param int $start_ts Starting timestamp
* @param int $end_ts Ending timestamp
* @param bool $full_weeks If true, full weeks will be returned meaning
* the start and end will be adjusted to create full weeks
* @return array
@brianlmoon
brianlmoon / gist:1647760
Created January 20, 2012 15:02
Example output from running GearmanManager with do all workers
$ ./pecl-manager.php -c config-single.ini -w ./pecl-worker-classes -vvv
PID Type Message
25968 INFO Loading workers in ./pecl-worker-classes
25969 PROC Helper forked
25969 INFO Loading workers in ./pecl-worker-classes
25968 PROC Started with pid 25968
25970 WORKER Adding server 127.0.0.1
25970 WORKER Adding job Avg
25970 WORKER Adding job Sum
25968 PROC Started child 25970 (Avg,Sum)
@brianlmoon
brianlmoon / gist:4962688
Last active December 13, 2015 19:28
Here is a function to return you the width the browser is using for media queries. For some reason, it is a bit different in each browser.
function getWindowMediaWidth() {
if(!window.matchMedia){
// it should be this, so return it when we can't
// figure it out. Of course, it does not do a lot
// of good if the browser does not support media
// queries.
return document.body.clientWidth;
}
@brianlmoon
brianlmoon / block_mac_nav.js
Created January 27, 2014 17:23
Block Mac navigation when form has changed
function blockLeaving (e){
if(e.metaKey && (e.keyCode == 39 || e.keyCode == 37)){
switch(document.activeElement.tagName){
case "INPUT":
case "TEXTAREA":
break;
default:
if(!window.confirm("You have unsaved changes. Are you sure you want to leave the page?")){
e.preventDefault();
}
@brianlmoon
brianlmoon / socket_connect_timeout.php
Last active February 26, 2021 06:13
Using socket_connect with a reliable timeout in PHP
<?php
/**
* I was having trouble with socket connections timing out reliably. Sometimes,
* my timeout would be reached. Other times, the connect would fail after three
* to six seconds. I finally figured out it had to do with trying to connect to
* a routable, non-localhost address. It seems the socket_connect call would
* not fail immediately for those connections. This function is what I finally
* ended up with that reliably connects to a working server, fails quickly for
* a server that has an address/port that is not reachable and will reach the
@brianlmoon
brianlmoon / apache_cors_example
Last active November 19, 2023 03:14
CORS example for Apache with multiple domains
# Sets CORS headers for request from example1.com and example2.com pages
# for both SSL and non-SSL
SetEnvIf Origin "^https?://[^/]*(example1|example2)\.com$" ORIGIN=$0
Header set Access-Control-Allow-Origin %{ORIGIN}e env=ORIGIN
Header set Access-Control-Allow-Credentials "true" env=ORIGIN
# Always set Vary: Origin when it's possible you may send CORS headers
Header merge Vary Origin
@brianlmoon
brianlmoon / current_on_object.php
Last active April 21, 2016 23:18
WTF PHP current() on object?
<?php
class Foo {
private $bar = "private variable 1";
private $bar2 = "private variable 2";
}
$foo = new Foo;
echo current($foo)."\n";
@brianlmoon
brianlmoon / pecl_memcached_safe.php
Last active August 29, 2015 14:18
Safest way to add servers using pecl-memcached and persistent connections.
<?php
// Let's assume this is coming from a config file somewhere
// that maybe we can't trust, or could allow for someone to
// accidently add the same host/port twice.
$servers = array(
array("host" => 'localhost', "port" => 11211, "weight" => 10),
array("host" => 'localhost', "port" => 11212, "weight" => 20)
);
@brianlmoon
brianlmoon / pecl_memcached_quick.php
Created April 9, 2015 18:51
Quickest, safe way to add servers using pecl-memcached and persistent connections.
<?php
// Let's assume this is coming from a config file somewhere
// that we can trust to not have duplicate servers in the list.
$servers = array(
array("host" => 'localhost', "port" => 11211, "weight" => 10),
array("host" => 'localhost', "port" => 11212, "weight" => 20)
);
$mc = new Memcached("test");