Page 2 of 3

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:11 am
by sirab33
robnet wrote:What happens if you try http://127.0.0.1/phpinfo.php ?
ah well THAT WORKS.

http://127.0.0.1/~username/phpinfo.php does NOT work, but
http://127.0.0.1/phpinfo.php DOES.

And what does happen is that I get this big long page full of tables with data and stats about PHP Version 5.2.6, Apache Environment, etc. etc.

I'm assuming this is a GOOD thing, yes? 8)

Otherwise, any file with a .php extension keeps opening up as the code itself - what's weirder is that when I go to View>Source (in Safari) - its greyed out - not even available.

In FIREFOX, its even worse: when I open a .PHP file Firefox gives me this:
Image

Its like it won't even consider opening it in the browser.

this HAS to give us some clues as to what's going on, shouldn't it?

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:13 am
by Eran
Is it possible you are running PHP in safe mode?
what does ~username go, is it a real directory or a symbolic link?

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:27 am
by sirab33
pytrin wrote:Is it possible you are running PHP in safe mode?
How do I know if I'm running PHP in safe mode? (what does that even mean?) And how/where do I change it from SAFE to something else? Please be really specific.

what does ~username go, is it a real directory or a symbolic link?
Its not a real directory - as far as I know. When I use it I get this:
Not Found

The requested URL /~xxoxoxox/phpinfo.php was not found on this server.

Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2 Server at 127.0.0.1 Port 80
I'm actually not too clear on this username business - would like to make sure I am using the right USERNAME - is that the same username that I use to log-on to my computer, or is there some other username defined someplace else that I should be using?
Where can I find out what my username IS?

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:31 am
by sirab33
pytrin wrote:Is it possible you are running PHP in safe mode? what does ~username go, is it a real directory or a symbolic link?
Did you see my previous post pytrin? the one with the Firefox error?
Check it out...

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:32 am
by Eran
you must be confusing something here. If you expected ~username to go somewhere without you specifying it, it won't happen. Apache is a separate service and does not use your OS users/groups settings for directory layout.

Currently it is looking for the subdirectory /~username under the document root (which is specified in the apache configuration file) and when it finds nothing it gives you a URL not found error. Why do you need this subdirectory for anyway? isn't this your local server?

This is safe mode - http://www.php.net/manual/en/ini.sect.safe-mode.php. It can prevent scripts from executing outside of certain permitted directories.
Also, you better get yourself familiarized with the manual. you'll find most of the information you seek there

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:50 am
by sirab33
pytrin wrote:you must be confusing something here. If you expected ~username to go somewhere without you specifying it, it won't happen. Apache is a separate service and does not use your OS users/groups settings.
well here's the deal: I'm only using this username business because other posts have advised me to try this:
http://127.0.0.1/~username/phpinfo.php

as a way of trying to figure out what's wrong.

When I did go to that URL, I got errors, but when I omitted the ~username portion it worked fine, and I got this HUGE page with tons of tables describing in great detail everything about my system, Apache, PHP, etc. - which I took to be a good thing of course. It shows that everything is there and running correctly.

But that's the only reason I've been even bothering with this username business.
I otherwise have no use for it - I'm just trying to get some PHP code to work and nothing I'm doing is getting it there.

Have you seen my previous post with the screen-grab of the error-message FIREFOX is giving me?
Its like 2-3 posts above - check it out.
It basically tries to immediately download the file or open it with a default app - it just won't display it in the browser.

There's gotta be a reason for this - and I doubt it has anything to do with usernames, know what I'm saying?

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 7:56 am
by Eran
Yes I saw that. It's not an error - firefox is trying to download the file since the webserver is not parsing it - treating it as a normal file. That is why I mentioned the safe mode, since it could cause that. Are you sure the path for the script that opens a download-dialog is under the document root?

If you have no use for the ~username stuff, then forget about that since it is irrelevant especially if you didn't setup anything to create that structure.

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 8:01 am
by robnet
I think there is some confusion going on here.

OS X has a webserver built in. By default going to http://127.0.0.1/~username will take you to your own page.

However, Sirab is using MAMP, not the built in one.. (Not a bad idea, there are certain useful features that aren't standard on the build in server.. And MAMP is completely self contained and easy to port :) )

As previously said, there is no reason the ~username version should work unless it's been previously configured.

If you want to setup a ~username the easiest way would probably be a symbolic link, though adding to your httpd.conf would work too. However, I think this might just be a red herring and can be ignored??
sirab33 wrote: And what does happen is that I get this big long page full of tables with data and stats about PHP Version 5.2.6, Apache Environment, etc. etc.
Yes - the output from phpinfo() as you described sounds good. It's basically just an output of all things supported by your php/apache install.

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 8:20 am
by sirab33
pytrin wrote: Are you sure the path for the script that opens a download-dialog is under the document root?.

Ok, to the best of my knowledge, the document root is the "htdocs" folder inside the MAMP folder (which is in the APPLICATIONS folder of the Mac's hard-drive.) That's how it was set-up by the MAMP installer, and that's where the MAMP's Read Me file says all the files should go.

So that's my understanding - but maybe "document root" is different under these circumstances (I have no experience with running a Server locally so it could be that the terminology in this context is different than what I'm used to.)

To answer your question then, I'm NOT sure the path is correct - but I think it is.
Its MAC-->APPLICATIONS-->MAMP-->htdocs-->filename

Does that seem right to you?
Let me know if there's something else I should be trying.

In terms of "soft mode", again, how do you control/switch that?

(By the way, it seems we're all in agreement that we should no longer pursue the username route as its not the issue here - and I'm glad to have at least that much worked out.)

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 8:27 am
by sirab33
robnet wrote: I think this might just be a red herring and can be ignored??
agreed! no more talk of username please 8)

We're using MAMP here so that's that.

So now what in the world could be causing the server to NOT correctly parse the PHP files?
Is there a list of items/possibilities I can go through and check-off as possible problems?

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 8:35 am
by robnet
Is the current problem that you're still seeing php code in your browser?

If so I suspect you are opening the php directly in the browser as in: browsing to file:///Applications/MAMP/htdocs/file.php. By doing this your browser would be opening the file directly, bypassing apache and php and therefore not processing it as you want. Assuming your file is in htdocs try browsing to http://127.0.0.1/file.php instead.

What the http://127.0.0.1 part does is tells your browser connect to the server "127.0.0.1" over the http protocol, meaning apache will process it first, rather than picking up the file directly.

127.0.0.1 is a loopback address - it always points to the local machine. - localhost will usually work instead if that helps make sense of things.

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 8:54 am
by Eran
robnet is right, make sure that you enter the address in the browser as a URL and not a local file path. Also, edit your apache configuration file and make sure that what you think is the document root is defined there as such.

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 9:24 am
by sirab33
robnet wrote: I suspect you are opening the php directly in the browser as in: browsing to file:///Applications/MAMP/htdocs/file.php....
CORRECT!!!

You were right - and thought there were still other problems, I figured them out on my own using your correct method 8)

Let me explain: I did open the file as you suggested - by typing the full path with http://127.0.0.1/filename - and it FINALLY WORKED and I get it - its gotta go through the server first - that makes sense cause PHP is all about running on the server, isn't it? :) (I'm so used to actionscript, javascript, html, etc.) - ANYWAY, that much is good.

HOWEVER...
I had problems with another file, a regular HTML file that has a form requesting the user to type in his name. Upon clicking the "submit" button, the ACTION on the FORM calls another file - a PHP file called "message.php" - that will simply spit back the name - and that file wasn't working.
I looked at the ACTION attribute in the FORM tag (this is in the HTML file) - it was

Code: Select all

<form action="message.php" method="post">
and it wasn't working - it was just displaying the CODE of the PHP file.
So...I changed the action to be

Code: Select all

<form action="http://127.0.0.1/message.php" method="post">
figuring this would force it to go through the server - it worked.

Friggin' FINALLY!! :D

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 9:36 am
by sirab33
robnet wrote: I suspect you are opening the php directly in the browser as in: browsing to file:///Applications/MAMP/htdocs/file.php....
CORRECT!!!

You were right - and though there were still other problems, I figured them out on my own using your correct method 8)

Kinda feels like this was a big DUH on my part :?
But like I said, I'm new to all this, I didn't know what needed to be done.. :oops:

Anyway, let me explain: I did open the file as you suggested - by typing the full path with http://127.0.0.1/filename - and it FINALLY WORKED and I get it - its gotta go through the server first - and that's how you force it to do that and that makes sense cause PHP is all about running on the server, isn't it? :) (I'm so used to actionscript, javascript, html, etc.) - ANYWAY, that much is good.

HOWEVER...
I had problems with another file, a regular HTML file that has a form requesting the user to type in his name. Upon clicking the "submit" button, the ACTION on the FORM calls another file - a PHP file called "message.php" - that will simply spit back the name - and that file wasn't working.
I looked at the ACTION attribute in the FORM tag (this is in the HTML file) - it was

Code: Select all

<form action="message.php" method="post">
and it wasn't working - it was just displaying the CODE of the PHP file.
So...I changed the action to be

Code: Select all

<form action="http://127.0.0.1/message.php" method="post">
figuring this would force it to go through the server - it worked.

Friggin' FINALLY!! :D

It all makes sense - and I knew it had to be something really simple cause all the install and utility/diagnostic app & files were clearly saying everything was running smoothly...

Like I said: its a big DUH - BUT, we're finally DONE with this. Its working.

Now I can finally focus on learning the actual language of PHP - which seems really straightforward thus far - instead of focusing on this back-end stuff - which I've never been interested in and always handed off to the sys-admin guys who's job it was to take care of it anyway.

So....THANK YOU GUYS!!

Really appreciate it.

I'm off an running - thanks to you :drunk:

Cheers,
-S

Re: Why am I seeing the PHP code in my Browser?

Posted: Tue Aug 11, 2009 9:38 am
by robnet
Great stuff!

Bear in mind though that if you view the initial html file via the server you shouldn't need to add the full (absolute) link. So if you visit http://127.0.0.1/file-with-form.html and within that form you just use href="message.php" it should resolve it correctly.

Ultimately it's best when dealing with any server side code that you always make the requests through the server. - Even for pure html or javascript. That way if you ever do need to link to your php you don't have to use the absolute links and it's more like a production environment which will make it easier to publish.

For example, if you want to upload your code to a webserver on the net and it still has the href="http://127.0.0.1/" part in it you will get errors because the page won't exist (except on your dev box!).