I am a beginner of web development and just started to learn about a month
intro:
I recently come across SQL injection and decided to look it up. After summarizing it, I tried to upgrade my cpanel php version from 5.3 to 5.4 due to PDO support. So I was messing around with .htaccess file and after I uploaded my new .htaccess file I can no longer view my php page again. I don't remember what codes did I put.....
Question: What should I do to undo my stupidity?
Tried solution:
I emptied my .htaccess file and still the same result
I loaded up codes in .htaccess file with
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php
AddType application/x-httpd-php php
(from Browser ask me to download php file)
and still the same result
Somewhere I have read to change the php file name will undo the damage, It works. But I like my original php file name, so I tried rename back to the original file name and the problem happens again
Sorry for my bad English practice
Update:
I was browsing my website again, and somehow got into an error saying 500 - Internal Server Error, so I decided to delete all my files in public_html file. Then,I re-upload all my web file again and suddenly it all worked. Did not download php file again.
Thanks for everyone who spends time on looking at my problem. I apologize to all the contributor.
The file name on both regist and register have a extension name of .php
You're being asked to download the file because the server no longer recognizes the file as something it needs to process.
I'm not sure why you would need all the PHP declarations in your .htaccess file (especially LoadModule). Normally those are stored in config files (files like /etc/httpd/conf.d/php.conf). Here's what the default on my box looks like (this is for Apache so YMMV)
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
#<IfModule prefork.c>
<IfModule !worker.c>
LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
LoadModule php5_module modules/libphp5-zts.so
</IfModule>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
#AddHandler php5-script .php
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps
Related
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
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.
Recently I configured Apache 2.2 server to run PHP and have configured to load PHP 5 as a module. I am able to start Apache with no problems but when I try to run PHP files it pops up would you like to open or save file?
Running on Windows XP SP3
Internet Explorer browser but not related
Simply trying to open it as a file not as my index.php and haven't set Apache to look for that as my index. If it helps I think my issue might be here:
;AddType application/x-httpd-php .php
;AddType application/x-httpd-php .php5
;AddType application/x-httpd-php .phtml
I tried to modify in my Apache conf file addtype and addhandler but no change and apache still starts both ways.
I tried putting these inside the mime configuration part and outside of it same both ways but not sure where in doc that should go my loadmodule part is also entered.
Im not sure but the only other place i could see being an issue would be in my php.ini file to which i have made no modifications.
Can someone point me in the right direction? What I am missing here?
Edit httpd.conf and add (or remove the ; at the begin of line):
# PHP 5.x module
LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php
# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php
AddType text/html .php
# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):
AddType application/x-httpd-php-source phps
I don't know, but I think you need to restart the Apache.
From http://dan.drydog.com/apache2php.html (step 11)
I wanted to change the .php extension to .foo, to hide it for visitors.
My webserver has cPanel installed, so I logged in and clicked on "MIME Types", and entered the following:
MIME Type: application/x-httpd-php
Extension: foo
The problem is that it's not being parsed as PHP, but instead is downloaded (when you click the link, a file containing all the code of that file is being downloaded)
How would I solve this?
Add in httpd.conf or vhost.conf
<IfModule mime_module>
AddType application/x-httpd-php .foo
</IfModule>
But best way to solve you problem is in using mod_rewrite
If you have the necessary AllowOverride permissions to use .htaccess, try
AddHandler application/x-httpd-php5 .php .foo
For some reason our CentOS server needs x-httpd-php5, where as our WAMP internal server just uses x-httpd-php like you have (and -php5 will not work)
I have a CPanel based website and we used the above rule to set .html to be parsed as php, didn't even bother with the CPanel Mime settings... but there are other ways to determine if a server is running PHP so you're not really hiding that you use PHP from anyone who knows what they are doing
I have an apache web server. Let's say this server's domain is example.com.
When I access example.com, then the index.php file is correctly displayed in the browser.
However, when I access e.g. example.com/~user, then the index.php file of /home/user/public_html/index.php file is downloaded rather than displayed.
How do I fix this problem? I changed "expose_php = Off" in php.ini, but nothing has changed.
If you are on Debian/Ubuntu take a look at this file /etc/apache2/mods-available/php5.conf
mine looks like this and you can see I had to comment some line to get PHP working in the user dir
<IfModule mod_php5.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
# To re-enable php in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_value engine Off
# </Directory>
#</IfModule>
</IfModule>
Please note that after editing the file you would have to restart apache for the modifications to take effect, the command to restart apache on a debian based system is: /etc/init.d/apache2 restart
Hope this saves someone else the headache. I know this question is old, but it still comes up when searching for this problem.
I'm not sure if this works across all installations of apache2, but I am running apache2 on ubuntu and had the problem of my web browser downloading files instead of displaying the correct index file.
The problem lies in the file /etc/apache2/mods-enabled/dir.conf
The default document setting here was overriding what I had set in /etc/apache2/httpd.conf
So just open up /etc/apache2/mods-enabled/dir.conf and change the order of the files listed.
:)
I've had a similar experience - some php files working OK, but others seem to have the raw php code downloaded.
In my case, it was due to the broken files using the short tag format of <? and ?>. This is not recommended, and you may find the default php.ini has this support forced off. With support off, the php code is sent down to the browser as if it was HTML.
If you can't avoid short tags (as in my case - a whole legacy codebase using short tags), then you can set it to be allowed in php.ini:
short_open_tag = On