3

is there a way to view local PHP files in my browser without running Apache or anything? I'm using Snow Leopard.

2
  • 2
    Do you want to see the native PHP code in your local browser or do you want to the rendered HTML equivalent? Jul 28, 2010 at 21:54
  • 1
    Do you want to see the actual PHP code or you want to run the scripts and see the output (in a browser or CLI)?
    – Tom
    Jul 29, 2010 at 11:32

3 Answers 3

8

A browser can not show you the output of a PHP file. This is because a PHP file needs to be run by an interpreter (the php interpreter). A browser has no means to do this, it just expect HTML code or in the best cases it will show you the content of .php file as a text.
However, there are a few ways you can see the output of a PHP file. You can run any PHP file from Terminal. cd into the folder where your php file is, and run it like this php ./my_file.php You will see the output in the Terminal window. The output will be the HTML code if your PHP generates HTML code. If this is the case, you can php ./my_file.php > output.html and open the output.html file in your browser.
Please note, there is no way you can interact with your PHP files. You have no way to fill in HTML forms and click HTML buttons.
These rules are general for all operating systems, not just MacOS.

1

If you don't want to argue with apache configuration (which is shipped with snowleopard), you can install MAMP(the free version) ! I've a very easy to use Apache + MySQL solution, that will allow you to run php pages locally.

0
1

If you only see code in a browser window with web sharing turned on, you may not have PHP turned on, and therefor need to edit httpd.conf located here: hard drive/private/ect/apache2/httpd.conf

It's hidden, but you can open with bbedit. Do a search for 'PHP'. Uncomment (remove hash from) the first instance you find to turn on PHP and save. The line looks like this:

LoadModule php5_module libexec/apache2/libphp5.so

Be sure to restart Apache as the last step (turning web sharing off and on again will do it)

To test that PHP is now working, create a PHP test file in your new user level web root (~/Sites) with the following command:

printf "<?php phpinfo(); ?>" > ~/Sites/phpinfo.php

to perform this in Terminal, use this guide: https://ole.michelsen.dk/blog/setup-local-web-server-apache-php-macos-x-mavericks.html

1
  • 1
    Please read the question again carefully. Your answer does not answer the original question. This is the opposite of what the OP asked for.
    – DavidPostill
    Dec 12, 2015 at 23:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .