Skip to content

Instantly share code, notes, and snippets.

@apphp-snippets
apphp-snippets / Get Remote IP Address in PHP
Created May 19, 2012 19:10
This code allows to get the IP address from which the user is viewing the current page.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function getRemoteIPAddress(){
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
return $ip;
}
/* If your visitor comes from proxy server you have use another function
to get a real IP address: */
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
$useragent = $_SERVER['HTTP_USER_AGENT'];
echo "<b>Your User Agent is</b>: ".$useragent;
?>
@apphp-snippets
apphp-snippets / gist:2759472
Created May 20, 2012 20:45
This code allows you to Unzip a ZIP file with PHP
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function unzip($location,$new_location){
if(exec("unzip $location",$arr)){
mkdir($new_location);
for($i = 1;$i< count($arr);$i++){
$file = trim(preg_replace("~inflating: ~","",$arr[$i]));
copy($location."/".$file,$new_location."/".$file);
unlink($location."/".$file);
}
@apphp-snippets
apphp-snippets / gist:2759481
Created May 20, 2012 20:49
Allows to perform PHP redirection (must be placed before any browser output)
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
// stick your url here
header('Location: http://you_address/url.php');
exit;
?>
@apphp-snippets
apphp-snippets / gist:2759485
Created May 20, 2012 20:50
This code removes all special characters from the given URL and make it SEO friendly
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function makeMyUrlFriendly($url){
$output = preg_replace("/\s+/" , "_" , trim($url));
$output = preg_replace("/\W+/" , "" , $output);
$output = preg_replace("/_/" , "-" , $output);
return strtolower($output);
}
?>
@apphp-snippets
apphp-snippets / gist:2759487
Created May 20, 2012 20:50
You may use this code to highlight specific words in your displaying search results
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function highlight($str, $words){
if(!is_array($words) || empty($words) || !is_string($str)){
return false;
}
$arr_words = implode('|', $words);
return preg_replace(
'@\b('.$arr_words.')\b@si',
'<strong style="background-color:yellow">$1</strong>',
@apphp-snippets
apphp-snippets / gist:2759488
Created May 20, 2012 20:51
Get short urls for Twitter with PHP
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function getShortUrl($url){
return file_get_contents('http://tinyurl.com/api-create.php?url='.$url);
}
?>
@apphp-snippets
apphp-snippets / gist:2759490
Created May 20, 2012 20:52
Send Mail using mail function in PHP
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
$to = 'myfriend@gmail.com';
$subject = 'Test Email';
$body = 'Body of your message here. You can use HTML tags also, e.g. <br><b>Bold</b>';
$headers = 'From: John Smith'."\r\n";
$headers .= 'Reply-To: from@email.me'."\r\n";
$headers .= 'Return-Path: from@email.me'."\r\n";
$headers .= 'X-Mailer: PHP5'."\n";
$headers .= 'MIME-Version: 1.0'."\n";
@apphp-snippets
apphp-snippets / gist:2759493
Created May 20, 2012 20:53
This function highlight_string() outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
highlight_string('<?php phpinfo(); ?>');
?>
@apphp-snippets
apphp-snippets / gist:2759496
Created May 20, 2012 20:53
his is a very common PHP question of HOW TO remove last character from string in PHP. Find below some ways how to delete last character from string in PHP
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
// method 1 - substr and mb_substr
substr($string, 0, -1);
mb_substr($string, 0, -1);
// method 2 - substr_replace
substr_replace($string, '', -1);
// method 3 - rtrim