PHP GET Base URL Absolute Path Example

Today, We want to share with you PHP GET Base URL Absolute Path Example.In this post we will show you PHP: How to get Main or Base URL?, hear for How to get base url, current url in PHP we will give you demo and example for implement.In this post, we will learn about PHP Get Absolute Path, Document Root, Base URL with an example.

PHP GET Base URL Absolute Path Example

There are the Following The simple About PHP GET Base URL Absolute Path Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop php get base domain url from String, so the some php Way to get base domain url for this example is following below.

Also Read This πŸ‘‰   how to insert data in database in wordpress plugin?

PHP: How to get Main or Base URL?

$base_url = getBaseUrl();
echo "<pre>";
echo $base_url;
echo "</pre>";
//Output Like https://www.pakainfo.com/student-management/

Get Base Path Method

/**
 * Example, I am browsing run this in my domain  pakainfo.com 
 * https://www.pakainfo.com/student-management/index.php?id=8
 */
function getBaseUrl() 
{
    // output: /student-management/index.php
    $currentPath = $_SERVER['PHP_SELF']; 
    
    // output: Array ( [dirname] => /student-management [basename] => index.php [extension] => php [filename] => index ) 
    $pathInfo = pathinfo($currentPath); 
    
    // output: pakainfo.com
    $hostName = $_SERVER['HTTP_HOST']; 
    
    // output: http://
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
    
    // return: https://www.pakainfo.com/student-management/
    return $protocol.$hostName.$pathInfo['dirname']."/";
}

PHP Get Absolute Path

/var/www/path/pakainfo.com/httpdocs/path/directory
$base_dir = __DIR__;
echo $base_dir;

PHP Get Document Root

php get base domain url Example

/var/www/path/pakainfo.com/httpdocs
$doc_root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']);
echo $doc_root;

PHP Get Base URL

BASE_URL and ROOT_PATH

Example of the PHP Get Absolute Path, Document Root, Base URL

Free Live Chat for Any Issue

//PHP GET base directory
$base_dir = __DIR__;

//PHP GET Your server protocol
$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';

//PHP GET domain name
$domain = $_SERVER['SERVER_NAME'];

//PHP GET base url
$base_url = preg_replace("!^${doc_root}!", '', $base_dir);

//PHP GET server port
$port = $_SERVER['SERVER_PORT'];
$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port";

//PHP GET put em all together to get the complete base URL
$url = "${protocol}://${domain}${disp_port}${base_url}";

echo $url; // = https://www.pakainfo.com/path/directory

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read This πŸ‘‰   Laravel detect device and redirect to Mobile-tablet website in PHP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP GET Base URL Absolute Path Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.