Skip to content

Instantly share code, notes, and snippets.

View cballou's full-sized avatar

Corey Ballou cballou

View GitHub Profile
@cballou
cballou / login-table.sql
Created March 25, 2012 14:28
Securing Your PHP Sessions with a Random + Unix Timestamp Salt (old, use bcrypt)
CREATE secure_login (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`email` VARCHAR(120) NOT NULL,
`salt` VARCHAR(8) NOT NULL,
`password` VARCHAR(40) NOT NULL,
`session` VARCHAR(40) DEFAULT NULL,
`disabled` TINYINT(1) UNSIGNED DEFAULT 0,
# your hidden salt will be the reverse of the created_dt value
`created_dt` INT(11) UNSIGNED,
`modified_ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
@cballou
cballou / class-c-ip.php
Created March 25, 2012 14:34
Securing Your PHP Sessions - Helpers
<?php
// sample IP
$ip = '192.168.1.100';
/**
* Trims the IP address and returns it in the
* format XXX.XXX.XXX.0
*/
function trimIP($ip) {
$pos = strrpos($ip, '.');
@cballou
cballou / get-ip-address-optimized.php
Created March 26, 2012 00:51
PHP - Advanced Method to Retrieve Client IP Address
<?php
function get_ip_address() {
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
// trim for safety measures
$ip = trim($ip);
// attempt to validate IP
if (validate_ip($ip)) {
@cballou
cballou / kohana-image-dimension-callback-validation.php
Created March 26, 2012 01:01
Kohana Callback Function to Validate Uploaded Image Sizes
<?php
class Test_Model extends Model {
const HEIGHT = 100;
const WIDTH = 100;
const EXACT_SIZE = true;
/**
* Test function demonstrating how to utilize a callback function to
* validate an image's height and width requirements.
@cballou
cballou / zend-bootstrap-viewhelper.php
Created March 26, 2012 13:55
Override the Default Zend Escape Method
<?php
// you may need to add the view helper directory to your include path for autoloading
set_include_path(
APPLICATION_PATH . '/views/helpers' . PATH_SEPARATOR .
get_include_path()
);
// load the view instance, add a new helper path, and override the default escape mechanism
$view = Zend_Layout::getMvcInstance()->getView();
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper');
@cballou
cballou / youtube-vimeo-embed-urls.php
Created March 27, 2012 15:52
PHP Function to Convert Youtube and Vimeo URLs to Lightbox-Ready Equivalents
<?php
/**
* Given a string containing any combination of YouTube and Vimeo video URLs in
* a variety of formats (iframe, shortened, etc), each separated by a line break,
* parse the video string and determine it's valid embeddable URL for usage in
* popular JavaScript lightbox plugins.
*
* In addition, this handler grabs both the maximize size and thumbnail versions
* of video images for your general consumption. In the case of Vimeo, you must
* have the ability to make remote calls using file_get_contents(), which may be
@cballou
cballou / box-model-sizing-fix-example.css
Created April 10, 2012 12:20
The below code fixes the box model for the given block level elements, meaning that
/* the following div will remain at 100% width even with padding applied */
div.padded {
width: 100%;
padding: 20px;
}
@cballou
cballou / example-secure-hash-class-usage.php
Created April 21, 2012 19:35
Secure PHP Authentication Revisited - Using bcrypt
<?php
/**
* The code below shows example usage of the SecureHash class for
* encrypting a password. In terms of additional usage, you should
* store the resulting encrypted password in addition to the salt
* in your db.
*/
// load the class
$secure = new SecureHash();
@cballou
cballou / relative-position.js
Created May 11, 2012 14:12
Implementing relative positioning with jQuery Tools Tooltip Plugin
// assumes you have a parent container with relative positioning
$('a.my_tooltip').tooltip({
effect: 'fade',
relative: true,
predelay: 300,
onBeforeShow: function() {
var
config = this.getConf(),
$tip = this.getTip();
@cballou
cballou / php-clean-string.php
Created May 24, 2012 17:43
Clean a string pasted from an unknown source (i.e. Microsoft Word) that might have invalid UTF-8 chars
<?php
function clean($value, $escape = true, $quotes = true)
{
$a = array(
'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë',
'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø',
'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å',
'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò',
'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā',
'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č',