Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the base path/URL

Tags:

url

php

base-path

I am trying to get the base path of the documents through a function as I do not want to find the paths like ../folder1/folder2/mypage.php or ../../../folder1/folder2/somepage.php.

Therefore I tried...

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: localhost
$hostName = $_SERVER['HTTP_HOST'];

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

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

Then i give write the code...

$base = getBaseUrl();
require_once $base.'_include/db/qry.php';
require_once $base.'_include/db/functions.php';

Both the files qry.php & functions.php is in http://localhost/mysite/_include/db/

While i run the page, error shows ...

Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\mysite\_include\header.php on line 9

Warning: require_once(http://localhost/mysite/_include/db/qry.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\mysite\_include\header.php on line 9

Fatal error: require_once(): Failed opening required 'http://localhost/mysite/_include/db/qry.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\mysite\_include\header.php on line 9

I tried by echoing the getBaseUrl() like echo $base; and it is showing the right path i.e. http://localhost/mysite/.

What should I do ?

like image 275
Raja Avatar asked Jun 03 '14 06:06

Raja


People also ask

How do I get the base URL in HTML?

The <base> tag specifies the base URL and/or target for all relative URLs in a document. The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.

What is base URL and base path?

basePath is the URL prefix for all API paths, relative to the host root. It must start with a leading slash / . If basePath is not specified, it defaults to / , that is, all paths start at the host root. Valid base paths: /v2.

How do I get the base URL with PHP?

The base URL can be retrieved from a string using PHP parse_url() function. The following code snippet shows how to get base URL from URL string with PHP. PHP_URL_SCHEME – This component returns URL scheme (http/https). PHP_URL_HOST – This componenet returns host name (example.com/www.example.com).

What is site base URL?

The URL found in the address bar of the front page of a website is its base URL. In other words, the common prefix found while navigating inside a given website is known as the base URL. One can select a base URL from the list of those available with help of the URL general properties page.


2 Answers

you can use $_SERVER['DOCUMENT_ROOT']

like image 186
krozero Avatar answered Oct 20 '22 00:10

krozero


You should just use the absolute path on the server instead of url.

You could get the base path by using __DIR__.

For example:

// just example, change to fit your real path.
$base = __DIR__ . '/../';

require_once $base.'_include/db/qry.php';
require_once $base.'_include/db/functions.php';
like image 33
xdazz Avatar answered Oct 20 '22 00:10

xdazz