Another complex question - that probably has a simple answer.
I am trying to execute a php code in my html file. It won't work at all. Doesn't even start.
A basic example, like:
echo "Hello There";
Nothing happens. NOTE: This is on a real server - not my local machine.
Full php files work, but not code islands in html.
The server is running Linux, and PHP Version 5.2.17.
The file I'm testing with is: i-graphicsdesign.com/test/index.htm
If you look at the page source you can see what's missing.
If I change the htm extension to php the file works.: i-graphicsdesign.com/test/index.php
Is there a different way of calling php on a Linux server,
does the php installation have to be configured to recognize htm as a possible php code source?
do I have to use the php extension on every file that contains php code?
Your file needs to be a .php not a .html
That seems to contradict the info I get from other sites.
For example: " Escaping from HTML - Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents." From php.net
What you've read is correct. And it'll work as long as the file uses *.php as its filename extension. Otherwise, the file will be ignored by the PHP interpreter, and will be treated as a simple HTML document. So, change the filename extension to *.php. Or you can use this convention *.html.php for your PHP templates if you like. e.g. index.php, index.html.php etc. Take your pick.
After the last 3 hours work that's what I was beginning to suspect. However, that sounds like a PHP file with HTML code islands in it.
Anyway, I was trying to set a variable and execute a 'silent' PHP file when the HTML document loaded. Perhaps there's a way to use the OnLoad command.
<i> more research...</i>
So far the problem is not solved.
For this application I can not use the PHP extension.
I am trying to use one or two simple lines of code to;
pass a variable from HTML to PHP, and then
execute a 'silent' php file.
The example files remain: [index.htm and [URL="http://www.i-graphicsdesign.com/test/index.php"]index.php](http://www.i-graphicsdesign.com/test/index.htm)
Any help would be appreciated.
Hi imillard. The ideal is to have the .php extension on your pages (and there's no reason not to, IMHO, except that your site may already be built with .html files, which would make it a bit of a pain to change everything).
However, you can tell your server to run PHP on .html / .htm pages. You just need to do this via a .htaccess file, as described here:
I removed the PHP info link from your first post, as that's considered a huge security risk to do that. Ideally, once you've checked that PHP info, remove that page from your site altogether.
Ralph: Thanks for the deletion. I've heard that can be a problem, but I'm not sure how the info could be used. I wouldn't think some huge servers in Arizona would be that easy to break into. But, I'll take your word for it.
As to passing variables, I thought there was some method of setting a variable in JS that could be read by PHP. And, a PHP app could be triggered by a window or body load, after the variable had been established. Renaming everything to PHP seems like a cop-out, but as a last resort....
I tried resetting the PHP to read htm files, but the whole site stopped working - so that approach is out.
This is the part where I look to people with superior experience in coding.....
Heh heh ... Hackers seem to be able to get into just about anything.
As to passing variables, I thought there was some method of setting a variable in JS that could be read by PHP.
JS isn't all that reliable, though, as people can turn it off ... so I wouldn't force PHP to rely on that.
I tried resetting the PHP to read htm files, but the whole site stopped working - so that approach is out.
Did you edit / create your .htaccess file in the root folder? Adding the code shown in that page I referenced to a .htaccess file should not cause a problem. Sometimes it can be tricky to create or edit a .htaccess file, but it's ultimately just a simple text file. I find it's generally best to log in to your web hosting control panel and open the .htaccess file from there, if there is one. What steps did you actually take to do this?
The whole point fo PHP is that you need to tell the server to look out for it, and the standard way is to end the file with a .php extension. Other than that, it makes no difference whether you use .html or .php. I always use .php by default, as I might want to put some PHP in there at some point. (Well, I always do, in fact, as I normally have PHP includes on every page.)
If you don't like the look of the .php extension, I never show that anyway. Instead of having a page file like page.php, I put the page in a folder called /page/, and then call the file index.php. That way, your URL looks like this:
... which I think is a lot nicer than
I edited (in notepad) the .htaccess file. It was empty, (length of 0) so I added the code as shown. It made no difference in IE - the PHP still didn't run. However, FF raised a dialog, asking to save or open the index,htm file. If I chose Open, it saved the file to the local drive and opened it, and the PHP still didn't work.
I went into the control panel on the server I use, and tried adding the htm extension to the list of files that PHP looked at. As I said, everything stopped working so I removed those additions.
Now this project has reached the point of diminished returns. I can't invest any more time in it. From now on all my files will be PHP extension.
In the mean time I will use htm files with a one-line redirect to the new php names until the search engines catch up.
I am the kind of guy who never adopts anything until there is sufficient technical advantage in upgrading. You won't see me camped outside any store waiting for the newest..... especially an Apple product. For a long time I have limited my PHP usage to external modules, but now the advantages of mixed code are clear - so I'm diving in.
Thank you for your help and advice.
If you created a new valid .htaccess and uploaded it in ASCII it should have had something on these lines:
AddType application/x-httpd-php .html .htm
# Process *.htm and *.html as PHP
If you wanted to to process .htm as PHP maybe you should have tested the .htaccess in a sub directory first. The phpinfo() can tell people about your server environment and file-system structure so usually it's best not to publicly post it because if it's not hardened you're giving outsiders extra info/ammo.
That is exactly what the .htaccess file had, courtesy of php.about.com I have little faith in my typing ability, so I copy and paste to avoid errors.
And I did try the file in a sub-directory, where the rest of the subject files were located. When it didn't work I also copied it to the root folder. Still no joy. I try all the obvious things - and a few obscure things too - before asking for help or throwing in the towel.
One thing still ticks me off a little: All the books I have read (and there have been many) and all the sites where I have gone looking for help have all said about the same thing. To run PHP in your HTML file, simply use <?php and ?> to surround the code.
Not one of them mention the "HTML" file has to be saved with the extension .php.
Which means it's not an HTML file: It's a PHP file with HTML code in it. An HTML file has the extension htm(l). Makes me crazy.
I'm not sure if that's technically right or not. But I think of it as an HTML file, with an extension to alert the server that there's some PHP to process before the page is sent to the browser.
Which is the best way to explain this I wonder?
On most servers, if it receives a request for index.html (or index.htm) the server tries to find the file, picks it up and serves up the contents of that file.
On a PHP configured server, if it receives a request for index.php the server tries to find the file, picks it up, passes it to PHP which does some work and then serves up the contents of that file.
index.php might contain a mixture of straight html but also some PHP - eg <?php echo time(); ?>
That is great too.
Now, what you want is to have your server serve up a html file -- BUT you want it to be passed to the PHP processor.
Whoa. So now you want to break the general rule, so be fully aware that what you want to do goes against the grain.
Can it be done ***, certainly, yes, as has been explained previously by the good folk of this forum.
Does that take some knowledge, yes it does.
Is this exception to the rule documented in PHP books, no it is not.
Is that the fault of the PHP book authors? No, not in my opinion, nor in the opinion of others on this forum.
Let me try and explain it with a simple allegory.
Imagine you bought a normal little compact car from me, and then decide you would like to cross the Russian tundra in winter in my little car.
You are going to need at least snow chains for your tyres, and extra strong anti-freeze so your engine does not freeze up.
Just because you cannot find any reference to snow chains and how to handle -70 degree temperatures in the owners hand-book does not make me, nor does it make the car -- nor even the owners handbook -- invalid.
It is doable.
It means you have to accept that what you want to do is outside of the norm, so you will have to do some work, but don't go phoning me from Moscow saying your car broke down.
*** I ran a large site which was written in .asp, so I reconfigured the server to serve up .php pages as .asp pages so that we would not lose any SEO points and left the URLs as they were. I'm not saying I would do the same now, but it is doable.
Cups: I'm afraid your vehicle analogy is flawed for several reasons - not the least of which is, every car owner's manual I have ever seen has information on snow tires and winter driving. **
"what you want to do is outside of the norm" In fact, everything is outside the norm until enough people do it, when it becomes accepted and then the norm. And I have been 'doing a little work' for most of the last two days, only to discover the usual self-styled experts have been at work too.
Bottom line: To talk blithely of inserting PHP in an HTML file, but not mention the file must be saved as PHP is a glaring omission. In the same category as failing to mention the maximum dosage on the pain killers - because of course everybody Knows you only take two....
Through the years I have found many experts who love to show off their skills. Unfortunately, they have all lost touch with the people they are trying to teach, assuming the reader has some 'common knowledge' from a mysterious unnamed source. You tout yourself as a PHP Guru, but you seem to forget that everybody - even you - started at zero, and had to learn everything from the ground up.
If I announce a way to put an active table of numbers into a word processing document, but fail to mention the document must now be saved as an XLS file for it to work.... you might be a little irritated. So should I point out that you really ought to know that without being told? And should I look down on you because you said XLS is not the same as DOC?
I have learned something from this forum. Well, I've learned several things..., but mainly - thanks to Ralph - I will be writing my pages in PHP with islands of HTML code in them. And I maintain the extensions we use when saving files will label the file for what it is. Otherwise, why bother with file extensions at all?
** Snow is something I can deal with. I have lived in northern Ontario, and now reside in Atlantic Canada. We know snow - and snow tires.
I am 6'4". How tall do you think this snowbank is? And -45° is a normal winter night. Winter allegories are lost on us. We sneer at winter!
Yes, that's a big problem the world over—one that frustrates me a lot. But as a teacher, I also realize that you always have to assume something about your audience, and the trick is to work out what's reasonable. If you are reading an article on PHP, you don't want the writer to teach you English first, for example. A good writer (like a good teacher) will often mention at the start some prerequisites for understanding what's to come, so that you are warned in advance if it's going to be over your head.
This is why I tend to go for books instead of random articles here and there. If a book call itself "a beginner's guide", there is a kind of contract with the reader that you will be told all you need to know. Of course, many writers fail to honor this contract ... but it's not an easy task, I have to admit.
Actually I do not tout myself a PHP Guru, people on here voted me that last year. I am not at all comfortable with that badge, but I agreed to sport it, because maybe it was "my turn", because while am not a true guru am a trier - and I do care, a lot. I am in awe of the many visitors on this site who are PHP Gurus, but secretly do not count myself amongst them.
So, I am not slighted by your remark at all.
The fact that you can read about PHP development but not realise you had to save the file as .php makes me sad.
Just like to point out, the documentation for PHP assumed you have read this part:
In this tutorial we assume that your server has activated support for PHP and that all files ending in .php are handled by PHP. On most servers, this is the default extension for PHP files, but ask your server administrator to be sure. If your server supports PHP, then you do not need to do anything. Just create your .php files, put them in your web directory and the server will automatically parse them for you.
It states quite clear that the extension to be used is ".php" and not ".htm(l)". Further along, it is also stated here:
The server finds out that this file needs to be interpreted by PHP because you used the ".php" extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.
I see no omission in the documentation concerning this.
Yeah, I think imillard's complaint was that people who write about PHP and give tips often forget to mention things like this. But they are usually assuming that they are dealing with people who already know at least the most basic things. Of course the official manual needs to lay out all the details of the language. And as I said, there's more responsibility on books to provide readers with the info they need.
This topic is now closed. New replies are no longer allowed.