简体   繁体   English

似乎无法让 php 在 MAMP 中工作

[英]can't seem to get php working in MAMP

I'm trying to learn php and step one is getting php working in some capacity.我正在尝试学习 php,第一步是让 php 以某种方式工作。 I'm attempting to use MAMP but I'm having some trouble.我正在尝试使用 MAMP,但遇到了一些麻烦。

Specifically: if I create a file with the below code and save it as index.html in MAMP's "Document Root" directory, I get a blank page when pointing my browser at http://localhost:8888/index.html .具体来说:如果我使用以下代码创建一个文件并将其保存为 MAMP 的“文档根目录”目录中的 index.html ,当我将浏览器指向http://localhost:8888/index.html时,我会得到一个空白页面。

Code:代码:

<html>
<body>

    <?php
    echo "Hello World!";
    ?>

</body>
</head>

Alternatively, if I put a bit of php into its own file (say test.php) and then point my browser at this file, it just displays the full text of the file in the browser.或者,如果我将一些 php 放入它自己的文件(比如 test.php)中,然后将我的浏览器指向这个文件,它只会在浏览器中显示该文件的全文。

Any ideas what I might be doing wrong?任何想法我可能做错了什么?

I had the similar issue.我有类似的问题。

Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:在 TextWrangler 或 Komodo 中创建一个新文件,然后添加以下代码:

AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm

You're going to save the file as .htaccess (with the dot in the front; this is the file name).您要将文件另存为 .htaccess(前面有点;这是文件名)。 Save it in /Applications/MAMP/htdocs.将其保存在 /Applications/MAMP/htdocs 中。 This is the same place you'll save your php and html files.这与您保存 php 和 html 文件的位置相同。 This .htaccess will be an invisible file;这个 .htaccess 将是一个不可见的文件; you will not see it in Finder, tho you can if you cd to it in Terminal, or searching w/in Finder and choosing the File Visibility type under Kind.你不会在 Finder 中看到它,如果你在终端中 cd 到它,或者在 Finder 中搜索并选择 Kind 下的 File Visibility 类型,你就可以看到它。

Now try going to localhost:8888/ and you should see all of the available files there.现在尝试访问 localhost:8888/,您应该会在那里看到所有可用文件。 And with this newly created .htaccess file, you can now embed php inside an html file too.使用这个新创建的 .htaccess 文件,您现在也可以将 php 嵌入到 html 文件中。

In MAMP, edit the file:在 MAMP 中,编辑文件:

/Applications/MAMP/conf/apache/httpd.conf /Applications/MAMP/conf/apache/httpd.conf

and then search for '#AddHandler type-map' (exclude quotes).然后搜索“#AddHandler type-map”(不包括引号)。 Below that, add,在此之下,添加,

AddHandler application/x-httpd-php .php .html AddHandler 应用程序/x-httpd-php .php .html

Save the file and stop and re-start MAMP.保存文件并停止并重新启动 MAMP。 Php parsing will occur in files ending with the extensions: .php and .html. PHP 解析将发生在以扩展名结尾的文件中:.php 和 .html。

You must save a file with PHP inside it with a .php extension.您必须使用 .php 扩展名在其中保存包含 PHP 的文件。 So you would need to name it index.php instead of index.html .所以你需要将它命名为index.php而不是index.html Simple fix.简单的修复。

So, this just worked for me:所以,这对我有用:

instead of having:而不是:

MAMP/htdocs/folder-that-contains-all-files/ MAMP/htdocs/包含所有文件的文件夹/

put all your files directly in the htdocs folder!将所有文件直接放在 htdocs 文件夹中!

so:所以:

MAMP/htdocs/all your files including index.php etc. MAMP/htdocs/你所有的文件,包括 index.php 等。

Hope that helps!希望有帮助!

Modifying /Applications/MAMP/conf/apache/httpd.conf修改/Applications/MAMP/conf/apache/httpd.conf

searching for #AddHandler type-map搜索 #AddHandler 类型映射

and inserting AddHandler application/x-httpd-php .php .html并插入 AddHandler application/x-httpd-php .php .html

worked for me.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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