Viewing .php files locally

I'm trying to test some .php pages locally and preview them in Safari (and other browsers), but none of them render the code (they just show the actual code in the window). I should mention, these pages end in .php but they don't contain any actual php code. I used to be able to preview these pages locally and now I can't. Does anyone know a workaround for this?

Thanks..

Powerbook 15" 1.25, Mac OS X (10.4.8)

Posted on Feb 6, 2007 5:12 PM

Reply
19 replies

Feb 12, 2007 3:25 AM in response to Gbowe

Hi. No, it's a local issue. I'm talking about viewing the pages locally. But what I need to make 100% is that I'm not talking about viewing any actual php code. Most of the cases are that I have a page of mostly straight HTML, but the page extension is .php. Then in the middle of the page somewhere there's a php include tag. I don't need to view what that include is calling up. I just need to view my page in general as I'm working on layout. This USED to be possible on early version of Mac OSX, Safari and other browsers. I have no idea why this has changed.

So, to summarize, I'm talking about the browsers not rendering the pages locally, just because of the .php extention. The browser either tells me it doesn't know what to do with this type of file and asks me to choose another app, or in the case of Safari it shows me the page code in the browser window (and yes, I've already set the helper app preferences for .php files to be handled by the browser).

Anything I can do besides changing the .php extensions to .htm? That will be a pain in the neck.

Thanks...

Feb 12, 2007 5:29 AM in response to Scott F.

Did not know it was possible to view .php files locally by browser.

If you have apache and php (both installed by default), why not put the files in you web server folder and view them through the web server?

If you look inside Safari.app: Show Package Contents/Contents/Info.plist, then look under

Root:CFBundleDocumentTypes:8:CFBundleTypeExtensions, you can see a list of supported protocols (maybe if you add php?)

<key>CFBundleTypeExtensions</key>
<array>
<string>html</string>
<string>htm</string>
<string>shtml</string>
<string>jhtml</string>
</array>

Feb 12, 2007 5:46 AM in response to Gbowe

Did not know it was possible to view .php files
locally by browser.


Well, at least it used to be 😉

If you have apache and php (both installed by
default), why not put the files in you web server
folder and view them through the web server?


Sounds good! Forgive my ignorance, but where is this folder located? I can't seem to find it.

If you look inside Safari.app: Show Package
Contents/Contents/Info.plist, then look under

Root:CFBundleDocumentTypes:8:CFBundleTypeExtensions,
you can see a list of supported protocols (maybe if
you add php?)

<key>CFBundleTypeExtensions</key>
<array>
<string>html</string>
<string>htm</string>
<string>shtml</string>
<string>jhtml</string>
</array>


So, this would be an option if the web server folder idea doesn't work, correct? I like the first idea better, because I need to test the layout/code across multiple browsers.

Feb 13, 2007 4:06 PM in response to Scott F.

New problem. I just tried your recommendation:


If you look inside Safari.app: Show Package
Contents/Contents/Info.plist, then look under

Root:CFBundleDocumentTypes:8:CFBundleTypeExtensions,
you can see a list of supported protocols (maybe

if
you add php?)

<key>CFBundleTypeExtensions</key>
<array>
<string>html</string>
<string>htm</string>
<string>shtml</string>
<string>jhtml</string>
</array>


And Safari wound't launch anymore. Then I went back and removed the extra line regarding php, resaved the file and tried to launch Safari again ... still no luck. I restarted, repaired permissions,etc... still, nothing. Safari won't launch. So, what should I do about this? I can't find an installer on the apple site for Safari's latest verion. It seems to only come with the OS.

Thoughts?

Feb 13, 2007 7:19 PM in response to Scott F.

Odds are you can just copy a PLIST file from another Safari App and replace yours.

As for PHP. Thats the easy part here.
Firstly enable your webserver. Apple Menu -> System Preferences -> Sharing -> Services. Check Personal web sharing and that will enable Apache. Opening the port in your firewall is up to you. If you are worried about someone trying to hack your computer then you might want to close it, but in the long run it won't hurt you to have it open.

To make sure the webserver is running type in to Safari, Firefox, Opera, Cameo, or any other browser installed on your computer. http://127.0.0.1/. You should see a page saying "If you can see this, it means that the installation of the Apache web server software on this system was successful."
If you are on a different computer type in the "IP address" found in Apple Menu -> System Preferences -> Network of the computer that you are enabling Apache on. Depending on your network setup this number may change upon reboot.

Now Apache is running. To place files in your webserver, put them in /Library/WebServer/Documents/. Or you can put files in the sites folder of your home directory. ~/Sites/. Files that you put in your sites folder of your home directory can be accessed by typing in http://127.0.0.1/~<username>/ Where <username> is your username. For example http://127.0.0.1/~frank/. (Or again insted of 127.0.0.1 using your IP address)

From here install PHP from http://entropy.ch/software/macosx/php/. The installer will guide you through the steps of installing PHP.

Once PHP is installed, any file you put in /Library/WebServer/Documents/ or ~/sites/ will be executed if you load them from http://127.0.0.1/<filename>, or from http://127.0.0.1/~<username>/<filename>.


Note thought that any file you just double click on will not be executed. This is because the PHP parsing engine is within Apache, not your web browser.

Feb 14, 2007 5:20 AM in response to siriusfox

Thanks so much for this REALLY well written, clear, patient response! One question ... is there a chance that PHP is already installed (but not activated) on my machine? I read this in an earlier post in this thread. If indeed it's on the machine already, two questions:

1. Would it just be easier to re-install based on the instructions you sent me to and if so, is there a problem to install over an older version?

2. How could I check to see if it's on the machine already and is it easy enough to activate it?

Thanks!

P.S. After posting this, I found this comment on the instructions page for PHP you gave me:

NOTE: If you have previously enabled Apple’s PHP module included in Mac OS X by un-commenting the respective lines in the httpd.conf file, you need to undo that change before you install this package, otherwise bad things will happen...

Feb 14, 2007 5:54 AM in response to Scott F.

SiriusFox....

I found some simple directions to enable my install of PHP that came with OSX. I found them on this site:

http://the.taoofmac.com/space/HOWTO/Enable%20PHP%20on%20Mac%20OS%20X

It installed PHP 4.4.4

Do you think I'll be all set with this? Or should I upgrade to a more current version? I won't be doing anything fancy.

Back to what you mentioned regarding the Firewall. I can't see how to enable Web Sharing and turn off access to other via the Firewall.

Thanks...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Viewing .php files locally

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.