Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't seem to get php working in MAMP

Tags:

html

php

mamp

I'm trying to learn php and step one is getting php working in some capacity. I'm attempting to use MAMP but I'm having some trouble.

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.

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.

Any ideas what I might be doing wrong?

like image 513
jkjenner Avatar asked Mar 03 '12 22:03

jkjenner


People also ask

Why my PHP code is not working?

If you are running your PHP script on a Windows computer, you need to manually install PHP. If you haven't already done so, your PHP code won't execute. Instructions for the installation process, versions and the system requirements are listed at the PHP website.

Do you need Mamp for PHP?

MAMP stands for Mac Apache MySql PHP, this is needed if you want to run a PHP script on your local computer, not online.


4 Answers

I had the similar issue.

Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:

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). Save it in /Applications/MAMP/htdocs. This is the same place you'll save your php and html files. This .htaccess will be an invisible file; 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.

Now try going to localhost:8888/ and you should see all of the available files there. And with this newly created .htaccess file, you can now embed php inside an html file too.

like image 93
Thomas Avatar answered Sep 30 '22 05:09

Thomas


In MAMP, edit the file:

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

and then search for '#AddHandler type-map' (exclude quotes). Below that, add,

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

Save the file and stop and re-start MAMP. Php parsing will occur in files ending with the extensions: .php and .html.

like image 32
Ralph Frost Avatar answered Sep 30 '22 04:09

Ralph Frost


You must save a file with PHP inside it with a .php extension. So you would need to name it index.php instead of index.html. Simple fix.

like image 34
Joe Torraca Avatar answered Sep 30 '22 06:09

Joe Torraca


So, this just worked for me:

instead of having:

MAMP/htdocs/folder-that-contains-all-files/

put all your files directly in the htdocs folder!

so:

MAMP/htdocs/all your files including index.php etc.

Hope that helps!

like image 26
Roanna Sold Avatar answered Sep 30 '22 04:09

Roanna Sold