PHP: script works on some machines only - php

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);

Related

php script works fine, but not inside HTML

I've installed WIndows 10 version of XAMPP with MySQL (MariaSQL) & PHP version 5.6.28
The SQL is working just fine, i.e., I can log in, create DATABASE, etc.
php -v works fine on the command line ... PHP 5.6.28 (cli)...
phpinfo.php works as expected.
It tells me display_errors is ON
php.ini = display_errors=On
As a php script: mysql_test.php output to the screen is fine.
<?php
echo "Hello World of PHP!";
echo mysql_connect ('localhost', 'joe', 'gonzo9876');
?>
When I embed it in plain vanilla HTML, i.e., http://localhost/mysql_test.html
The php code won't echo/print on the screen
- and -
when I right-click for viewing the source code, the php code is visible
- and -
the Google debugger has converted the php tags to
Your Apache, by default, will only run files with .php extension as PHP. .html will be displayed to browser as is.
You need to either:
Rename your file from mysql_test.html to mysql_test.php; or
Config your Apache to also treat .html files as PHP script
The later one is an unusual practice. I wouldn't recommend it.
Basically no hosting provider will do it. So even if you make it work in your XAMPP setup, it won't work in any normal shared hosting. So if you potentially need to move your code to a shared hosting, please don't do it.
You can't process the PHP code inside html page with .html extension(without parse). It's only for rendering html, if you wanna use embed/mix both php with html, then use .php extension instead as PHP is server-side scripting language. When talking about server-side language, you need a server either local(xampp,wampp,etc..)/production server to host and run your apps.
Reflect to Commenter's comment :
Another workarounds is by telling the Apache to treat .html as .php and with this, you can mix php code with html by using .html, but it's just kinda a HACK for me(personal perspective). Well the choice is yours.
You need to make extension .php if you want to put php code inside html tags. But if you do not want to show .php, please use .htaccess for url rewrite. You can make file as .php but with .htaccess you can show as .html so user will see it as .html.
RewriteEngine On
RewriteRule ^test.php test.html [R=301,L]
or something like this, please search for url rewrite for more detail.

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.

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

Having trouble getting php to work

I am just starting in web development, and I am having trouble getting php to work. I have access to a domain on my universities' server that should have php installed, although I'm not entirely sure about that.
This is the code I am trying to run.
<html>
<body>
<p>
testing to see which scripting languages work
</p>
<?php
echo "php works";
?>
<script type="text/javascript">
document.write("javascript works")
</script>
</body>
</html>
I notice you are able to 'run' HTML and javascript code by opening the text file with a browser (I use Chrome). But when I view the page source inside the browser, the php is commented out. This same code is loaded onto my website the university provides, and the same thing happens there.
I've looked around for how to solve this, I figured you need to have some sort of flag indicating the page runs php, but I can't find anything.
Here is my webspace if it helps:
Removed for privacy purposes
Thanks everyone for the responses, I was able to get it to work by changing it to a php file.
The usual flag for indicating that a file contains PHP is to give it a filename ending in .php.
Support from the server is, of course, required.
In more general terms, there are lots of different ways that this can be done, but they depend on the specific web server software that is being in use.
With Apache HTTPD, you use the directive SetHandler application/x-httpd-php inside a conditional.
For example, to take the Debian php5.conf file:
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
This causes any files with a name that matches that regular expression (i.e. end in .php, .php3 or .phtml) to be processed by PHP.
For certain, your server is running php. I tried to invent a web page name that i was certain didn't exist and got this error:
Apache/2.2.3 (Red Hat) DAV/2 mod_pubcookie/3.3.4a PHP/5.1.6 mod_python/3.2.8 Python/2.4.3 mod_ssl/2.2.3 OpenSSL/0.9.8e-fips-rhel5 mod_perl/2.0.4 Perl/v5.8.8 Server at home.engineering.iastate.edu Port 80
You're running PHP, alright. Like everyone else has mentioned, you'll have to ensure that the filename extension of that file whose code you posted above ends in .php then try it again.
You can't just open a php script in a browser, because it has to be executed by the php program first. This is done by a webserver (you can install one to test on your own machine, see, e.g., XAMPP).
If the server is configured to execute php (which it seems to), you should be able to make it work by naming your script index.php. The file extension .php tells the server to run the script first before sending anything to the browser.

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