Browser ask me to download php file - php

I am a new guy in web programming who still learning new stuff.
I am creating a web form that consist of one INPUT. I connected the form in the HTML file to the php file. I am using a localhost MAMP that I recently installed on my MacBook.
The Problem : When I open the HTML file on a webpage and type something on the input box and hit submit, the browser ask me to download the php file rather processing it.
I don’t think the problem is related to mysql or apache since when I run the MAMP it give me green signs next to the apache and mysql.
Do anyone know how I can fix this problem and the let the php file open and transfer inputs to the database?

You'll want to make sure that Apache has been told that .php files should be treated as PHP scripts. That means one of the following:
LoadModule php5_module modules/libphp5.so # on windows, this'd be a .dll instead
AddHandler php5-script php
and/or
AddType application/x-httpd-php php
in your httpd.conf file.

open your file from virtual web-server, not filesystem.
you have to type something like
http://localhost/form.html
in your browser's address bar

Did you checked out http://www.mamp.info/en/documentation/faq.html#q8 ?
Maybe you didn't put your site into the folder "htdocs"?
PHP won't be parsed unless it is located in that folder or a subfolder.
That would explain the fact that your browser downloads the php file instead of getting the parsed php from apache.

This error is not only about php but general. The file that the browser suggets to down load is simply the script name, php or not.

Related

Running php file asks for download instead of executing in apache 2.5?

I have a windows7 64bit, I tried to run php and my IE browser was asking me for to download the file instead of executing it. Someone told me to download apache, so i downoaded wamp2.5-32bit version, installed it well, and also chose Firefox as default for wamp. But still when I double click php files in www directory of wamp, firefox asks me to download it instead of running it.
I have tried adding these lines in httpd.conf but no change;
LoadModule php5_module "c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
#PHPIniDir c:/wamp/bin/php/php5.5.12
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
AddType text/html .php
LoadModule php5_module modules/mod_php55.so
This wamp option also seems like a tag of war, I am going around editting notepad files and its now lots of hours gone. Is there another option?
Matt,
If you look through httpd.conf you will find all of those lines you say you have added already exist in there proper place already.
Assuming you have also done some other changes you have forgotten about or not mentioned I would suggest you Uninstall WAMPServer (using the standard windows uninstall mechanism )
Then manually delete c:\wamp as any files you have edited will not be removed by the uninstall.
Now reinstall WAMPServer.
Once you have a fresh install and WAMPServer's wampmanager icon is green enter this in your browser.
http://localhost
If you see the WAMPServer homepage then PHP is working, as this page is written in PHP.
Now you say you are double clicking a filename in the \wamp\www folder, by this I assume you mean you are using Explorer and double clicking a filename! DONT DO THAT That will just launch this file in the browser but will not run the file through Apache. That method is ok if you just have a HTML & Javascript page, but it will not compile and run a PHP script.
To get a PHP script to run you must run the page through Apache so it can spot the <?php tag. Apache then knows to pass the script through PHP so it can be interpreted and executed.
So to make this happen you enter the url into the browsers address bar like you would to launch the StackOverflow page like so :-
localhost/project1/text.php
Did you check the PHP-server is running? Apache can run, but PHP is another service.
Right click on the WAMP icon in you taskbar en check that. Good luck!

Apache: Inline PHP not working on Linux

I am having trouble getting my PHP to work for my Apache server. I am running Oracle Linux, and used yum install php + yum install httpd to get my PHP and Apache. I have scoured the internet and done a couple of things:
Firstly, I have gone into my httpd.conf file and added LoadModule php5_module modules/libphp5.so. I have chosen to use the one in my php.conf file, which is the exact same one. Furthermore, I have done AddType application/x-http-php .php, so now my .php files are loading fine.
Secondly, I have tried to run my Apache server, and it works fine. It displays my index.html file, which I set with DirectoryIndex. However, when I tried to put php code into it, it got automatically commented out, which I assume is because I haven't set it to properly execute on the server side.
Now, with all that said, I am wondering what else I may be missing. I have two set-ups, one where I have a .php file, and one where the php is inline with the html.
index.html
<!DOCTYPE html>
<body>
<?php echo "Hello everybody."; ?>
</body>
</html>
index.php
<?php phpinfo(); ?>
The index.php file loads fine, but the index.html doesn't run the php code. How do I get the inlined version of php to work?
Note: I have set-up my Mac OS to work fine with .php files, but it is also having trouble with inline php within an Html file. What am I missing?
SOLUTION: Html files themselves cannot include php. Instead, the file must be a .php extension, and within a .php file, you can have text, html, and JavaScript.
I do not think .html files are run through php at all so the <? ... ?> is treated as a normal tag which is invisible.
Change the extension to .php.
Php can contain html.
Edit:
There exists an option to configure for example Apache to parse html files as if they are php if you for some reason cannot or do not want to use php endings.
This, in my opinion, is not a good solution as it hides the fact that the page is dynamic to a future maintainer.
There is a similar question which has a similar problem:
PHP code is not being executed, instead code shows on the page
You might want to check out points 2 to 5 in the accepted answer:
LoadModule (It seems, that you have configured that properly)
Set Apache to run PHP files (this is the third point, and it seems that
you didn't configure that). Add the following line to your httpd.conf file: AddType application/x-httpd-php .php
Make sure, that you have the file ending with the .php extension (after you have configured Apache to run PHP files, see the previous point)
Change your code to use the long PHP opening tag (<?php instead of just <?)
You need to set short_open_tag = On in your php.ini file

Which file to adjust to allow .php files to display (not download)

I'm having some trouble with .php files which seem to download when I open them in the browser.
I've googled around and have found this is because the server doesn't know how to parse the file, and that I need to add something to make this work.
I've read around and it seems I need to add something to a .htcaccess file, but I'm not sure where this is or where I should create it.
Below is a screenshot of the directories on my server if anyone can help me.
You mean something like
AddHandler x-httpd-php5 .php
...this should already be in place in the master Apache file (or the appropriate .conf include - in some distributions, configuration directives may be parceled between several configuration files) if PHP is properly installed.
you dont need to generate a htaccess file for this issue, if you installed a web server and php and configured them properly.

Apache downloads php files instead of executing script

I just set up a local web server on a new Mac running 10.6 (Apache2 / PHP / Mysql). Everything seems to be working fine except when I navigate to SOME (not most) pages Apache downloads php files instead of executing them.
I thought this might have something to do with the time it takes php to execute the script so I increased my "max_execution_time" but it didn't help.
I wish I could give more details but the error logs don't output anything so I really have no idea why this is happening. One thing that might be of significance is that only pages that don't query the database seem to work every time.
UPDATE:
Just discovered something strange that might give more insite into the problem. If I type the following URLs:
localhost/index.php // the home page is served up correctly
localhost/index.php?page=1 // the home page is served up correctly
localhost/index.php?page=home // ERROR: index.php is downloaded !!!!!!!!!!
localhost/index.php?page=contact // the contact page is served up correctly
localhost/index.php?page=blog // ERROR: index.php is downloaded
localhost/index.php?page=10 // the blog page is served up correctly
Uncommenting the following line in /etc/apache2/httpd.conf fixed the problem for me:
LoadModule php5_module libexec/apache2/libphp5.so
You could try adding this to your conf or htaccess:
AddType application/x-httpd-php .php
I know that this was asked a long time ago, but I just encountered this problem so I thought I'd post what the cause was for me.
Make sure that you don't have...
AddHandler application/x-httpd-php5 .php
...in BOTH your .htaccess file and your httpd.conf file. Check your .htaccess file, comment it out, restart Apache, and try again. That is most likely your problem since Apache renders php for some sites but not others. It has to be at the directory level, which would point to a .htaccess file setting.
I stumbled upon this problem after upgrading php to 5.5 and reinstalling apache.
Finally,this fixed it, in case someone else needs it.
apt-get install libapache2-mod-php5
(here's the answer)

PHP not executing correcting when running PHP and HTML files on my local server (Mac Snow Leopard)

I am learning how to code in PHP and MySQL and I am running into trouble with my HTML calling up PHP. The code looks like:
<form method="post" action="report.php">
When I ran the PHP and HTML on web server (hosted by GoDaddy), the code worked fine.
However, now I am running on a local server (using Apache and MySQL on my Mac Snow Leopard) and I am getting errors. When PHP gets called, a dialogue window pops up saying that "I have chosen to open report.php" and asking if I want to open it with a text editor or save it. I am using FireFox 3 and it's the same dialogue window that appears when opening up a file.
Do I need to do something in particular when I am using my local server? Do I need to save my files in a particular location?
I've spent a long time trying to fix this so any help would be greatly appreciated!
Have you opened /etc/apache2/httpd.conf and enabled the php5 module?
Should be able to uncomment this line:
LoadModule php5_module libexec/apache2/libphp5.so
This sounds like a server configuration issue. Apache doesn't know what to do with PHP files and is giving them back to the browser instead of executing them. This generally means PHP is not installed properly. Check in your httpd.conf file that the correct AddType directives have been added so that Apache knows about the PHP interpreter.
you need to open the file via the server not a directory so in your browser something like http://localhost/file.php

Categories