This question already has answers here:
Include another HTML file in a HTML file
(41 answers)
Closed 8 years ago.
for example my website is "myweb.com". how can i do opration like following.
include(http://myweb.com/file);
this is an internal URL.
for example i want to include
http://myweb.com/process.php?action=update
this is not a file
"?action=update"
thus how can i do this operation?
To include pages with PHP, simply put the following code in a.html, where you would like the code to appear:
<?php
include "b.html";
?>
You can't include an HTML document into another HTML document.
You can do that using PHP but the file that includes the other must be a PHP document. In your example a includes b therefore a is a.php while b can stay b.html.
Then inside a.php you can write:
<?php echo file_get_contents('b.html'); ?>
and the content of b.html will be included into a.php.
Related
This question already has answers here:
Get filename of file which ran PHP include
(7 answers)
Closed 5 years ago.
I want to get file name from URL in an included page for example in meta.php
I use basename(__FILE__) but I get 'meta.php'
how I can echo index.php in an included page??
<meta name="description" content="<?php
if(isset($FILEDATA_LANG['page_'.basename(__FILE__).'_keywords'])){ echo
$FILEDATA_LANG['page_'.basename(__FILE__).'_keywords']; }?>">
This has been asked earlier and you could've found your answer within seconds of searching, however, here's a proper response:
Set a variable in your index.php or a definition, such as:
define("THIS_PAGE", __FILE__); or $thisPage = __FILE__;
Then in your file you're including (after this variable), simply use:
<?php echo $thisPage; ?> or <?php echo THIS_PAGE; ?>.
(Keep in mind to check if the variable is set (isset($thisPage)) or (defined("THIS_PAGE")).
Reference: Get filename of file which ran PHP include
I'm trying by adding
.basename($_SERVER['SCRIPT_FILENAME']).
I Hope it would work when i publish this page
This question already has answers here:
Prevent direct access to a php include file
(33 answers)
Closed 7 years ago.
I am somehow a noobie in PHP and I want to learn. I am making a proyect, in which I use require('parts/header.php') statements to include functions and templates. My pages look like this:
<?php
include('core/checklogin.php');
//This will check if the user is logged in and can see this page or not
include('parts/top.php');
//This loads the <head> tags and the header, including the navbar
?>
<section id="mainArea">
<h1>Hello <?php echo getUserNickname()?></h1>
<p>Some stuff</p>
</section>
<?php
include('parts/bottom.php');
//This loads the <head> tags and the header, including the navbar
?>
The problem is if someone enters myproyect.com/parts/top.php he would see the top part, and that file is going to be executed. I don't want that. I was thinking doing some stuff in a .htaccess file like:
#.htaccess inside parts directory
dont_serve_anything_inside_this_directory_and_return_forbidden();
But I don't know how without affecting the server side code.
Another alternative is to use the equivalent of if __name__ == 'main': of python, and do like:
//parts/top.php
if(__name__ == 'main'){
header('Location: /index.php');
exit();
}
What could I do?
You need to create file .htaccess inside your so-called protected from the outside direct access folder and put the following content there:
Deny from all
This will prevent users from being able to access your files using http://example.com/parts/bottom.php
Just add .htacccess to parts folder.
This question already has answers here:
Return html response from PHP page in to variable
(2 answers)
Closed 9 years ago.
I have a php file myfile.php which is basically a lot of html and some php code within, for example:
<body>
Name: <?php echo $_GET['id'] ?>
<!-- and so on ... -->
</body>
I am using an open-source HTML to PDF converter written in PHP which requires as an input the html content to be converted:
PDFConverter::convertHTMLToPDFFIle($html_input, $filename_output_pdf);
How can I feed in the html generated from myfile.php?id=XX into $html_input?
You could use a little output buffering and include it.
<?php
ob_start();
include "myfile.php";
$content = ob_get_clean();
// $content now contains the processed code of myfile
At that point, the file contents has been processed by PHP because of the include and is in the $content variable. You can pass that to your convertHTMLToPDFFile() method.
PDFConverter::convertHTMLToPDFFile($content, $outFilename);
EDIT: OP edited question adding requirement that the included file needs to be able to accept variables.
When a file is included/required, it inherits the current scope. That means it has access to any variables, class definitions, functions, etc. that are defined. So, determine which variables are shared and set those values before including the file. For example:
<body>
Name: <?php echo $id; ?>
<!-- and so on ... ->
</body>
and then...
<?php
ob_start();
$id = <whatever the value should be>;
include "myfile.php";
$content = ob_get_clean();
If you have fopen url wrappers enabled on your host you can use file get contents
$html = file_get_contents("http://mysite.com/somefile.php?var=1&var2=2");
This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I looked around, but couldn't find a satisfying answer.
Problem:
I have a menu bar that appears on the top of the page. I want it to show across all of the pages on the website. So how would someone do that without copying the same code each time. Would someone use html, php, css, or javascript/jQuery to accomplish this?
Note: I want to have a separate html file to access the information from.
From what I have seen, this is typically done with php using a template file.
The template file may have HTML code in it that you want to display on every page, as well as placeholders for content that is page specific. e.g: template.php
<html>
<head>
<title><?php print $title; ?></title>
</head>
<body>
<nav>Test</nav>
<?php print $content; ?>
</body>
</html>
In this case, as long as $title and $content variables are set, you can then do a include 'template.php'; to output this HTML code in other php files.
Read more about php's include.
It seems that you will need to use include, although an explaination on how to use it (or at least an example can be found here: https://www.youtube.com/watch?v=XmoF-6vshSI
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
I have the following html list:
<ul id="ulGroups">
<?php
include('organizer_picturearchive_groups.php');
?>
</ul>
I am trying to include this file, which also includes another file:
<?php
include('lib/organizer_functions/picture_archive.php');
echo select_all_picture_category_groups();
?>
Basically, what I am trying to do is to call a function that is in the picture_archive.php file. I do that in the organizer_picturearchive_groups.php file.
I am including this file in the first file where the html list is. I am not pasting the whole code from there, because it is a lot, but it works.
However the include thing doesn't, because when I tried to make a test echo statement in the "organizer_picturearchive_groups.php" file it works and I can see the text I am printing.
Do you have ideas what may cause this problem ?
Double check the path. If organizer_picturearchive_groups.php is in the same directory as the code, then try configuring the path as relative:
<?php
include('./organizer_picturearchive_groups.php');
?>