/    Sign up×
Community /Pin to ProfileBookmark

Reading a .PHP file with a browser?

I’d first like to say that I’m a complete noob at anything web-related beyond basic HTML. Due to a project I’m working on I’ve been force to learn about web development, but some of the things I’ve been reading through have made me want to curl up into a fetal position and crawl back to managed desktop applications ?

The issue I’m facing now is I need to open a .PHP file inside a web browser (Chrome). I initially assumed that I could open it just like I can an HTML file and the browser would parse it for me, but apparently that isn’t the case. Through a convoluted path I ended up installing XAMPP (as I read that apache needs to be installed in order to use php files). Then I called the file in the web browser using localhost (?) and I am now stuck at a 403 Forbidden error.

Some other forums suggested changing parts of the apache/conf/extra/httpd-xampp.conf file, which I did, but to no avail.

Am I even on the right track with XAMPP? Is there a way to open a PHP file without all this mess (I always thought you could name the file with a .html extension, and the html would just use the php script)? Sorry for all the questions, but I really don’t have a good understanding of these things.

I’d really, really appreciate any and all help.

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@nathanwallApr 17.2012 — You cannot open a PHP file in a web browser. PHP is a server-side language intended to let the server process files before sending them to the browser. If you want a programming language which the browser can read, you should use JavaScript.

You are on the right track with XAMPP. If you want to open a PHP file you must have a server to process the file -- XAMPP is a good setup to use on your local computer.

It's been quite a long time since I used XAMPP myself, but changing some settings in an httpd configuration file sound reasonable to me for fixing your problem. Also make sure it's set up to point to a real folder and that you've got an index.html or index.php file in your folder. I might also check the folder permissions in Windows. Unfortunately, I can't advise you more specifically -- perhaps someone else will come along who can. A server can be a real pain to get working for your first time, but keep working at it; you'll figure it out eventually.

As a last resort, if you can't find a way to get it working in XAMPP, you might try IIS if you have Windows Professional or Home Premium. It can be just as hard to get working for the first time as XAMPP, but it's another option.
Copy linkTweet thisAlerts:
@ScottyBoyApr 17.2012 — PHP is a server side language, which means that the browser doesn't know what to do with it. And that's a good thing, because most PHP applications interact with databases and sensitive data that you wouldn't want people to access directly. That is why PHP can only be executed on a web server (XAMPP is a local web server). Also, the script won't be executed if you're using the .html extension; you have to use .php.

As for the 403 error. I'm not sure what the solution is to that, as I haven't had that issue with XAMPP before. Although I know on my Mac, the file permissions must often be changed before they can be executed properly. I'm not sure if that applies here or not.
Copy linkTweet thisAlerts:
@PeteStreetauthorApr 17.2012 — Thank you for the responses! So to be clear, if I want to run any sort of PHP script from a webpage, I need PHP (and a web server) installed? Would I need this setup if I tried to use ajax instead?

I also came across php.js (http://phpjs.org/pages/home). Is that worth looking into if I want the functionality of PHP without using a web server?

Thanks again!
Copy linkTweet thisAlerts:
@tobias_wagnerApr 17.2012 — AJAX and JavaScript are client-side scripting so you can test it directly on your PC without any web server. If you need to run PHP on your local PC, I would recommend WAMP, it's more straight forward for beginners and no additional configs are required to run a PHP file. Here you may check for some instructions http://webdesy.com/how-to-setup-a-local-server-using-wamp/

As for php.js it's not a full replacement to PHP as you can read in the description and without some good knowledge of JS you probably won't be able to use it.

P.S. Learn PHP, you won't regret it )
Copy linkTweet thisAlerts:
@ScottyBoyApr 17.2012 — As previously stated, php.js isn't a language replacement. While it probably offers some similar functionality, you still won't be able to directly work with databases or files, which is the bread and butter of PHP.

And yes, in order to run any kind of server side script {PHP, ASP, Perl, etc.} you need a server. I personally use the XAMPP local server, but WAMP is a good choice, too.

Best of luck,

Scotty
Copy linkTweet thisAlerts:
@PeteStreetauthorApr 18.2012 — Alrighty, I installed WAMP and everything went smoothly, but when I first tried to open a php file all it showed me was the source code. Then it proceeded to give me 403 errors on every php file I tried. I will try editing the httpd.conf file again, but my hopes aren't very high :/

I read through an article on building an ajax website (which is what I think I want to do), but I'm pretty sure I would still need to use some sort of database if it needed to be accessible from multiple computers. Is there anything else I could try?

Thanks!
Copy linkTweet thisAlerts:
@tobias_wagnerApr 18.2012 — That';s quite strange that you're having those issues. Are you sure you're doing everything correctly?

If you installed WAMP to default location C:wamp , then you should place the php file (for example test.php) into C:wampwww

and to run the file you should type in your browser localhost/test.php

Oh, and don't forget to start the WampServer. Start -> All Programs -> WampServer -> Start WampServer You should get a green [B]W[/B] icon in the tray

And try restarting your PC after installing WAMP (if you haven't done that)
Copy linkTweet thisAlerts:
@nathanwallApr 18.2012 — Did you uninstall XAMPP before installing WAMP?
Copy linkTweet thisAlerts:
@spufiApr 18.2012 — AJAX and JavaScript are client-side scripting so you can test it directly on your PC without any web server.[/QUOTE]

Ajax interacts with the server.
Copy linkTweet thisAlerts:
@ScottyBoyApr 18.2012 — Alrighty, I installed WAMP and everything went

I read through an article on building an ajax website (which is what I think I want to do), but I'm pretty sure I would still need to use some sort of database if it needed to be accessible from multiple computers. Is there anything else I could try?[/QUOTE]


You [i]could[/i] try saving data in an XML or JSON file. That's only recommended for certain cases though, when the nature of the data being saved isn't private. XML and JSON can both be read from the browser, so don't store things like passwords in that format.
Copy linkTweet thisAlerts:
@PeteStreetauthorApr 18.2012 — That';s quite strange that you're having those issues. Are you sure you're doing everything correctly?

If you installed WAMP to default location C:wamp , then you should place the php file (for example test.php) into C:wampwww

and to run the file you should type in your browser localhost/test.php

Oh, and don't forget to start the WampServer. Start -> All Programs -> WampServer -> Start WampServer You should get a green [B]W[/B] icon in the tray[/QUOTE]


I did all that at the start, but I did not get a green "W" in the tray. So I will follow those steps once I get home and see if it works.

Thank you!
Copy linkTweet thisAlerts:
@ScottyBoyApr 18.2012 — I had a similar problem with XAMPP on Windows XP, where Apache refused to start. After talking to someone on the XAMPP forums, they found that the computer didn't have VC9 runtime files, and so Apache would never compile. You can read through the thread, but I don't know how much of it applies to WAMP.

http://www.apachefriends.org/f/viewtopic.php?f=16&t=48483&hilit=scotty
Copy linkTweet thisAlerts:
@DasherApr 19.2012 — You have open the files using the local url

i.e. //localhost/test.php not c:wampwwwtest.php
×

Success!

Help @PeteStreet spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...