为什么我无法在 XAMPP 上的 require 或 include 语句中使用变量?
在清理一些旧的 php 脚本时,当我尝试使用变量时,我注意到 require/include 语句出现了一些奇怪的行为。
在实时服务器上,以下代码工作正常..
<?php
$test = "http://localhost/elearning/trunk/mypage.php";
require "$test";
?>
..但是在我的 XAMPP 安装((基本包)版本 1.6.7)上,我收到以下错误:
警告:require() [function.require]: URL file-access在 C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php 第 22 行的服务器配置中禁用
警告:require(http://localhost/elearning/trunk/mypage.php) [function.require]:无法打开流:否合适的包装器可以在 C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php 第 22 行找到
致命错误:require() [function.require] : 无法打开所需的 'http://localhost/elearning/trunk/mypage.php' ( include_path='.;C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\php\pear\') 在 C:\Documents and Settings\username\Desktop\xampp-win32-1.6 中。 7\xampp\htdocs\elearning\trunk\test.php 第 22 行
如果我复制+粘贴 http:// /localhost/elearning/trunk/mypage.php (直接来自错误)进入我的浏览器,mypage.php 加载。 这是我的配置错误还是我的方法错误?
While cleaning up some old php scripts I've noticed some weird behavior with require/include statements when I try to use variables.
On the live server, the following code works fine..
<?php
$test = "http://localhost/elearning/trunk/mypage.php";
require "$test";
?>
..but on my XAMPP installation ((basic package) version 1.6.7) I receive the following error:
Warning: require() [function.require]: URL file-access is disabled in the server configuration in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22
Warning: require(http://localhost/elearning/trunk/mypage.php) [function.require]: failed to open stream: no suitable wrapper could be found in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22
Fatal error: require() [function.require]: Failed opening required 'http://localhost/elearning/trunk/mypage.php' (include_path='.;C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\php\pear\') in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22
If I copy+paste http://localhost/elearning/trunk/mypage.php (directly from the error) into my browser, mypage.php loads. Is this an error in my configuration, or my approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
出于安全考虑,您不能在某些服务器上使用以 http:// 开头的路径。 您想要使用的是目录路径。
您可以通过执行以下操作来获取当前目录路径
echo $_SERVER['DOCUMENT_ROOT'];
这将为您提供服务器根文件夹的目录路径。 这是您想要用来包含/需要内容的路径。
如果您不太明白,请尝试这个。
有两种类型的路径:
万维网路径:
http://example.com/directory/file.php
服务器目录路径:
/home/usr/www/site/html/
服务器目录路径是文件在服务器硬盘上的位置。 就像计算机的硬盘驱动器一样,除非您移动文件,否则它永远不会改变。 另一方面,万维网路径(每个人用来访问您的网站的路径)可以根据您使用的域、文档根指向的位置、Mod 重写等进行更改。
注意:文档根目录是服务器提供文件的最顶层目录。 因此,如果您的文档根目录中有index.php,它将在网络上显示如下:
http://example.com/index.php.
当 PHP 查找文件时,它使用服务器目录路径,因为它在服务器上运行。 当从用户计算机运行的 Javascript 想要查找文件时,它会使用万维网路径来访问该文件,因为它不是从服务器访问该文件。
我真的希望其中的某个地方有一些有意义的东西。
You can't use paths that start with http:// on some servers because of security. What you want to use instead is a directory path.
You can get your current directory path by doing something like
echo $_SERVER['DOCUMENT_ROOT'];
that will give you your directory path from the root folder of the server. That is the path you want to use to include/require stuff.
If you didn't quite understand that, try this.
There are two types of paths:
World Wide Web Paths:
http://example.com/directory/file.php
Server Directory Paths:
/home/usr/www/site/html/
The Server directory path is where your files are located on the server's hard drive. Much like your computer's hard drive, it is never changes unless you move the files. On the other hand, The World Wide Web Path (the one everyone uses to access your website) can change based on what domain you are using, where your Document Root is pointing, Mod Rewrites, and more.
Note: The Document Root is the top most directory that your server serves files from. So, if you had index.php in your document root, it would show up like this on the web:
http://example.com/index.php.
When PHP looks for a file, it uses the Server Directory Path, because it is running on the server. When Javascript, which runs from the user computer, wants to look for a file, it uses the World Wide Web path to access it, because it isn't accessing it from the server.
I really hope that somewhere in there, something made sense.
尝试要求“./mypage.php”
不是整个确切的目录
try require "./mypage.php"
not the whole exact directory
当您使用类似的 URL 表示法时,PHP 不够聪明,无法知道您正在从同一服务器获取文件,因此它认为您正在从单独的服务器获取文件(这可能很危险)。
您的服务器配置设置为禁止加载这些外部文件(错误消息“URL 文件访问在服务器配置中被禁用”表明),因此失败。
由于您位于同一服务器上,因此您可以简单地使用相对路径,例如:
PHP isn't smart enough to know that you're including from the same server when you use URL notation like that, so it thinks you're grabbing files from a seperate server (which can be dangerous).
Your server configuration is set to disallow loading these external files (evident by the error message "URL file-access is disabled in the server configuration"), so it's failing.
Since you're on the same server, you can simply use relative paths, like:
不要使用这样的路径...使用相对路径或绝对路径。
例子
Don't use a path like that ... use a relative path or absolute path.
Example