Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>
Related
Here is what I have so far
file_put_contents('./'. $dir .'/'. file .'.php','<?php include "../head.php"; echo $head; ?>' . $body);
I also tried
file_put_contents('./'. $dir .'/'. file .'.php','<?php include "../head.php"; ?>' . $body);
and then echo $head; in the /head.php
I want a head.php that is called by each of the generated files. There for I need to insert the php code <?php include "../head.php"; ?> on top of the files.
with the given code <?php include "../head.php"; ?> it shows  on top.
head.php
echo '<html>...';
echo $_SERVER['REQUEST_URI'];
...
The issue is, it shows .
I don't think this is a dupe.
Im new to Php. I kinda need code to open Links in browser when script is loaded.
heres my code below.
<?php
$links = array_open("https://stackoverflow.com/",
"https://outlook.office.com/",
"https://www.protectedtext.com/",
"https://www.adobe.com/",
"https://www.linkedin.com");
echo $links[array_rand($links)];
?>
Hi change array_open to array
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
echo $links[array_rand($links)];
?>
And if you want to send the browser to the link you would send a location header like so...
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
header('Location: ' . $links[array_rand($links)]);
exit;
?>
So I would like to print name of the current page in the title tag of the head.
I include my head in every page like this:
include 'includes/head.php';
This is my head:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
I thought this would work, but now it just shows "Head" on every page.
I know I can make it work by just putting the $page variable on every page but I would like to prevent this.
So is there any way to print the name of the current page through the included head.php file without adding anything to every page?
Thanks
EDIT
This is how I fixed it:
$page = pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME);
If you were to create a file head.php with the following content
<?php
$xpage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_BASENAME );
$ypage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME );
echo "
<!--
with extension
{$xpage}
without extension
{$ypage}
-->";
?>
and include in your regular php pages using include '/path/to/head.php' you should get the desired result ~ you will see two options - with or without file extension.
To add this to the document title simply echo whichever option is preferrable
<title><?php echo $ypage;?></title>
Try this enclosing the $page variable in php tags:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
You have several problems, first one is curly bracket in front of the echo, the second one is that end title tag is missing forward slash and probably last one is that page variable is not inside php tags...
So your code should look like:
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?></title>
You could use a function like this:
head.php
<?php
function head($page) {
echo "<head>";
echo "<title>".ucfirst($page)."<title>";
echo "</head>"
}
index.php
<?php
include 'includes/head.php';
$page = basename(__FILE__, '.php');
head(page);
I would like to include a site name from a file called sitename.php I have tried
$thisPage='include "sitename.php" '; ?>
and get error report. I also need to add the file into
include("../../../EngineerLocation/sitename.php");
instead of the words sitename I would like to include the sitename from the php file
You can either include an actual php file:
// sitename.php
<?php
$thisPage = "Page Name";
// otherpage.php
<?php
include("sitename.php");
echo $thisPage;
Or use file_get_contents
// sitename.txt
Page Name
// otherpage.php
<?php
$thisPage = file_get_contents("sitename.txt");
To include a file using the name in sitename, do the following:
<?php
$thisPage = file_get_contents("sitename.txt");
include("../../../EngineerLocation/{$thisPage}.php");
try:
include("$_SERVER[DOCUMENT_ROOT]/sitename.php");
My current project routes everything through index.php. Then I see what the $_SERVER['REQUEST_URI'] is, check against a whitelist, then perform some logic to include the relevant html template. Like this:
<?php
include("header.html");
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page in $whitelist){
$page_title = $requested_page;
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
}
include ("footer.html")
?>
My problem is that I want to use $page_title in the header template, like this...
<title><?php echo $page_title?></title>
But obviously it will not work because when the header is included, $page_title has not been set. Can anyone suggest how to get around this? Might "output buffering" help? If so, how?
If there is something majorly wrong with this way of using php to produce pages, please say so ! Thanks
EDIT: I should add, the conditional logic is quite a bit more complicated in my real project, and I've simplified it a lot here. I don't want to have to keep repeating my includes of header and footer, so that's why they're not included. I'd really rather keep them out of all the logic if possible.
You can reorder your code to achieve this:
<?php
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page in $whitelist){
$page_title = $requested_page;
include("header.html");
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
}else{
//Make sure we include header if page not in white list
include("header.html");
}
include ("footer.html")
?>
Edit: Actually thinking about it you could probably decouple the logic and the includes:
<?php
$requested_page = $_SERVER['REQUEST_URI'];
$includes = array();
$includes[] = 'header.html';
if ($requested_page in $whitelist){
$page_title = $requested_page;
$includes[] = $requested_page . "_controller.php";
$includes[] = $requested_page . "_template.html";
}
$includes[] = "footer.html";
foreach($includes as $include){
include($include);
}
?>
I wrote this for my load MVC library to load views, but I think this will become handy:
$target = "myfile.php";
$data = array(
'page_title' => 'page title'
);
ob_start();
if(count($data) > 0) extract($data);
include_once($target);
$content = ob_get_clean();
echo $content;
And you're able to use $data keys as variables with use of extract() function.
Easy
<?php
$requested_page = $_SERVER['REQUEST_URI'];
$page_title = "";
if ($requested_page in $whitelist) $page_title = $requested_page;
include("header.html");
if(!empty($page_title)) {
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
}
include ("footer.html")
?>
What about..?
<?php
$page_title = $requested_page = $_SERVER['REQUEST_URI'];
if (!in_array($requested_page, $whitelist)) {
header('Location: http://example.com/your404');
exit(0);
}
include 'header.html';
include "$requested_page_controller.php";
include "$requested_page_template.html";
include 'footer.html';
Try redirecting back to your index.php page with a $_POST or $_GET, with the page title.
If these are set, you can read them from the start of the page.
<?php
if (isset($_GET['pagetitle'])){
echo "<html><head><title>".$_GET['pagetitle']."</title></head>";
}
include("header.html");
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page in $whitelist){
$page_title = $requested_page;
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
header( 'Location: http://www.yoursite.com/index.php?pagetitle='.
urlencode('This is the page title'),'"' )
}
include ("footer.html")
?>