Aug
8
2014

Get Actual IP Address with PHP

/** * More reliable function to acquire actual user IP address * @access public * @param none * @return string */ function getip() { if( isset( $_SERVER ) ) { if( isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) { $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif( isset( $_SERVER["HTTP_CLIENT_IP"] ) ) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if( getenv( ‘HTTP_X_FORWARDED_FOR’ ) ) { $realip = getenv( ‘HTTP_X_FORWARDED_FOR’ ); } elseif( getenv( ‘HTTP_CLIENT_IP’ ) […]

Nov
27
2013

Clean sanitized database contents on output

/** * Function to clean data for nice output * * @access public * @param string * @return string */ function clean( $text ) { return stripslashes( html_entity_decode( nl2br( $text ) ) ); }

Jun
21
2013

Force File Download with Correct Content Type

This function will force a specified file to download to the users computer using the path to the file to download, as well as the output filename.  This one is a ‘bit’ heftier, so I’ll add a download as a file as well just to be on the safe side. /** * Force Download * * Generates headers that force a download to happen * Example usage: * force_download( ‘screenshot.png’, ‘./images/screenshot.png’ ); * * @access […]

Jun
17
2013

Remove a directory with all contents

/** * Function to delete all files in a given directory * * @access public * @param string $directory * @return void */ function delete_files( $directory ) { if ( !file_exists( $directory ) ) return false; if ( is_dir( $directory ) && $dir_contents = opendir( $directory ) ) { while ( false !== ( $single_file = readdir( $dir_contents ) ) ) { unlink( $directory . ‘/’ . $single_file ); } closedir( $dir_contents ); } return […]

Jun
17
2013

Make a new directory

/** * Function to make new directory * @access public * @param string full path to new directory * @return bool */ function new_directory( $directory ) { if( !is_dir( $directory ) ) { if( mkdir( $directory ) ) { chmod( $directory, 0744 ); return true; } else { return false; } } }

Jun
9
2013

Validate user Email

/** * Validate email addresses * @access public * @param string email * @return bool */ function valid_email( $email ) { if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { return true; } else { return false; } }

Jun
9
2013

Validate URIs

/** * Validate URL’s * @access public * @param string url * @return bool */ function valid_url( $url ) { if( filter_var( $email, FILTER_VALIDATE_URL ) ) { return true; } else { return false; } }

Jun
9
2013

Output images with fallback and defined variables

/** * Function to output clean images with sizes defined * Checks for absolute OR relative paths and determines if the file exists * Example with local file (Same technique for remote files): * <?php image( ‘assets/images/crowd4.png’, true, ‘Local File Alt tag’, ‘magic-class bigger’ ); ?> * * @access public * @param string url to image (Required) * @param bool specify if the size should be calculated * @param string image alt text (Optional) * […]

Jun
9
2013

Output case sensitive word

/** * Function to provide case sensitive context * Context will always only have suffix for 0 or >2 counts * * @access public * @param int count * @param string word * @param suffix * @return string */ function tense( $count = 0, $word, $append = ‘s’ ) { if( $count == 0 || $count >= 2 ) $return = $word . $append; else $return = $word; return $return; }