且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP中的基本URL

更新时间:2023-02-26 17:56:12


我修补了大部分 $ _SERVER [] 标签和
属性,以及 parse_url()


不要修补它们。没有干净/自动的方式来做你正在做的事情。只需在配置文件&不要担心 - 相对路径再次。如果你需要设置一个基本的URL,那么这个过程是相似的。



所以就文件路径而言,你应该明确地设置一个 $ BASE_PATH 如下:

  $ BASE_PATH ='/ full / path / to / your /代码库/位置/'; 

如果您不知道您的文件系统基本路径是什么,只需将此行代码放入你的PHP代码;像 index.php

  echo你的路径是:。 realpath(dirname(__ FILE__)))。 < br />; 

然后加载该页面。您的路径为:/ full / path / to / your / codebase / here /




然后用这个设置,你可以将你的代码改成如下:



然后设置您的 include_once ,如下所示:

  include_once $ BASE_PATH。 包括/ myfile.php; 

有些人可能会说你应该使用 $ _ SERVER ['DOCUMENT_ROOT'] 甚至 dirname(__ FILE __)直接意味着您可以简化代码可移植性。但文件路径设置为安装的方式可能会有所不同,所以它永远不会很好你在奇怪的服务器上怪异的可能性很高。



***只需手动设置一个 $ BASE_PATH 在配置文件中,当您移动代码,而不是处理由PHP常量导致的头痛,如 $ _ SERVER 在安装,设置和配置。



只要使用基本URL,只需遵循与本地开发设置相同的思路:

  $ BASE_URL ='/ websitename /'; 

这是在您的生产服务器上:

  $ BASE_URL ='/'; 

所以用 $ BASE_URL 设置,那么你可以这样做:


几乎所有链接都有基本脚本,除了
包括头文件和

现在,您只需在 $ BASE_URL &你应该很好去。


I've got a bit of a dilemma, and it's been bothering me for quite some time. I have a local testing server that's set up like so: 127.0.0.1/

My website in offline mode looks like this:

127.0.0.1/websitename/index.php

My live version of the website looks like this:

websitename.com/index.php

I've got the base script for almost all of the links, except for the including header and footer files.

The links in the footer and header files work except for the home page (on the root of the website).

Could anyone redirect me to a proper method of doing multi sub directory base URL linking for both offline and online?

I’ve tinkered around with most of the $_SERVER[] tags and attributes, as well as parse_url().

I’ve tinkered around with most of the $_SERVER[] tags and attributes, as well as parse_url().

Don’t tinker with them. There’s no clean/automated way to do what you are doing. Just set a base path manually in a config file & don’t worry about it—relative paths—ever again. And if you need to set a base URL, the process is similar.

So as far as a file base path goes, you should explicitly set a $BASE_PATH like this:

$BASE_PATH = '/full/path/to/your/codebase/here/';

If you don’t know what your file system base path is, just place this line of code in your PHP code; like index.php:

echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";

Then load that page. Somewhere near the top will be this text:

Your path is: /full/path/to/your/codebase/here/

Then with that set you can change your code to be something like this:

And then set your include_once like this:

include_once $BASE_PATH  . 'includes/myfile.php';

Some might say you should use $_SERVER['DOCUMENT_ROOT'] or even dirname(__FILE__) directly with the implication being that you can simplify code portability that way. But the way file paths are set for installs can vary so it just never works well & the chances of you getting snagged on an odd server quirk is high.

It’s always best to just to manually set a $BASE_PATH in a config file when you move code than deal with the headaches caused by PHP constants like $_SERVER not being consistent between installs, setups & configurations.

And as far as a base URL goes, just follow the same thinking with this being on your local development setup:

$BASE_URL = '/websitename/';

And this being on your production server:

$BASE_URL = '/';

So with that $BASE_URL set then you can just do this:

I’ve got the base script for almost all of the links, except for the including header and footer files.

Now just prepend any path you might need requested via a URL with $BASE_URL & you should be good to go.