Home » PHP GET Base URL Absolute Path Example

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.

Read Also:  Laravel 6 Clear cache Example

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.

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

Read Also:  WooCommerce Get Product Tax, Shipping & Stock

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

//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 :

Read Also:  ng-table using Angular Example

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Module Controller using Angular Example
  2. Read Also:  Top 10 Great Cloud Storage File Sharing Services
  3. Read Also:  Send Simple Mail Function using PHP
  4. Read Also:  Laravel Redirect Back with() Message
  5. Read Also:  How to remove undefined value from Laravel PHP array?
  6. Read Also:  PHP cURL HTTP Request (GET & POST)
  7. Read Also:  Working with datetime in PHP Examples
  8. Read Also:  WooCommerce Get Product Tax, Shipping & Stock
  9. Read Also:  Working with datetime in PHP Examples
  10. Read Also:  PHP cURL HTTP Request (GET & POST)

Leave a Comment

Your email address will not be published. Required fields are marked *