How to get the base url in php

How to get the base url in php

Suppose you have to retrieve the URL of the application in PHP i.e http://example.com
Using this piece of code
//output : example.com
$DOMAIN = $_SERVER['SERVER_NAME']; 
// output: http:// 
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://'; 
// output : http://example.com
$main_url = $protocol.$DOMAIN; 
When you are running the application in the subfolder / subdomain in the main website.I.e http://example.com/myproject/
In this case you want to retrieve the base url of the application not the main site url.

Use this simple function to get the base url of your application.
 
function getBaseUrl() 
{
    // output: /myproject/index.php
    $currentPath = $_SERVER['PHP_SELF']; 

    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) 
    $pathInfo = pathinfo($currentPath); 

    // output: example.com
    $hostName = $_SERVER['HTTP_HOST']; 

    // output: http://
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';

    // return: http://example.com/myproject/
    return $protocol.$hostName.$pathInfo['dirname']."/";
}

$base_url = getBaseUrl();

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
How to embed the user address using google map in php

How to embed the user address using google map in php

If you want to display the user's address in google map in their profile’s page. It is very simple follow these procedure.

Generally in user registration we store the user information like country, state, city or even taken the address fields. Do you want to display the user's address map on their profile page. You must need the address of the user.

This simple piece of code going to display the user's address in embed google map.
<?php  
 $userAddress ="Arundelpet, Guntur, Andhra Pradesh, India";  
?>  
<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $userAddress; ?>&output=embed">  
</iframe>   
Out put:


* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
How to delete the entire directory with in its files in php

How to delete the entire directory with in its files in php

Generally we use the remove directory rmdir() method. In this case the directory will be removed when it has no files on it.
another way is unlink() method. This method can be used for removing the single file from the directory location.

How to download the multiple directories as a zip file in PHP

If you want to remove the directory and its all files and sub directories.
First removes the sub directories and its files after that removes the main directory. This is called as the recursion.
 <?php  
  function deleteDir($dirPath) {  
   if (! is_dir($dirPath)) {  
     throw new InvalidArgumentException("$dirPath must be a directory");  
   }  
   if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {  
     $dirPath .= '/';  
   }  
   $files = glob($dirPath . '*', GLOB_MARK);  
   foreach ($files as $file) {  
     if (is_dir($file)) {  
       deleteDir($file);  
     } else {  
       unlink($file);  
     }  
   }  
   rmdir($dirPath);  
 }  
 deleteDir("Directoryname");  
 ?>  

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
How to get the user ip address in php

How to get the user ip address in php

In many situations developers want's the user ip address.

Those are
In user registration time store his/her ip address.
In login time also store his/her ip address .

In these two factors developer compares the registered ip address and logged ip address. If both are equal no issue when these are not equal then intimate the user "You are logined the unknown ip address".

In other situation when developers want's to block the some of the users ip.
Because of user voilating the site rules webmaster can restrict the access to the perticular ip address.

Some situations completely block the site access to the particular ip address.

This is a simple script to get the users ip address using the php.
 
<?php  
  $client = @$_SERVER['HTTP_CLIENT_IP'];  
  $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];  
  $remote = @$_SERVER['REMOTE_ADDR'];  
  if(filter_var($client, FILTER_VALIDATE_IP)){  
  $ip = $client;  
  }elseif(filter_var($forward, FILTER_VALIDATE_IP)){  
  $ip = $forward;  
  }else{  
  $ip = $remote;  
  }  
 echo $ip;  
?>

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
Live html compiler using php and bootstrap

Live html compiler using php and bootstrap

If you want to make the live html compiler similar to the w3schools.com try editor

What is live html compiler ?
you can see the exact output of the html code before save and run in the browser.

This is a simple technique to develop the live html code in the php.
Here we are going to use the textarea as a input of the html code and iframe as a output of the the input code.

in left textarea give the input of the html code and click the see results button then it renders the html code preview in the right iframe instantly.

index.html
 
 <title> Live html compiler using Php and bootstrap</title>  
 <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">  
 <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>  
 <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>  
 <style type="text/css">  
 iframe { border: 1px silver solid; }  
 textarea { resize:none; }  
 .glyphicon{font-size: 25px;}  
 </style>  
 <div class="container" >  
 <div class="row">  
 <div class="col-md-6">  
 <h2><span class="glyphicon glyphicon-align-justify"></span> Html Code </h2>  
 <form id="htmlcomplier" name="htmlcomplier" action="result.php" target="view" method="post">  
 <div class="form-group">  
 <textarea class="form-control" required rows="24" id="code" Placeholder="Enter your code and Submit" name="code">  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
  <title>Bootstrap Example</title>  
  <meta charset="utf-8">  
  <meta name="viewport" content="width=device-width, initial-scale=1">  
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>  
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
 <div class="container">  
  <div class="jumbotron">  
   <h1>Demo</h1>  
   <p>Demo tag line!</p>  
  </div>  
  <div class="row">  
   <div class="col-sm-4">  
    <h3>Column 1</h3>  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>  
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>  
   </div>  
   <div class="col-sm-4">  
    <h3>Column 2</h3>  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>  
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>  
   </div>  
   <div class="col-sm-4">  
    <h3>Column 3</h3>  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>  
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>  
   </div>  
  </div>  
 </div>  
 </body>  
 </html>  
 </textarea>   
 <br/>  
 <input class="btn btn-success pull-right" type="submit" value="See result" />  
 </div>  
 </form>  
 </div>  
 <div class="col-md-6">  
 <h2><span class="glyphicon glyphicon-list-alt"></span> Result </h2>  
 <div class="form-group">  
 <iframe width="100%" height="510px" frameborder="0" src="result.php" name="view" id="view"></iframe>  
 </div>  
 </div>  
 </div>  
 </div>  

result.php
 
<?php  
$Getcode = @$_REQUEST["code"];  
print $Getcode ;  
?>  

Download Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff