Apache treating PHP as comments? - php

I've just installed Apache and PHP. Apache appears to run php correctly, but it treats my php as comments when I delimit it with simply <?; it appears only to run code delimited with <?php
This is a problem for me because I am creating a development environment for a pretty sizeable website which I did not build. (Don't want to have to go through hundreds of files and change a lot of tags.)
What can I do to get it to treat all <? tags as php start tags?
Details:
Running Apache 2.2.19, PHP 5.2.17, Windows XP
To get Apache to run PHP, I added the following lines to the config file:
LoadModule php5_module "C:/Program Files/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "C:/Program Files/php"

Check your php.ini. Make sure short_open_tag is set properly (i.e., you want it to be set to On).

This is a php configuration question. You just need to set
short_open_tag = On

You would need to turn on short_open_tag in the php.ini file. You can read more about this file here.

It's quite simple really. All you have to do is install the HTML in the root of your Apache PHP JavaQuery XRSS folder and then run the script that gets generated. After the script is run type in "22145" and it will run a PHP script that changes the way PHP is interpreted by the Internet and make it so you don't have to use the <?php tag on your Silverlight Python C++ encoding applet.

Related

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)

PHP only coming out with code

I'm honestly not sure exactly what is going on here. I'm teaching myself php and I think I've finally gotten the server working, but now I have more questions. When I plug in the code, I should get a single output. In this case, it should just come out with "hello world." Instead I literally get this:
<?php
echo "hello world";
?>
I'm pretty sure that's not supposed to happen. If I put in the HTML code, it will do exactly the same thing, but with the html code as well. Someone in a chat room mentioned I might have a php configuration problem, but after following another set of instructions online it appears I have nothing wrong.
I'm using OSX 10.7 with the onboard apache server, and I'm testing in Google Chrome. Also, I have to have very specific instructions on how to do something in Terminal since I also have very little formal computer training and therefore have no idea what I'm doing.
Edited: Turns out my files weren't allowing me to access them. A friend showed me how to alter the files so I could view them properly. It was an obscure problem and required a somewhat obscure solution.
Try this page. By default Apache in Lion does not have PHP enabled so you'll need to change the configuration so you can use it.
Looks like, PHP is not enabled!
Open terminal and type!
sudo nano /etc/apache2/httpd.conf
Remove '#' before (You can use search)
LoadModule php5_module libexec/apache2/libphp5.so
Save the conf file and you should have it!
In my Apache for windows I have
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
PHPIniDir "C:/Program Files/PHP"
AddType text/html .php .phps
AddHandler application/x-httpd-php .php
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
On any other system it should look alike except paths to files.
Take a look what Nab Raj wrote. After completing it might be needed to restart server. These lines are to say to Apache that it is going to give PHP files that have extensions of .php

php files are downloaded instead of ran

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

Why are my php tags converted to html comments?

A friend's lamp host seems to be misconfigured. I try to execute php, but it doesn't seem to be working.
In Chrome's inspect element:
<?php echo 'test'; ?>
becomes :
<!--?php echo 'test'; ?-->
Furthermore, its been triggering a file download, rather than opening it as a webpage.
I've tried various code in an .htaccess file, but it doesn't seem to have any effect:
AddType x-mapp-php5 .php
AddType application/x-httpd-php .php
AddHandler x-mapp-php5 .php
The place to correctly configure PHP operation is the httpd.conf file, which resides in the conf subdirectory of your Apache installation directory.
In there, you'll want to look for the module loading section, which will be a bunch of lines that start with LoadModule. Somewhere in there, you should have the following (or something very similar):
LoadModule php5_module "location\of\your\php\installation"
AddType application/x-httpd-php .php
PHPIniDir "location\of\your\php\configuration\file"
I'm not all too familiar with Linux, but in Windows (WAMP) installations, those would be something along the lines of:
LoadModule php5_module "c:/program files/php/php5apache2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/program files/php"
And the httpd.conf file, on my machine, is at C:\Program Files\Apache Group\Apache2\conf\httpd.conf.
It could also be that PHP is simply not installed at all on your machine, in which case, you will have to download it and install it. Brad's already posted the relevant link in one of his comments, (+1, by the way, Brad), but for the sake of having everything in one spot:
PHP: Installation and Configuration - Manual
Your Chrome is lying to you.
Your PHP source file is <?php echo 'test'; ?>. Because PHP is not executed, this file is sent to the browser. If the browser should interpret this text, it will stumble upon the <? ?> marks. They have a meaning - they are "XML processing instructions", and the text after the opening angle defines the target.
Obviously the browser does not know about a target named "PHP", so this text is ignored.
And then the element inspector tries to display the DOM and is lying about the original source code, because he is working on the PARSED source - which is great because you usually want to know on which data the browser acts, and this includes how the browser interpreted your source.
But if you make any error, the browser will try to fix it, and the fix is included in the element inspector.
Obviously the fix for an unknown XML processing instruction is to disable it by commenting it out.
This just happened to me. Turned out I had forgotten to change the filetype from .html to .php
Sounds to me that your PHP is not correctly configured or installed in your lamp configuration. What distribution are you using? It might be as simple as running a command to re-install PHP, otherwise you will likely need to compile apache with php support.
This answer doesn't apply to the OP's specific variant of this problem, but I had the basic same issue – <? var_dump($test); ?> being converted to <!--? var_dump($test); ?--> – which I could solve by enabling short_open_tag in php.ini.
I was faced with exact same problem when I accidently tried to test local php file in browser on server through file:// protocol, not through installed site.
So the answer is one: "Mr. PHP has left the building". We need to check the configuration, location of a file or access.
And browser is just trying to fix a web page and help us.
If you are placing your code outside the standard directories (development scenario, in my case) you should check in your /etc/apache2/mod-enabled or /etc/apache2/mod-available in the php5.conf (for ubuntu) and comment the lines that the comment indicates:
# 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>
Sounds like you are using an editor that is changing what you enter. Make sure that what you want in the file is what is actually in the file. Using FTP to upload a php file should ensure this.
I just solved this same problem.
You need to open your file from your WAMP, and not from your hard drive directrory.
In your browser, put: localhost/...../yourfile.php
Otherwise, your browser will replace all <?php ?>
with <!-- ?php ?-->
It seems you have to instruct apache explicitly to handle html files as php files, I was having the same problem but renaming the file to .php solved the issue for me.
On an Ubuntu system, installing the apache php5 plugin worked for me:
sudo apt-get install libapache2-mod-php5
sudo service apache2 restart
I have same problem sometimes, probably the extension of your file is "html".
Change It to "php" and it'll OK.

Change scripts extension to .php5 for php 5 execution?

Is there any drawback to this setup?
I want to be able to run PHP5 filters and functions. Will renaming a file to something.php5 allow me to do this?
My hosting provider suggested this instead of upgrading to PHP5.
The default is still php4 with registered_globals ON.
Changing the file extensions won't change the version of PHP that's installed on the server. If your host only has PHP4 then you can only use PHP4 unless they're willing to give you PHP5. The only way this will work is if your host already has PHP5 running and has it setup to only work with files that have the .php5 extension (this is an entirely possible scenario).
I'd say the first thing to do is to create a simple phpinfo file, like below, and name it with the .php5 extension and see what it says.
<?php
phpinfo();
?>
phpinfo() will give you the PHP version in big bold text at the top of the file. So if a file with .php says it's PHP 4.x and a .php5 file says its PHP 5.x, then your host has both installed on the server and is telling Apache to use PHP5 with .php5 files and PHP4 with .php files.
And on a side note if your host has register_globals turned on and they won't turn them off, I'd recommend putting the following in a .htaccess file at your document root.
php_flag register_globals off
Are you allowed to rewrite those settings per directory with a .htaccess file?
If you can, just ask you hosting provider how the setting is made and change it just for you. Something like
Action application/x-httpd-php /path/to/php5

Categories