本地 PHP 文件无法在浏览器中打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12998127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:37:30  来源:igfitidea点击:

local PHP file doesn't open in browser

phpxampplocal

提问by stevenvh

I just installed XAMPP on my Windows 7 PC, and wanted to test it with a local PHP file. Double-clicking the file opens a new tab in my browser (Firefox 16.0.1) but the file doesn't open. Instead the tab's label flashes between "connecting" and "new tab" about once per second, and I have a hard time closing the tab again.

我刚刚在我的 Windows 7 PC 上安装了 XAMPP,并想用本地 PHP 文件对其进行测试。双击该文件会在我的浏览器 (Firefox 16.0.1) 中打开一个新选项卡,但该文件未打开。相反,标签的标签大约每秒在“连接”和“新标签”之间闪烁一次,我很难再次关闭标签。

I stopped Apache and even ended XAMPP, but no joy. Even reducing the file to the bare minimum doesn't help:

我停止了 Apache,甚至结束了 XAMPP,但没有任何乐趣。即使将文件减少到最低限度也无济于事:

<!DOCTYPE HTML>
<html>
<head>
    <title>php test</title>
</head>
<body>
    Lorem Ipsum...
</body>
</html>

Firefox does open the file if I rename it to a .htmfile.

如果我将其重命名为文件,Firefox 会打开该.htm文件。

Any ideas what's going on and how to fix it?

任何想法发生了什么以及如何解决它?

回答by jtheman

You cant load the file in browser by doubleclicking in the explorer window, you load it through http protocol:

您无法通过在资源管理器窗口中双击来在浏览器中加载文件,而是通过 http 协议加载它:

http://localhost:8080/folder/file.php 

Where - 8080 should be your XAMPP port if not 80 - folder is the subfolder to htdocs if any - file.php is your script

哪里 - 如果不是 80,则 8080 应该是您的 XAMPP 端口 - 文件夹是 htdocs 的子文件夹(如果有) - file.php 是您的脚本

回答by Havelock

You can't just "open" (per double click, as you described) a .phpfile in your browser. At the very least, because the browser can't interpret PHP.
You need to send a proper (HTTP) request to the server, which will then pass the PHP code to the interpreter, get the results of it and forward it to the browser - in an (for the browser) understandable (e.g. HTML) form over a proper (HTTP) response.

您不能只是.php在浏览器中“打开”(如您所描述的那样,每次双击)一个文件。至少,因为浏览器无法解释PHP。
您需要向服务器发送正确的(HTTP)请求,然后服务器会将 PHP 代码传递给解释器,获取结果并将其转发给浏览器 - 以(对于浏览器)可理解的(例如 HTML)形式通过正确的 (HTTP) 响应。

To be able to initially play around with PHP, create an indexfile (php or html) on which just set links to your .phpfiles, so you can see them in your browser.

为了能够最初使用 PHP,创建一个index文件(php 或 html),在其中设置指向您的.php文件的链接,以便您可以在浏览器中看到它们。

Add
Just saw your comment, then you should be able to see the file in your browser, going to http://localhost/your-subfolder/file.php.

添加
刚刚看到您的评论,那么您应该可以在浏览器中看到该文件,转到http://localhost/your-subfolder/file.php.

The difference: now you're requesting the file through it's URL (http://some.domain/some-file.php) as it usually happens for websites. What you tried first was to open it through it's location on the file system (when you see file:///path/to/some-file.phpin the address bar of your browser).

不同之处:现在您通过它的 URL ( http://some.domain/some-file.php)请求文件,这通常发生在网站上。您首先尝试的是通过它在文件系统上的位置打开它(当您file:///path/to/some-file.php在浏览器的地址栏中看到时)。