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
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Im trying to send 2 variables to my php page via a link, i can't figure out what im doing wrong. The outcome is not right. Im expecting just the display of values and instead the whole php code displays.
Thanks for helping in advance, below is my html, php and result.
My html page is named "index.html" and my php "default.php"
HTML:
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Information sänd via adressfältet och länkar</title>
<meta charset="UTF-8">
</head>
<body>
Link
</body>
</html>
php:
<?php
header("Content-Type: text/plain");
echo $_GET["name"];
echo $_GET["gender"];
?>
Result:
<?php
header("Content-Type: text/plain");
echo $_GET["name"];
echo $_GET["gender"];
?>
There are a couple of factors affecting such
first one confirm that your php module is properly configured to Apache and it reads the .php extension
two that your file is actually saved as .php if you are using a text-editor like notepad which appends the .txt file format at the end, unless you select file type as all files
I found the problem thnx to this thread: PHP code is not being executed, instead code shows on the page
The problem was:
Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php
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:
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.
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');
?>