Skip to content

Instantly share code, notes, and snippets.

@jrivero
jrivero / get_ip_address.php
Created March 15, 2012 10:02
The most accurate way to retrieve a user's correct IP address in PHP
<?php
// found http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php
function get_ip_address()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (explode(',', $_SERVER[$key]) as $ip){
$ip = trim($ip); // just to be safe
@jrivero
jrivero / index.php
Created March 14, 2012 22:40
Implementing Twitter sign-in with Silex and PHP
<?php
define('CONS_KEY', 'Application consumer key');
define('CONS_SECRET', 'Application consumer secret');
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
// register the session extension
$app->register(new Silex\Extension\SessionExtension());
@jrivero
jrivero / array_reindex.php
Created March 8, 2012 18:05
Array reindex
<?php
// found at http://php.net/manual/en/function.array-filter.php
function array_reindex(array $source, $blacklist = array())
{
$i = 0;
foreach ($source as $key => $val) {
if ($key != $i) {
unset($source[$key]);
@jrivero
jrivero / LargestTables.sql
Created February 23, 2012 23:10
Finding out largest tables on MySQL Server
-- http://www.mysqlperformanceblog.com/2008/02/04/finding-out-largest-tables-on-mysql-server/
SELECT CONCAT(table_schema, '.', table_name),
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
@jrivero
jrivero / OptionParser.py
Last active February 21, 2024 11:35
OptionParser boilerplate
if __name__ == '__main__':
parser = OptionParser()
options_a = [
["-s", "--spiders", dict(dest="spiders", type="int", default=1, help="sends more than one spider")],
["-S", "--nospinner", dict(dest="spinner", action="store_false", default=True, help="turns off the spinner")],
["-v", "--verbose", dict(dest="verbose", action="store_true", default=False, help="outputs every request (implies --nospiner)")],
["-d", "--depth", dict(dest="depth", type="int", default=-1, help="does a breadth-first crawl, stopping after DEPTH levels (implies --breadth)")],
["-b", "--breadth", dict(dest="breadth", action="store_true", default=False, help="does a breadth-first crawl; may be used with --depth")],
]
for s, l, k in options_a:
@jrivero
jrivero / gist:1387602
Created November 23, 2011 00:54 — forked from jsl/gist:1387560
SELECT DISTINCT message_id
FROM SELECT *, id AS message_id FROM user_messages WHERE user_id = ?
AND read = false AND parent_id IS NULL AS initial_messages
UNION
SELECT *, parent_id AS message_id FROM user_messages WHERE user_id = ?
AND read = false AND parent_id IS NOT NULL AS replies;
@jrivero
jrivero / uuid_pad.php
Last active February 21, 2024 11:35
Create uuid with specific dimensions
<?php
function uuid_pad($pad = 6)
{
return substr(md5(uniqid(rand(), true)), $pad, $pad);
}
// example
echo uuid_pad();
@jrivero
jrivero / versionIE.js
Created July 17, 2011 01:38 — forked from padolsey/gist:527683
A short snippet for detecting versions of IE in JavaScript without resorting to user-agent sniffing
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
@jrivero
jrivero / csv_splitter.py
Created July 15, 2011 20:33 — forked from palewire/csv_splitter.py
A Python CSV splitter
import os
def split(filehandler, delimiter=',', row_limit=10000,
output_name_template='output_%s.csv', output_path='.', keep_headers=True):
"""
Splits a CSV file into multiple pieces.
A quick bastardization of the Python CSV library.
Arguments:
@jrivero
jrivero / jsCookies.js
Last active February 21, 2024 11:36
Pure Javascript Cookies Management
// found on http://snipplr.com/view/36790/jscookies--my-simple-easy-pure-js-javascript-cookies-function/
// create my jsCookies function
var jsCookies = {
// this gets a cookie and returns the cookies value, if no cookies it returns blank ""
get: function(c_name) {
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {