简体   繁体   中英

PHP absolute relative URL issue during site migration

I am migrating my PHP files to a new server. On the old server, I have used absolute URLs in my require_once and include_once directives. To be clearer, the absolute URLs specify the location from where index.html is stored.

However, the new server requires relative URLs. So if A.php requires to include B.php , the relative path to B.php from A must be specified.

Is there a setting in php.ini which enforces the PHP interpreter to accept absolute URLs?

No there is not. If You set path to var/www/something.php it is relative, if You set path to /var/www/something.php (or C:\\\\var\\\\www\\\\something.php in windows) it is absolute

There is a way of configuring your new server to accept your URLs as relative to index.html .

You are looking for a directive include_path in your php.ini . The solution is to create php.ini file in the same directory with index.html (in the root directory of your site) and set:

include_path="/path/to/your/directory"

Then, all the include and require instructions will try to search the file in the specified directory first.

Here is the documentation for this directive: http://php.net/manual/en/ini.core.php#ini.include-path

Note that not all hosting providers will let you use your own php.ini file. I mean, sometimes that new php.ini is just ignored. Then, you will need to find a way of changing the original php.ini

Other way is using $_SERVER['DOCUMENT_ROOT'] constant:

include_once $_SERVER['DOCUMENT_ROOT'] . '/file.php'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM