Skip to content

Instantly share code, notes, and snippets.

View gr8pathik's full-sized avatar

Pathik Gandhi gr8pathik

View GitHub Profile
@gr8pathik
gr8pathik / flattenArray.js
Created July 18, 2017 12:25
Flatten Array - Javascript
function flattenArray(array) {
var arrayPool = [];
if(array.length > 0) {
for(var i=0; i < array.length; i++){
if(array[i].constructor === Array){
arrayPool = arrayPool.concat(flattenArray(array[i]));
} else {
arrayPool.push(array[i]);
}
}
@gr8pathik
gr8pathik / getRealIpAddress.php
Created December 3, 2013 07:28
Get Real IP Address in PHP
<?php
function getRealIPAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
@gr8pathik
gr8pathik / directory_listing.php
Created December 3, 2013 07:30
Directory Listing in PHP
<?php
function list_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
@gr8pathik
gr8pathik / encode_decode.php
Created December 3, 2013 07:27
Base64 Encode and Decode String in PHP
<?php
function base64url_encode($plainText) {
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_,');
return $base64url;
}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');
$base64 = base64_decode($base64url);
@gr8pathik
gr8pathik / mail.php
Created December 3, 2013 07:27
Send Mail using mail function in PHP
<?php
$to = "viralpatel.net@gmail.com";
$subject = "VIRALPATEL.net";
$body = "Body of your message here you can use HTML too. e.g. <br> <b> Bold </b>";
$headers = "From: Peter\r\n";
$headers .= "Reply-To: info@yoursite.com\r\n";
$headers .= "Return-Path: info@yoursite.com\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
@gr8pathik
gr8pathik / function.php
Created December 2, 2013 12:55
Auto convert URL into clickable hyperlink - Source: http://zenverse.net/php-function-to-auto-convert-url-into-hyperlink/
<?ph
function _make_url_clickable_cb($matches) {
$ret = '';
$url = $matches[2];
if ( empty($url) )
return $matches[0];
// removed trailing [.,;:] from URL
if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
$ret = substr($url, -1);
<?php
function get_client_language($availableLanguages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $availableLanguages)){
return $choice;
}
@gr8pathik
gr8pathik / gzcompress.php
Last active December 30, 2015 00:19
Compress data using gzcompress() - When working with strings, it is not rare that some are very long. Using the gzcompress() function, strings can be compressed. To uncompressed it, simply call the gzuncompress() function as demonstrated below:
<?php
$string =
"Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,
sapien vel feugiat vestibulum, nulla dui pretium orci,
non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
@gr8pathik
gr8pathik / code.php
Last active December 30, 2015 00:19
Check if server is HTTPS
<?php
if ($_SERVER['HTTPS'] != "on") {
echo "This is not HTTPS";
}else{
echo "This is HTTPS";
}
@gr8pathik
gr8pathik / php_errors_email.php
Last active December 30, 2015 00:19
Email PHP errors instead of displaying it
<?php
// Our custom error handler
function pg_error_handler($number, $message, $file, $line, $vars){
$email = "
<p>An error ($number) occurred on line
<strong>$line</strong> and in the <strong>file: $file.</strong>
<p> $message </p>";
$email .= "<pre>" . print_r($vars, 1) . "</pre>";