Skip to content

Instantly share code, notes, and snippets.

View openprojdev's full-sized avatar

yayati openprojdev

View GitHub Profile
@openprojdev
openprojdev / gist:6165357
Created August 6, 2013 15:10
Best guess of clients IP Address
/**
* Retrieves the best guess of the client's actual IP address.
* Takes into account numerous HTTP proxy headers due to variations
* in how different ISPs handle IP addresses in headers between hops.
*/
function get_ip_address() {
// check for shared internet/ISP IP
if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
@openprojdev
openprojdev / gist:6123768
Last active January 20, 2021 07:50
Install python 2.7.x on RHEL5.x from source and create virtualenv
#!/bin/bash
#setup build essentials
yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel
wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
tar zxvf Python-2.7.5.tgz
cd Python-2.7.5
./configure --prefix=/usr/local/python27 --enable-shared
make altinstall
echo "/usr/local/python27/lib" >> /etc/ld.so.conf.d/usr-python27.conf
@openprojdev
openprojdev / gist:6105277
Created July 29, 2013 15:43
Auto versioning of static file based on unix mtime
<?php
/*
* .htaccess
* <IfModule mod_rewrite.c>
* RewriteEngine on
* RewriteCond %{REQUEST_FILENAME} !-d
* RewriteCond %{REQUEST_FILENAME} !-f
* RewriteRule ^(.*)\.(.+)\.(css|js)$ $1.$3 [L] # Strip out the version number
* </IfModule>
@openprojdev
openprojdev / gist:6062247
Created July 23, 2013 13:12
generate UUID , RFC 4122, extracted from cakePHP String class. This snippet depends on $_SERVER var set by the web server, and hence would recommend pecl extension http://pecl.php.net/package/uuid if this function is to be called from the CLI Remember to change the SALT line 52
/**
* Generate a random UUID
*
* @see http://www.ietf.org/rfc/rfc4122.txt
* @return RFC 4122 UUID
*/
function uuid() {
$node = $_SERVER['SERVER_ADDR'];
if (strpos($node, ':') !== false) {
@openprojdev
openprojdev / gist:5975977
Created July 11, 2013 14:32
Mobile Request Redirection using WURFL Lib, and Tera Wurfl PHP Lib http://wurfl.sourceforge.net
//Set mobile check to true
$mobile_check = true;
if (preg_match('/bot|ipad|xoom|sch-i800|playbook|tablet|kindle/i',$_SERVER['HTTP_USER_AGENT'])) {
$mobile_check = false;
}
if ( isset($_GET['desktop']) && !empty($_GET['desktop']) ) {
$mobile_check = false;
setcookie('desktop', 1);
}
@openprojdev
openprojdev / gist:5948608
Last active December 19, 2015 11:38
Identify apache2 web server serving http requests from frontend by investigating the response http headers, were the web servers are under an load balancer. Example Response header should contain, were 30.10 are the last 2 digits of the web server internal IP address like 192.168.30.10: X-Fry : "30:10"
#!/bin/sh
# Only publish the last 2 digits of an IP address, do not expose the complete network range
# place this script post testing some were like /etc/rc.local, were issues like dynamic address, or cloned machines in cloud env will have an updated IP address in http header
# This script should run before http starts, experiment to get it right.
# Configuration section
network_id='192.168' # first 2 digits of the IP address identifying the network, usually would be an private ip address reange.
partial_ip_address=`ifconfig | grep 'inet addr:' | grep $network_id | awk -F: '{print $2}' | awk '{print $1}' | awk -F. '{print $3"."$4}'`
apache_conf_file='/etc/apache2/conf.d/X-fry' # RHEL/CentOS could be /etc/http/conf.d/X-fry
@openprojdev
openprojdev / gist:5946943
Created July 8, 2013 07:44
Log apache2 logs in MongoDB, avoid apache log rotate, Async logging to MongoDB, piped log parser to mongoDB using PHP
#!/usr/bin/php5
<?php
/*
Apache2 Log format in Json
LogFormat "{ \"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \"@fields\": { \"client\": \"%a\", \"duration_usec\": %D, \"status\": %s, \"request\": \"%U%q\", \"method\": \"%m\", \"referrer\": \"%{Referer}i\" } }" log_json
CustomLog "|/var/www/mongolog.php" log_json
*/
@openprojdev
openprojdev / gist:5803965
Created June 18, 2013 09:29
PHP CDN hostnames based on circular array shift, useful to distribute static content via multi hostnames serving the same content i.e increase concurrent downloads by browsers
$cdnHosts = array( //declared in an global config
'h1.hostname.com',
'h2.hostname.com',
'h3.hostname.com',
'h4.hostname.com'
);
/**
*
* cdnHostNames