简体   繁体   中英

local PHP file doesn't open in browser

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.

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

<!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 .htm file.

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

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

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

You can't just "open" (per double click, as you described) a .php file 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 (eg HTML) form over a proper (HTTP) response.

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

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 .

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.php in the address bar of your browser).

我花了几个小时试图解决这个问题只是为了意识到该文件夹​​没有被授予权限……如果您使用的是 Linux,请确保拥有。

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