Problem viewing local PHP files on Mac OS X

Discussion in 'Web Design and Development' started by mrbungle, Oct 10, 2011.

  1. mrbungle macrumors newbie

    Joined:
    Oct 10, 2011
    #1
    I used to be able to view local php files (containing mostly html) in WinXP without a php server installed - it worked in all browser (IE, Firefox, etc.).

    Now on my Macbook Air (2011), I keep getting a pop-up prompt in Firefox - and in Safari it displays the source code instead of rendering the page.

    How can I preview PHP pages/files on a Mac without a php server installed?
     
  2. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #2
    You can't. PHP is a server-side language so it must be executed by a server before the output is sent to a browser. A PHP server of some type is required.

    I'm not sure what you think you were doing on your Windows machine, but you weren't rendering php files in the browser without a server. PHP just doesn't work that way.

    Perhaps you had WAMP installed? You can install MAMP on your Mac.
     
  3. mrbungle thread starter macrumors newbie

    Joined:
    Oct 10, 2011
    #3
    I understand what you mean, but ask any windows user and they'll tell you that you don't need a php server installed to preview html pages (named .php) which contain php code.
     
  4. Hansr, Oct 10, 2011
    Last edited: Oct 10, 2011

    Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #4
    It's a browser based setting and that's the way it should be, rename it to .html if it doesn't contain php and then it will display correctly.

    Also Installing MAMP takes all of 2 min :)

    Also you could force the MIME Type to be txt/html for PHP but that seems illogical, should work though.
     
  5. mrbungle thread starter macrumors newbie

    Joined:
    Oct 10, 2011
    #5
    Yes, renaming the file to .html does solve the problem - but it's a inconvenient solution for previewing a whole site (interlinked with .php navigation links).
     
  6. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #6
    I am a Windows user, and I can tell you that what you describe doesn't work. Even if I right-click a php file and choose "open with->Internet Explorer", it gives me a download dialog. So you had something set up in a non-standard way on your Windows machine.

    I'll say it again... PHP is a a server-side language. It requires a webserver to parse the code and send the results to your browser. A browser alone cannot do this. This is true for Windows, Mac, Linux, whatever.
     
  7. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #7
    Setting the .php mime type for the browser to mirror html will work too as I mentioned.
     
  8. z3r0inc macrumors regular

    Joined:
    Oct 16, 2009
    #8
    Just install MAMP or XAMPP and be done with it. You'll need it anyway if you want to continue working on those php files right?
     
  9. frocco macrumors 6502

    frocco

    Joined:
    Jan 27, 2009
    #9
    Even if you do this, you will not be able to debug or see if the php code is producing correct results.
     
  10. mkelly macrumors regular

    Joined:
    Nov 29, 2007
    #10
    OK, hopefully this will help break the "no you can't / yes you can" thread that's going to start any minute.

    What you're got are a bunch of files that end in .php - these files contain a mix of both HTML and PHP code.

    Under Windows XP, you were able to open those files up in a browser. The browser *ignored* the PHP embedded in the files and just displayed the HTML.

    The people saying "You can't do that!" mean that a browser won't interpret PHP code - you need a PHP-enabled webserver to do that (hence the various calls for you to install MAMP, etc). They're correct in saying you need the server if you want to view the output of your files/code accurately, because otherwise the PHP won't run.

    But, if you're just interested in what the embedded html looks like, then yes you are correct - a browser can view the file and ignore the code surrounded by <?php ?> tags.

    Now, the reason it isn't working for you is that the browser doesn't recognize your PHP files as containing valid text/html (rightly so - the MIME type is incorrect)... so Safari just displays the source.

    As others have suggested, probably the easiest thing to do is set the MIME type for .php files to text/html. If you're using FireFox, there are a number of plugins that allow you to add/edit MIME types.

    You may also want to look into text editors with live HTML/PHP preview capabilities. Panic's Coda editor for example (http://www.panic.com) will allow you to open and edit your PHP files with a live preview - and can even run the PHP in your files via a local copy of the PHP interpreter. Code isn't free, but it's a nice package. There are several other editors w/live preview modes out there as well - but you'll have to do some digging, their names escape me at the moment.
     
  11. 7031 macrumors 6502

    7031

    Joined:
    Apr 6, 2007
    Location:
    England
    #11
    I'm a huge fan of Coda - probably the best web development software there is (I'd honestly rather use it than Dreamweaver).

    If you do plan on just using whatever other software you're using such as Dreamweaver, and want to preview the pages within your browser, I'd recommend either installing PHP/MySQL manually, or installing something such as MAMP, which gives you an Apache, PHP and MySQL server to play with locally.
     
  12. frocco macrumors 6502

    frocco

    Joined:
    Jan 27, 2009

Share This Page