News Ticker

Some Useful PHP Codes For Newbie PHP Developers

By Ajay Verma - Wednesday 11 November 2015 No Comments
Hello Friends, one user asked me a PHP code to show real IP of User behind a proxy server, to show on his website.
hence i am writing this blog to provide you Some Useful PHP Codes For Newbie PHP Developers. :)
Lets start with Asked one first.
1. Get Real IP Address of User Behind A Proxy Server
function GetRealIP_By_Unknowndevice64()
if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ip=$_SERVER['HTTP_CLIENT_IP']; // If IP from share internet
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; // If IP from proxy
$ip=$_SERVER['REMOTE_ADDR']; // Else This IP (Use this line without Proxy)
return $ip;

2. Velidate And Verify Email Address (Live With MX-Record)
i have write this code as a tool, you can use this from this link
Now i am sharing the source code here..
 /*** $email Variable of email.. like */
function validate_email_By_Unknowndevice64($email) {
//check for all the non-printable codes in the standard ASCII set,
//including null bytes and newlines, and exit immediately if any are found.
if (preg_match("/[\\000-\\037]/",$email)) {
return false;
$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
if(!preg_match($pattern, $email)){
return false;
// Validate the domain exists with a DNS check
// if the checks cannot be made (soft fail over to true)
list($user,$domain) = explode('@',$email);
if( function_exists('checkdnsrr') ) {
if( !checkdnsrr($domain,"MX") ) { // Linux: PHP 4.3.0 and higher & Windows: PHP 5.3.0 and higher
return false;
else if( function_exists("getmxrr") ) {
if ( !getmxrr($domain, $mxhosts) ) {
return false;
return true;
} // end function validate_email

3. Check And Convert Input Url Without http to http
i am facing this problem several times, when user input is a URL.
so i am using this two line code to Check And Convert Input Url Without http to http.
if (!preg_match("/^(http|ftp):/", $_POST['url'])) //Check for http or ftp
$_POST['url'] = 'http://'.$_POST['url']; //Append Url By Adding http

4. Encode and Decode String Of Base64 in PHP
this one is my favourite one, cause i use this frequently in website development to hide some code.. ;)
function base64_encode_by_unknowdevice64($Text) {
$base64 = base64_encode($Text);
$base64save = strtr($base64, '+/=', '-_,'); //Else Data Will Currupt
return $base64save;
function base64_decode_by_unknowdevice64($Text) {
$base64save = strtr($Text, '-_,', '+/=');
$base64 = base64_decode($base64save);
return $base64;

5. Redirect Current Page To Some Other HTTP Url
One Condition like if Login is true then -> login.php

header('Location: http://home.url/login.php'); // home.url ;)

Else, Similarly redirect to Another Page login.php

6. Detect Browser of User In PHP
some times you works on browser compactibility code below code will help you to detect browser.
function DetectBrowser_By_Unknowndevice64()
$useragent = $_SERVER ['HTTP_USER_AGENT'];
return $useragent;

7. Save image your server from url using PHP
$url = ''; // example url
$image = file_get_contents($url);
file_put_contents('/images/ud64.jpg', $image); //save the image on your server

If You Need Any Specific code you can write to me.. :)

No Comment to " Some Useful PHP Codes For Newbie PHP Developers "