PHP code is being printed as HTML comments - php

I am having the following piece of code in a php file:
<?php
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
?>
<html>....</html>
Instead of getting the expected result, I get the message "fetch_all(); ?>, which means that the above code is treated as HTML comment. I have read the similar threads(that blame the short_open_tag value mainly) around but didn't help me at all. Any ideas?
Edit: Many thanks for your responses. Seems that the specific weird problem was about an update on my Linux system which messed up a little my permissions. I changed the user to http instead of root and the problem is gone.

The problem is somewhere inside either includes/connection.php or includes/article.php. A line in there seems to indicate the start of the comment which goes all the way down to the -> part.

In case you did not installed php. you might look at WAMP server. It includes php en mysql and works great on your local (windows) computer.
http://www.wampserver.com/en/

Is this your output?
Your code isn't being parsed by PHP and there might be several reasons for it:
Check your URL. Does it look like you're fetching the file directly (C:\Users\Name\...) or are you going through your host http://localhost/file.php.
Check your file extensions. Make sure it's .php and not .html.
Check your server capabilities. Is PHP installed?

This specific problem was brought about by an update on my Linux system, which messed up my permissions a little. I changed the user in the Apache directory to http instead of root, and the problem is now gone.

Related

Why PHP include is working on local server and not on website

Problem Description in Brief:
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html file, but does not work when I upload it to my website. Note that I have made sure that all paths are correct, and that the script file has its own php tags, etc.
Problem Description in Detail:
Yes, I am new to PHP scripting, and yes, variants of this question have probably been asked before. The answers to a few of the questions I have read have noted the path of the php script files to be incorrect. I have checked all paths and confirmed that they are indeed correct (including those on the web hosting server). Furthermore, I have been successful in getting the script to work on my local server running Apache2 with PHP5, but have not been successful when uploading it to my website.
Essentially, I am trying to implement a hit counter script which I have acquired from a Stack Overflow post labelled Visitors counter for simple web sites like Vinaora. The code that invokes the php script looks something like this....
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include($_SERVER['DOCUMENT_ROOT'].'/php/hitcounter.php'); ?&gt
&lt/footer&gt
For the likes of me, I cannot figure out why it does not work on the web hosting server. I have tried other combinations of invoking the script like,
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include('./php/hitcounter.php'); ?&gt
&lt/footer&gt
and,
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include(dirname(__FILE__).'/php/hitcounter.php'); ?&gt
&lt/footer&gt
All combinations seem to work on my local web server, but not on the website! Also note that, I have no problem invoking other PHP scripts using other methods (even on the web hosting server), eg.
&ltform id="form-query" onsubmit="this.checkValidity();" action="./php/contact.php" method="post"&gt
Any advice/suggestions would be appreciated.
Do you get any PHP error?
First of all, you need to activate error reporting.
Put this before including your file
ini_set('display_errors',1);
error_reporting(-1);
PHP should tell you what's happening.
If you don't see anything, change the filename index.html to index.php and try again.
Maybe be you have used "\" in your include path
Wrong:
<?php include 'includes\header.php'; ?>
You should use "/" to work.
Current:
<?php include 'includes/header.php'; ?>
sometimes it might be dues to casing. check if you you uppercase in naming. on some servers Index.php is not equal to index.php
You might try pasting the "include" code directly into the calling code. Maybe it's the included code itself that's misbehaving...
You are doing completely incorrect thing in the first place.
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html
is just totally wrong
There is no such thing as embedding php file within html file (aside from mod_rewrite ...).
For PHP script to be interpreted you must have it (in 99% of cases) with php suffix. This way you allow PHP to distinguish it from regular PHP and send to php interpreter.
Put simply - create this file (a.html):
<body>
abcd<?php echo 'efgh';?>
</body>
and see the result in your browser - use .../a.html
What do you see?
abcd
and not
abcdefgh
On top you always have to have php not the other way around. Solve this or update your question if incorrect.

simple php script

New to php and taking a class for it. Bought php6 and mysql 6 bible to get started. Of course the hello world script is the first you get and it doesn't show. It just reads part of my script and I'm not sure the problem. Link to test - http://harden6615.com/
I am using a hosted server I bought for class, but I have also check it using MAMP. I figured my script is wrong, but I have copied and pasted and still no Hello World. Any suggestions?
What I copied:
<?php
print("Hello, World<BR />\n");
phpinfo();
?>
Safe the file as index.php instead of index.html.
Checklist:
host allows PHP
file has .php extension
file was edited with a programmer's text editor (not word/wordpad under windows or TextEdit under Mac) because some encode <, > symbols when saving the file
Well actually it isn't, try to use just <?php phpinfo(); ?> If this doesn't show anything - take a look at your webhost if he supports php...
There are a few reasons why PHP may not be being executed.
The most basic error is saving the file as a .html file instead of .php. Make sure you use the correct extension on your file.
Next, make sure your webhost supports PHP. Most free hosts do not, so it's always a good idea to double-check.
There is nothing wrong with your code. If the above two solutions didn't help, try contacting your webhost and asking them why PHP isn't working for you.

local php files will not work

I have to do a php project. I done php before so I understand the syntax for the most part. Just for a test, I made a file.php and in it I wrote:
<html>
<body>
<?php echo "helloWorld"; ?>
</body>
</html>
Well it won't display. The screen is blank. I tried it in chrome, firefox, IE and nothing wants to dispaly. Actually in IE, the source is displayed which is wierd. I also tried it without all the html and just used xampp to render it. It will not work. If I right click tho in the browser and view source, the code is there. Any ideas on what's going on?
Well, it comes from your web server configuration. If you're using Apache, have you enabled the mod-php module?
If you're new to setting up your own server, i would recommend using XAMPP (or WAMP), these are preconfigured PHP, Apache and MySQL servers.
If you're sure you have setup your server correctly check the following:
Make sure your executing your files from the server directory and NOT from a local directory. (your URL should look something like "http://localhost/test.php")
Note: You will need to phisically store the files in a place the apache server will look for, an example from XAMPP (on Windows, as thats what im assuming your using) is: "C:\xampp\htdocs"
Make sure your file ends in .php or something else that the Apache server will pickup as a PHP file. (.php3, .php4, etc)(make sure you didn't accidentally leave a .txt or something like that at the very end)
Check mod-php module is enabled (as Julien mentioned)
Hope that helps!
Edit:
Try
<?php
phpinfo();
?>
That as well, it should give you the php configuration information if the server is setup correctly.
EDIT2:
I see that you are using XAMPP, double check that the following file exists at the very least:
"C:\xampp\apache\conf\extra\httpd-xampp.conf", it loads the PHP module

PHP scripts are not executing on some of the pages

One of my php website is not working fully. Its showing me only home page and when i try to open up any present link it shows me the php code of the file that is called in for that link.
PHP is installed properly. Same server is hosting more than 10 other websites of PHP. No .htaccess parameters etc.. Same site is working on some other server with same settings and same code... I am unable to find the reason..
Make sure you are using
<?php .. ?>
tags everywhere, you might know this, but I am pointing it out just in case.
In earlier versions, apache would pick up and compile anything with
<? ... ?>
tags and it was a mistake since it was getting confused with the xml file versions.
so they changed it to complie only stuff in
<?php ... ?>
Also, again, you might know this, make sure you are hosting it on a php supported server.
There is also this "feature" that you can configure in your .htaccess to ignore certain files. make sure your .htaccess is not having exceptions for php compilation.
I cant think of anything else right now :)
Make sure everything of your PHP-Code is between the php tags like
iamserious has written upstairs.
Make sure you're filename's extension is .php index.php for example.
Make sure it's in the path to the file is ok!
Make sure you're webserver is working perfectly with PHP!

Just one PHP page won't run - it wants to download

All PHP pages are all running just fine on my site, except for one for which Firefox says "You have chosen to open checkpage.php which is a PHP script" and then wants me to select an application with which to open it.
The site is running PHP Version 5.2.10 on Centos 5.5.
I'm using exactly the same code on another site (PHP Version 5.2.10-2ubuntu6.4 on Ubuntu 9.10) and it's fine.
I've Googled myself silly trying to work out what the issue is!
Does anyone have any ideas why this one page might be causing a problem? The page is about 200 lines long but I'll post it here if it'll help...
All thoughts much appreciated
Mike
PasteBin: http://pastebin.com/A6uNj9CN
Last time I had this problem, I just installed PHP on my server. Worked fine after I rebooted it. Hope that helps.
Does the file that's downloaded have any content? Or is it zero bytes? It's possible something's causing PHP to puke on that script before it sends out any headers and so the browser has no choice but to interpret it as a download of a .php file with no content.
Check the server's error logs (and PHP's as well) to see if anything's showing up in there.
Is there a separate htaccess in the folder that stops apache from sending it to php or changes the mime-type? Does the file have a different extension than other files on your site?
Use something like a packet sniffer, or HTTP Debugger, to see what headers are actually sent by the webserver. That will help you, and us, debug the issue.
Call curl -I http://blahblah/foo.php and look at the content-type header. That will help point you in the right direction. Is it text/html or something else?
Try to change the permission of the file from 775 to 644 or try changing the permissions. Note: change to the correct permission type to avoid public viewing of the files

Categories