php files are downloaded instead of ran - php

My problem started when I was working with the .htaccess file to a directory trying to get php to run inside of html files. I tried ALOT of combinations of AddTypes and AddHandlers. I took them out and now my php files are being downloaded by the broswer instead of running. There are a few other questions on here that I have studied up and down, but they are not exactly my problem.
I'm fairly lost now, I've tried just about every combination of AddType and AddHandler. I've since decided to use mod_rewrite and just have all my files end in .php but I can't get them to run.
This is what I'm using to test:
<!DOCTYPE html>
<body>
<?php
echo("php working");
?>
</body>
</html>
Am I missing something? Luckily I made all my changes at a separate directory than the root so that the site still runs, but none of the php files in that folder can be viewed in broswer. What can I do? Any help is appreciated.
EDIT:
Just to make it clear, I was originally trying to be able to put php in my html files so I was messing around with handlers and addtypes. My server does have php. If I call a php script from a .html file, it runs. I just can't get a php file like the example I included above to open in brower ANYMORE, it used to open in browser. My fear is that I have messed something up by trying all the different handlers in .htaccess. I have since cleared my .htaccess file in hopes that would at least get me to square one, but it hasn't.
SECOND EDIT:
I went to the root directory and made a php file and it ran just fine so its definitely something I changed in the .htaccess for the particular directory. Is there anyway I can reset that directory?

have you made sure to install php correctly within apache?
if you are using php as a module, you need the following:
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"
or as a CGI binary
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php
# For PHP 4
Action application/x-httpd-php "/php/php.exe"
# For PHP 5
Action application/x-httpd-php "/php/php-cgi.exe"
other things to consider, are the permissions set correctly on the php binaries, is the php.ini present and correct, have you restarted apache since installing php into apache?
lastly, php doesnt run within html files, you would need to set the following as pretty much the last thing in the apache config
AddType application/x-httpd-php .html .htm

Do you see the line like this in your htaccess file?
php_flag engine off
If yes, delete it.
One more thing that may help you understand what happened
The following piece of htaccess would make Php from being executed but rather downloaded as a text file
<VirtualHost *>
ServerName sourcecode.testserver.me
DocumentRoot /var/www/example
AddType text/plain php
</VirtualHost>
One more thing
I'm guessing that this topic was addressed once here-> htaccess downloading file instead of loading

Related

Beginner / How to configure my .htaccess file so that it runs the PHP inside my HTML files?

So I'm trying to configure my .htaccess file so that it can runs the PHP code inside the HTML files.
When I go to mysite.com/thefile.php, it runs it correctly without any problem. Same if I go to mysite.com/thefile.html. But if I add <?php echo "Hello"; ?> inside the HTML file, it doesnt display anything. In fact, when I right click "View source code", it displays the php lines.
Here's what I tried with my .htaccess file:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .html .htm
I also a tried with just
AddType application/x-httpd-php .php .html .htm
But it displays a "500 Internal Server Error" that says:
The server encountered an internal error or misconfiguration and was unable to complete your request.
The most common causes of this problem are errors in the app's .htaccess file or incorrect file or directory permissions preventing the web server from reading the .htaccess file.
More information about this error is available in the app's Apache error log at:
/srv/users/SYSUSER/log/APPNAME/APPNAME_apache.error.log
I'm quite new to web development in general, and I want to mention that, except from these 2/1 lines in the .htaccess file, I have nothing else. Maybe I'm missing some lines in it? Just a guess
Thanks guys (and gals if they exist!)
With Ubuntu/Apache 2.4.x, in your /etc/apache2/mods-enabled/php7.1.conf or equivalent, you could add:
<FilesMatch ".+\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
(But you shouldn't do this at all! Use .php files for PHP code.)

Showing PHP Scripts [duplicate]

for a certain folder on my local Apache-Server (running with Ubuntu) I'd like that all *php-files will be displayed as if they were plain text-files. I need this since I only want to see the source code of these files and NOT run them.
While searching, I found that most people have the opposite problem :-) and couldn't really find a solution for me.
What would I need to include in the .htacces-file of my folder?
THANKS!
THE ANSWER:
in .htaccess-file type
php_flag engine off
#This will prevent apache from executing *.php-files
AddType text/plain php
#this wil display php-files in browser (if not, browser will want to download file!)
Thanks to Brad!
My Godaddy setup wont allow me to edit the httpd.conf files, and the php_flag command doesn't work due to how they've implemented php for me.
I was able to use this in my .htaccess file:
SetHandler default-handler
AddType text/plain php
I put this in the directory above where my FTP user is allowed to access, which forces all PHP files in that directory, as well as all sub-directories to show php as plain text.
This will work for other file types as well. All you need to do is add another line with whatever extension of file you want to be forced to display in plain text. AddType text/plain cgi for example
Look at your httpd.conf file for the AddType of .php extension, and change it fortext/plain, and php_flag engine to the offvalue just as sait by Sam Bisbee.
But prefer do these change in the httpd.conf, the .htaccess are useless if you have a dedicated server, and lowing your perfs.
But you can also just change the extensions of your PHP scripts...
Two solutions off the top of my head...
Change their file name extensions to .phps. Ex., index.phps.
Change the Content-type for them in the .htaccess file. AddType text/plain .php uses mod_mime to do this. More info at http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addtype
Turn off the the PHP module in apache? (if you won't be needing php execution, of course)

what code do i put in .htacess to use php?

Just wanted to know what code I have to put in my .htaccess file so my html pages read php that im going to put in there, (just a contact form), I did find it once but can't remember, sorry and thanks.
You have to convert your html files to php
put this line in your .htaccess if you're using apache2
AddType application/x-httpd-php .html .htm
but make sure that your .htaccess file is in the root directory of the website
If you want PHP in HTML files to be interpreted just add this line to your .htaccess file:
AddType application/x-httpd-php .html .htm
HTML Pages don't read your PHP. Instead Apache is reading your file and executes it with a PHP interpreter if some PHP is in there.
Just in case you have a debian server with apache2 you can apt-get install php5. But there is a lot more work to do, for example FCGI and so on.

Adding Handler to htaccess causes file to download, removing Add Handler directive causes internal server error

I'm stuck trying to figure out what directives my htaccess file for a directory needs. To give a little background, I was messing around with AddType and AddHandler to try to get html files to run php in them. I was adding directives like:
AddType application/x-httpd-php .php .html
and
AddType application/x-httpd-php5 .php .html
That didn't work, so I tried adding handlers as well:
AddHandler x-httpd-php .php .html
and
AddHandler application/x-httpd-php .php .html
That still didn't give me what I wanted so I tried many combinations, like:
AddType application/x-httpd-php .php .html
AddHandler php-script .php .html
I even tried combinations with SetHandler
AddType application/x-httpd-php .php .html
AddHandler php-script .php .html
SetHandler application/x-httpd-php .php html
I tried every imaginable combination of AddType and AddHandler found on the web.
!===== This is important ======!
I realized that its not good practice to have php run your html files just so you can run php in the html. I am no longer trying to do that. The only reason I'm posting this part above is to give you an idea on some things I've changed. These changes were made to the htaccess file at a directory above the root.
!==============================!
Now that I'm no longer trying to run my php in html files I cleared the AddType, AddHandler, and SetHandler directives from the htaccess file. But now I get an internal server error when trying to run php files.
So I tried putting some stuff back. If I had a handle, the browser now downloads the php instead of running it.
I should note that any php running below the directory I changed works fine. But everything in that directory or subdirectory of that directory has this same problem with php files.
I have tried deleting the htaccess file in that directory, but that does not work, I get an internal error.
If I turn Override off, I get an internal error.
Something I changed is persisting in that directory and I'm wondering how to either
a. turn it off
b. add the correct handler to that htaccess file so that php files are run again (like they are on the rest of the server)
I did have the exact same issue so I deleted all the content of .htaccess and started trying only with handlers until I figured it out. My local .htaccess copy had the following SetHandler:
AddHandler x-httpd-php5-3 .php
In my case it worked after changing it to:
AddHandler application/x-httpd-php5 .php
It seems the whole trick is only the handler, I tried this solution in 2 different servers with different configurations and in both cases just changing AddHandler worked. If this doesn't work for you try a different handler for php.
Hope it helps.
Just realized I left this up unanswered after I solved the problem. If someone has a similar problem and is reading this : Check the permissions on all associated folders. I can't remember the specifics but one of the directories involved (I believe it was the htaccess directory) had permissions that persisted after changing some things and then changing them back.

can't seem to get php working in MAMP

I'm trying to learn php and step one is getting php working in some capacity. I'm attempting to use MAMP but I'm having some trouble.
Specifically: if I create a file with the below code and save it as index.html in MAMP's "Document Root" directory, I get a blank page when pointing my browser at http://localhost:8888/index.html.
Code:
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</head>
Alternatively, if I put a bit of php into its own file (say test.php) and then point my browser at this file, it just displays the full text of the file in the browser.
Any ideas what I might be doing wrong?
I had the similar issue.
Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:
AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm
You're going to save the file as .htaccess (with the dot in the front; this is the file name).
Save it in /Applications/MAMP/htdocs. This is the same place you'll save your php and html files. This .htaccess will be an invisible file; you will not see it in Finder, tho you can if you cd to it in Terminal, or searching w/in Finder and choosing the File Visibility type under Kind.
Now try going to localhost:8888/ and you should see all of the available files there. And with this newly created .htaccess file, you can now embed php inside an html file too.
In MAMP, edit the file:
/Applications/MAMP/conf/apache/httpd.conf
and then search for '#AddHandler type-map' (exclude quotes). Below that, add,
AddHandler application/x-httpd-php .php .html
Save the file and stop and re-start MAMP. Php parsing will occur in files ending with the extensions: .php and .html.
You must save a file with PHP inside it with a .php extension. So you would need to name it index.php instead of index.html. Simple fix.
So, this just worked for me:
instead of having:
MAMP/htdocs/folder-that-contains-all-files/
put all your files directly in the htdocs folder!
so:
MAMP/htdocs/all your files including index.php etc.
Hope that helps!
Modifying /Applications/MAMP/conf/apache/httpd.conf
searching for #AddHandler type-map
and inserting AddHandler application/x-httpd-php .php .html
worked for me.

Categories