I have HTML and PHP files that include the "header" HTML for my website. In HTML files I include this header using
<!--#include virtual="/top.ssi" -->.
top.ssi in turn includes other files:
<!--#include virtual="/navbar.ssi" -->
<!--#include virtual="/advertising/slider_advertising.ssi" -->
and slider_advertising.ssi includes:
<!--#include virtual="/advertising/advertising.php" -->
This is the critical file as it prepares advertising data for display.
All the above works great when run from HTML files.
Now I have some PHP-driven webpages (Logon.php) that also want to display the header of the website using top.ssi. This code starts with:
<!DOCTYPE HTML>
<html>
<head><title>Login to HVmusic</title>
<?php
virtual("/top.ssi");
...
Here is where the problem comes in. The virtual("/top.ssi"); executes OK, but stops executing after it encounters the PHP file that is included in top.ssi <!--#include virtual="/advertising/advertising.php" -->. The output from advertising.php is displayed and then the PHP output from logon.php stops. So the output stops without even displaying the entire header.
If I remove the statement for <!--#include virtual="/advertising/advertising.php" --> then the logon.php displays it's page normally (without, or course, that bit of the header displayed by advertising.php). So this tells me that PHP is having a problem with a virtual() file that includes another PHP file.
Is there any way to fix this? Is it some known restriction in PHP? I've been googling for two days and can find no mention of this issue. Thanks for any help you can offer.
Thanks for the tips, that clarified my thinking. I have just converted all my web pages to PHP and this solved the issue. Most of my pages were already PHP, so it wasn't much work to convert the HTML pages. All the include files I used were easily converted to use <? php require "filename"; ?> instead of <!--#include virtual="filename" -->
The underlying problem probably has to do with some conflict about running in PHP, then calling virtual() which calls an apache instance, which then calls another PHP instance to process the PHP include files I used.
Related
I'm writing a small website that has multiple pages. I'd like to have the same footers on each page, but I don't want to manually update 10 pages of HTML everyday. I'd like to put a PHP call to an external file in each HTML page (now .php pages, thanks to #br14np) so that when I update the PHP file, all the pages - when loaded - will show the same footer text.
<p><?php footertext.php ?></p>
is my wild guess at loading the content in the file of the afformentioned name but to no avail. (In footertext.php the code is: <?php print("Test numba one") ?>).
How can I go about doing this? I'd prefer an answer involving PHP.
UPDATE:
This is the exact code I'm using. Everything is in the same directory.
Main File:
<html>
<head>
</head>
<body>
<p> Content: <?php include "footertext.php ?></p>
</body>
</html>
Footer Content:
echo 'Test numba TWO!';
Use the include function. Just give it the path to your file. Example:
<?php include "footertext.php"; ?>
There are a few other functions that do similar things, such as require_once(). You can read more about that here.
Response to update
You're missing closing quotation marks after "footertext.php. Another tip that may help this situation is to turn on php error reporting. This will display any syntax or other errors on your page. Just insert the following code at the very top of your pages:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
Also make sure you have opening and closing php tags (<?php ... ?>) in your footertext.php file.
The best fit solution for your query is include or require functions of php. now you need to identify which one out of those are your choices based on their functional behavior.
PHP include and require Statements
In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.
The include and require statements are used to insert useful codes written in other files, in the flow of execution.
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the
include will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename.ext';
or
require 'filename';
You may like to go through the details of
Include,
Require,
Require_once &
Include_once.
Enjoy!
Anand Chavan
I have a menu that appears on all my pages. It contains php for passing filenames to url's as follows. Here is a snippet...
document.write('<li>abc</li>');
document.write('<li>def</li>');
document.write('<li>ghi</li>');
It includes css and that works fine.
I have saved the menu as a seperate .js file and used...
<script src="js/menucss.js"></script>
...to load it from each page that I want it to appear. The menu displays and the css works fine - however the php does not parse and the page shows the php markup. What am I doing wrong?
Thanks in advance.
Neil
You shouldn't do it like that.
Have a page called menu.php saved somewhere, which includes:
<ul>
<li>abc</li>
<li>def</li>
<li>ghi</li>
</ul>
Then include it, on the server side (index.php):
<html>
<head>
<title></title>
</head>
<body>
<header>
<?php include("main.php"); ?>
</header>
</body>
</html>
This approach is far superior:
It does not require an extra request to get the data.
There will be no flickering or awkward moments of data loading, the menu would appear as if you've coded it directly into the page.
It's much faster.
It doesn't use JavaScript, which the client can choose to disable.
You confuse server side and client side. PHP is about server side and PHP code it is interpreted on server. JavaScript is about client side, and JavaScript runs when user already has gotten html page, so when you dinamically add php in html on client side by JavaScript it cannot be run.
From what I understood I think that you are Trying to run PHP inside a .js file.
You should include that code in a PHP file.
The problem you're experiencing is probably due to the fact that you're attempting to run PHP on a .js file, and your server is not configured to it.
Change the file's extension to .php and use:
<script src="js/menucss.php">
<html>
<head>
<!--#include virtual="header.php" -->
</head>
<body>
aa
</body>
</html>
I'm trying to put the header in a different file and call it with an include. But I can't make it work. Right now the only text in the header is the word header. The header is at: www.chusmix.com/game/header.php how can I call it? I don't care if I use include virtual, I just don't know how to do it.
Thanks
<?php include('header.php'); ?>
Your include needs to be in PHP -- the way you attempted to include it is a very old method of SSI (sever side includes) and has nothing to do with PHP.
In order to include this way, the page doing the including must be parsed as PHP, and on most servers that means ending the filename with a .php extension. You can have plain HTML in a .php file so you can just rename the file (if its not already) to .php and then include the line I wrote above.
include('header.php')
include_once('header.php')
require('header.php')
require_once('header.php')
take your pick ;)
To clarify: include and require differ only as much, that if file does not exist, include will only display warning, while require will throw fatal error.
The *_once functions on the other hands make sure file is included only once. Try including the same file two times, you'll get an error. *_once makes sure the 'including' is done only once.
On the other hand, *_once functions bring you a bit of overhead, but that should be taken in account when writing bigger applications.
You need to include it in the php code:
include (header.php);
I'm having problems embedding php inside an html file.
I first ran in to this problem when I was trying to 'include' a php file inside tags, and thought it was related to css formatting, or something. But now I've broken this down into the simplest php and html possible, with an example from a book that should work, and I'm still getting this problem. Here's the html sample that doesn't work:
<HEAD>
<TITLE>PHP inside HTML tester</TITLE>
</HEAD>
<BODY>
<?php
echo "Hello World";
?>
</BODY>
</HTML>
I'm expecting 'Hello World' to show up in my browsers, but nothing is displayed. When I try to 'view source', I see exactly the text above. I figure that after all the examples of this I've tried, the code is ok, but something is keeping what's inside the from being recognized.
Any suggestions? And thanks for helping me out with what's probably a dumb question.
There is something wrong with your PHP installation. The web server isn't passing requests for PHP pages off to the PHP interpreter.
If you did indeed save the file as an .html file, then your PHP code will never execute because most web servers have their handler mappings set to route only PHP (.php, .phtml, or .inc extensions) files to the PHP interpreter.
Looks like your server is not able to handle php or your server does not know how to handle the file type with - this code is in.
Hey everyone... I'm not too familiar with PHP... but I have PHP code that basically "includes" a file if a condition exists. This PHP code is going to be pretty lengthy and I was wondering if it was possible to create a .php file, and just INCLUDE THAT in my shtml files? What the below is doing is placing the correct navigation bar in the shtml file by running a series of if/else statements in PHP. If the REQUEST_URI (the current page) is xxxxx.shtml, then include THIS navigation bar file, etc. This is working perfectly, but as you can imagine for a site that has hundreds of pages, I would have to place each page in this below "conditional server side includes"- and the disadvantage to this is if some menu items get rearranged in the navigation bars, I want to be able to just edit ONE PHP file rather than every single shtml file that includes the below script. I HAVE NO IDEA how to include the below PHP into it's own file. I assume I need to leave out the as that's specific to HTML. But aside from that, is it just as easy as copying and pasting the above into it's own PHP file?
Currently, here's what I have in every one of my shtml files (it WILL get bigger, this is to just start out):
<!--#if expr="$REQUEST_URI = /1/"-->
<!--#include virtual="navFiles/featuresCurrent.shtml" -->
<!--#elif expr="$REQUEST_URI = /2/"-->
<!--#include virtual="navFiles/videosCurrent.shtml" -->
<!--#elif expr="$REQUEST_URI = /3/"-->
<!--#include virtual="navFiles/aboutusCurrent.shtml" -->
<!--#elif expr="$REQUEST_URI = /4/"-->
<!--#include virtual="navFiles/subscribeCurrent.shtml" -->
<!--#elif expr="$REQUEST_URI = /5/"-->
<!--#include virtual="navFiles/toolsCurrent.shtml" -->
<!--#elif expr="$REQUEST_URI = /6/"-->
<!--#include virtual="navFiles/membershipCurrent.shtml" -->
<!--#endif -->
I want to be able to create an individual .php file that contains the above code, and instead of putting the ABOVE in my shtml pages, I would just put something like "include xxxx.php". Is this possible?
Yes you put your code in a file and include that file like this:
<?php include ("path to file");?>
Have a look at include function.
you can use
auto_prepend_file = /path/to/file.php
in your php.ini or .htaccess file which will "require_once" file.php before every php script.
If you want this script to be available to your entire application, you should add its path to the library path in your php.ini by setting include_path = 'path\to\php\file'
Example of how to do that:
http://www.cyberciti.biz/faq/how-do-i-set-php-include-path-in-php-ini-file/