I have an HTML file that has a rather long navigation menu inside of it. I want to take that menu out of the HTML and place it into an external PHP page and then call it with
<?php include 'navigation.php'; ?> in the HTML file.
I have tried just adding this into the HTML file but it doesn't display anything as well as no errors on the page.
What do I need to do (if it's even possible) to keep the files HTML and use the php require function?
Add this in in your httpd.conf and then you can process PHP code on HTML pages
AddHandler application/x-httpd-php .php .html
Q: Did you give the page a .php suffix? That should be all you need to do.
Remember the way PHP works - you basically "embed" your PHP code in an HTML page, and the server executes the PHP before it serves (the rest of) the HTML.
But in order for PHP to "see" your code, you need to make sure your "HTML page" has a .php suffix.
As a crude workaround, you can add ".html" to the list of file suffixes that PHP will parse.
But this could cause other things to break.
If you want to embed PHP code in your "index.html", the best, cleanest approach is to simply rename it "index.php".
IMHO...
Related
I am having trouble with PHP includes, and I am not entirely sure I am using them correctly. What I have so far is an HTML page that I want to add a search bar to. My PHP code in the HTML page looks like this.
<?php include 'site/tools/search.php'; ?>
The problem I am having is that the search bar is not displaying on the HTML page. I know that the search bar works, because I have browsed to that file location and worked with the actual search bar.
You cannot use PHP code in HTML documents. Change the extension of your .html file to .php and it should work without affecting anything. This would mean any links to the page would have to be changed accordingly.
Check that your file ends with .php (not .html) and everything should work (your include statement is correct). If this fails, try using an absolute URL.
Use the complete path to the file with your include statement. Like so:
<?php include '/home/yourusername/public_html/site/tools/search.php';
It may be a problem with the path of the file. If you are using a relative path try adding "./" in the begginning like this: "./site/tools/search.php"
I downloaded a Template + CSS File for a Website that I'm Building, the template worked well until I tried to break it down and put every code in its own file (for easy modification and editing in the future).
So, when I cut the head part which included (Title + Meta Data .. etc ), and put it in its own file, and replaced it (for sure) with an include() function, I lost the CSS styles and returned to the basic & standard style (Black & white with no extra format .. etc)
Where did I Go wrong? Knowing that here is the include function that I've used:
<?php
include 'files/head.php';
?>
With an URL like file:///C:/xampp/htdocs/test6/index.php PHP is NOT executed. You must run it with apache being involved. Currently you are opening your PHP script as a regular txt or html file - it is just passed to browser without processing.
In order to make include function work you must run it with apache. As you are using xamp, I think you should simply open it with URL like http://localhost/test6/index.php In this case, apache will get that request and pass it to PHP. PHP engine will interpret your PHP script and "replace" include files/head.php with a content of head.php.
If everything is Ok, after pressing Ctrl+U (or looking at HTML with Developer Tools or Firebug) you should see a content of head.php instead of <?php include ....
Please note that css files should be linked with relative URL like css/screen.css. Or absolute URL like http://localhost/test6/css/screen.css. like Search for relative and absolute URLs in google for more info.
I have two files:
html
php
where html file is calling the php file to do something.
Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.
I would like to know how would I do so so that both html file and php file have the same PHP session ID.
Thanks for your help in advance.
You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.
As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.
To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.
EDIT
To answer your qs in comments below -
Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.
Myfile.php
<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
Go to next page (<?php echo $_GET['next_page']; ?>)
......
</body>
</html
so you just open and close php tags where you needed php stuff to go.
To print session ID - just use somewhere in the HTML of PHP file.
Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.
If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:
# Add this to public_html/.htaccess file
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
You should be able to reload the html page and your PHP code will run great.
Is it possible to have Movable Type spit out static html even though there is php code in the banner_header? I want to build a completely static page (no php addHandler allowed). Currently Movable type will echo out the php code if I view the source. Instead of doing that can Movable Type actually process my php so if I said <?php echo "hi";?> it would say hi on the page instead of <?php echo "hi";?>
Why don't you build your header and footer with MT instead?
A static Movable Type site produces html that is no different from a hand coded html file. I'm afraid it's impossible if you can't touch the .htaccess file (to add the handler).
It looks like you're publishing your site pages with .html extension, but without specifying the 'addhandler' .htaccess rule to interpret .html pages as PHP, your php coding would appear as such, instead of being interpretted as PHP coding.
This has nothing to do with the movable type capabilities.
You could either implement the addhandler .htaccess rule to make your .html pages be handler as PHP pages, or you could change your site files extensions to .php or finally you could just code your header and footer within movable type using static html coding.
I'd like to input some code, for example a menu in HTML from another file so that I can edit that menu and then all the menus for all the sites would change as they'd be linked to that page. Is there a way to do so without making all the pages .php?
Server side includes are going to be the best way to do this, but if that's really not an option you could do it with JavaScript - load the contents of another file using AJAX when the first page loads, and insert that content into a specified element on the first page.
For example (using jQuery, because it's simpler to write out here):
$.get('page2.html', function(data) { $('#whereToPutContent').html(data); });
You don't need to make all the pages in php. As long as the page you're going to include doesn't have php code, it can be pure html, or txt, or whatever.
The include HAS to be in a php page, that's all.
So, in your PHP page, just use include (or require) and you're set. For example:
<?php
include ('menu.html');
?>
Yes, it's possible with frames or AJAX (use <script src>). However, frames are deprecated and AJAX is only reliable if the browser has JavaScript enabled.
So PHP is the (only) solution here. Here are the four possibilities:
<?php
include 'menu.html';
require 'menu.html';
include_once 'menu.html';
require_once 'menu.html';
?>
You probably want to use include_once for a menu so that it is only include one time. Or if you are sure that it is only included one time you can just use include. require stops the script if it can't find the file, so that's probably not what you want.
You may use server-side includes (SSI):
include ./includes/include.html
or
include ./includes/include.ssi
or
include ./includes/include.shtml
iframes:
<iframe src="http://website.com/index.html">
<p>Your browser does not support iframes.</p>
</iframe>
or javascript (AJAX):
STEP 1: add code to html file
<script language="JavaScript" SRC="http://yourwebsite.com/js/file.js"></script>
STEP 2: configure [apache] server by adding to .htaccess file. Add this line:
AddType application/x-javascript .js
The pages including the menu page would have to be PHP yes, the menu page itself doesn't have to be. For this you can use: include()
include './file.html';
you can dynamically display menus (or whatever) with JavaScript.