PHP: <?= not working / Apache configuration issue? - php

I've ran into this issue before but don't remember what was causing it. I have a small app that uses the shorthand notation to print variables, <?=$myvar?>, but it doesn't seem to get parsed by the webserver?
Any ideas on what might be wrong here? It's a bit weird since everything else is working ok.
Thanks!

its a short open tag with an echo command (=). since php 5.4 its no longer marked as short open tag and therefore works even with php.ini setting short_open_tags set to off beginning with that version. for earlier versions it depends on that option.
more information:
http://php.net/manual/en/ini.core.php#ini.short-open-tag
you can check that option e.g. using $sot = ini_get('short_open_tags');

It may be turned off in the PHP configuration. Did you try adding this to your script?
<?php
phpinfo();
?>
Anyway, try to avoid short tags because they're good for nothing except creating a portability issue. :)

Not only check for the short_opened_tags but also be sure that AddHandler application/x-httpd-php .php is in your http.conf file. If its not there please add it and restart your apache server.

Related

PHP code is being printed as HTML comments

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.

PHP: script works on some machines only

Weirdest PHP issue I have gotten. There is some PHP code that runs on some machines only.
Client called to fix the problem. The name of the file is portada.html
I had seen the website before and it worked fine. I know PHP code is usually run on .php files but this one used to run just fine on this .html file. Maybe some apache conf or something their former web developer did.
So now it runs fine when you use some machines but on others it doesn't run. Anyone has an idea of why something like this would happen?
Here's the code that doesn't run fine.
<script language="" type="text/javascript">
var so = new SWFObject("gallery.swf?xmlPath=galleries/gallery__something_<?
$sql_conf="select galeria_something from ct_conf";
$res_conf=mysql_query($sql_conf);
$row_conf=mysql_fetch_assoc($res_conf);
echo $row_conf["galeria_something"];
?>.xml", "something", "320", "238", 7, "");
so.addParam("allowScriptAccess", "sameDomain")
so.addParam("quality", "high");
so.addParam("scale", "noscale");
so.write("renta");
</script>
the code gets executed on Google Chrome, not on Firefox
You say the file name is portada.html. If that's correct and it's not saved as a .php file, then you would also need to make sure that the server recognizes .html files as needing to be processed by the PHP interpreter. You can do this from an .htaccess file by adding:
AddType application/x-httpd-php .html
Can't say for certain but it may have to do with you using short tags which is typically disabled by default. Try using <?php instead of just <?.
Also your queries may be case sensitive on Linux servers but not on windows servers.
To make you code compatible across many machines:
Don't use short tags <?, use full tags <?php
Don't use deprecated functions such as mysql_query, use mysqli_query etc http://www.php.net/manual/en/book.mysqli.php
Turn on full error reporting to check compatibility: ini_set('display_errors', 1); error_reporting(E_ALL);

Beginner Question PHP (ServerQuestion)

Hy!!
I just get a vserver running debian. I just installed apache and php. Now the server should support php.
i uploaded the file index.php:
<?
echo "Hallo";
?>
The problem is if i start a request to the site my browser wants to download a file.
Whats could be the problem?
THX
have you restarted apache after installing PHP?
/etc/init.d/apache2 restart
This could mean that the Mime extension of the File is not registered with the web server which means you will need to check if you have the PHP Interpreter installed as a extension/plugin in your server.
Try:
<?php
echo "Hallo";
The short_open_tags is disabled in may distributions by default. If you have to support legacy pages you can anable them in php.ini, if not then just use the above, your code will be more compatible.
Your server isn't set to parse PHP files before they are output.
Check out these pages:
http://www.phpbuilder.com/board/archive/index.php/t-7100332.html
http://www.petefreitag.com/item/516.cfm
If your page works if you changed that to the <?php tags, then vbence would be right, it could be as shanethehat says that you just need to restart apache if you've already made the change to php.ini, one of the joys of all these things working together you need to work through all the options to find out where these things fall down so you know where to start looking to fix it
(Found why it wasnt displaying, I hadnt put code round the php tags)

PHP Mailer Session Problem

We are trying to implement a mailer with an attachment upload option.
It's working great on two of the servers I've uploaded it to.
However, it does not seem to work on my localhost wamp server or even on a different server:
http://203.76.126.68/mailer/
Why am I seeing session values inside the text boxes?
Can anyone point out what seems to be the problem?
Any help would be appreciated.
Thanks!
Short Open Tags might be turned off. The easiest way to fix this is to just do a find and replace and change <?= into <?php echo.
http://php.net/manual/en/ini.core.php#ini.short-open-tag
You can also turn it on (might be possible in .htaccess) in php.ini.
Check if that script is actually processed by PHP.
If it is process by the PHP CGI binary, make sure short_open_tag is set to On. Those <?= ... ?> thingies only work with short_open_tag enabled.
you have shorttags off that's why you have to set it on in php ini or i think you can use the wamp menu and then setting and then turn on shorttags

Including different types of files in PHP

I took over a PHP project and I'm trying to get it running on my dev box. I'm a developer and not a sysadmin, so I'm having trouble getting it working.
The previous developer used .h files to include PHP. My current configuration is taking that .h file and including it without executing it. What do I need to look for in my apache config (or is it my php.ini)?
EDIT:
Something clicked when I read one of the comments below. The code is using ASP style tags "<?". I've turned the option on in php.ini and according to phpinfo(), it's enabled, but apache is still just including the code as text.
I just checked it and running the code with the full opening PHP tag "<?php" fixes the problem. Having said that, I'd still like it to work the other way.
I'm running the code on a Macbook with the latest versions available. PHP 5.2.6, postgresql 8.3 and apache 2.
The code works on the staging server, but I can't figure out what the difference it.
EDIT
Durrr...I didn't have short_open_tags enabled in php.ini.
Could the problem be that the .h file you are including just starts into php code without the opening "<?php" tag?
From include documentation:
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
PHP is an interpreted language, so you can't 'include' a file without executing it. Remember that '.h' is just a file extension, so although the previous coder may have put PHP in a '.h' file, it's still just PHP.
More details on the problems you're running into would be helpful.
you can use a .htaccess file and add:
php_value auto_prepend_file /path/to/include-file.h
Edit
I've just read that .htaccess only works with the module version of php.
You should change them all to .php extensions. But, if you are going to leave them as .h, you change the mapping in Apache. It's Apache that runs the file through the proper interpreter, not PHP. The PHP engine will process anything passed to it.
include and require will execute the included file. If you are using readfile, it simply echoes the file without treating it as PHP code.
If the .h files are "missing" the <?php directive, then you may have to switch from include to something like:
eval( file_get_contents("file.h") );
Personally I try to avoid eval whenever I can, but sometimes there is just no choice.

Categories