如何使用 PHP 将导航栏 href 设置为绝对路径?
How can I use PHP to set my navbar hrefs to absolute paths?
我需要将这些锚点 href 标签设置为绝对路径,因为当我在文件夹中有 html 文档时,我包含很多内容的 header 不起作用(需要 .. / 在某些情况下)。
我看过一些建议使用的帖子:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
或
$root = "http://" . $_SERVER['SERVER_NAME'];
第一个选项导致错误:
localhost/:1 Not allowed to load local resource: file:///C:/xampp/htdocs/trips/index.php%7D
第二个选项导致此错误:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
Warning: include(http://localhost/MountainPlanner/includes/db.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
Warning: include(): Failed opening 'http://localhost/MountainPlanner/includes/db.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
我正在使用 XAMPP 如果这有区别的话。
谢谢!
在您的配置文件或常量文件中的某处设置第一行,甚至在您的文件中设置第一行。
<?php $base_url = "http://localhost/mysite/"; ?>
然后你可以像这样创建 href 链接:
<a href="<?=base_url?>my-link">clickie</a>
HREF 导航栏路径 != 系统路径
URL路径
这是您在浏览器地址中看到的内容,它被 HTML(CSS、JavaScript 等...)使用。 PHP 不用担心(某些流函数除外)。
为了动态创建基本路径,我使用了这个脚本
httpProtocol = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http' : 'https';
$base = $httpProtocol.'://'.$_SERVER['HTTP_HOST'].'/';
并且,当放入 href
a
标签时:
<a href="<?php echo $base; ?>link" title="Link">Link</a>
系统路径
服务器使用它来查找服务器上的任何文件。 PHP 将其用于 include
/require
服务器上的任何文件或共享文件。 Streams 函数也可以使用系统路径和 URL。
正如我所见,您询问的是系统路径。我已使用此代码规范化应用程序路径:
ini_set('include_path',
implode(
PATH_SEPARATOR,
array_merge(
array(dirname(__FILE__)),
explode(PATH_SEPARATOR , ini_get('include_path'))
)
)
);
然后,我的应用程序根目录可以用作我的应用程序的绝对路径:
/Application/Require.php
/Application/Script.php
index.php
它也适用于任何文件:
require('Application/Require.php');
require('Application/Script.php');
$_SERVER 包含由网络服务器填充的 headers。它不可靠,它会阻止您的代码在本地或测试环境中成为 运行。来自 php.net:
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
总而言之,不要依赖它来提供有关您的服务器的信息。
PHP 提供魔术常量 __FILE__
和 __DIR__
为您提供当前文件的文件或目录的完整路径。
对于 PHP 包含,您应该将这些常量与您要访问的文件的相对路径一起使用。
例如:
include __DIR__ . '/../file_in_previous_directory.php';
include __DIR__ . '/file_in_same_directory.php';
我需要将这些锚点 href 标签设置为绝对路径,因为当我在文件夹中有 html 文档时,我包含很多内容的 header 不起作用(需要 .. / 在某些情况下)。
我看过一些建议使用的帖子:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
或
$root = "http://" . $_SERVER['SERVER_NAME'];
第一个选项导致错误:
localhost/:1 Not allowed to load local resource:
file:///C:/xampp/htdocs/trips/index.php%7D
第二个选项导致此错误:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
Warning: include(http://localhost/MountainPlanner/includes/db.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
Warning: include(): Failed opening 'http://localhost/MountainPlanner/includes/db.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
我正在使用 XAMPP 如果这有区别的话。
谢谢!
在您的配置文件或常量文件中的某处设置第一行,甚至在您的文件中设置第一行。
<?php $base_url = "http://localhost/mysite/"; ?>
然后你可以像这样创建 href 链接:
<a href="<?=base_url?>my-link">clickie</a>
HREF 导航栏路径 != 系统路径
URL路径
这是您在浏览器地址中看到的内容,它被 HTML(CSS、JavaScript 等...)使用。 PHP 不用担心(某些流函数除外)。
为了动态创建基本路径,我使用了这个脚本
httpProtocol = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http' : 'https';
$base = $httpProtocol.'://'.$_SERVER['HTTP_HOST'].'/';
并且,当放入 href
a
标签时:
<a href="<?php echo $base; ?>link" title="Link">Link</a>
系统路径
服务器使用它来查找服务器上的任何文件。 PHP 将其用于 include
/require
服务器上的任何文件或共享文件。 Streams 函数也可以使用系统路径和 URL。
正如我所见,您询问的是系统路径。我已使用此代码规范化应用程序路径:
ini_set('include_path',
implode(
PATH_SEPARATOR,
array_merge(
array(dirname(__FILE__)),
explode(PATH_SEPARATOR , ini_get('include_path'))
)
)
);
然后,我的应用程序根目录可以用作我的应用程序的绝对路径:
/Application/Require.php
/Application/Script.php
index.php
它也适用于任何文件:
require('Application/Require.php');
require('Application/Script.php');
$_SERVER 包含由网络服务器填充的 headers。它不可靠,它会阻止您的代码在本地或测试环境中成为 运行。来自 php.net:
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
总而言之,不要依赖它来提供有关您的服务器的信息。
PHP 提供魔术常量 __FILE__
和 __DIR__
为您提供当前文件的文件或目录的完整路径。
对于 PHP 包含,您应该将这些常量与您要访问的文件的相对路径一起使用。
例如:
include __DIR__ . '/../file_in_previous_directory.php';
include __DIR__ . '/file_in_same_directory.php';