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.
When I try to open js.php, it gives an error in xampp while in 000webhost it doesn't . I don't understand why.
so here is the code.
js.php
<?php
header('Content: text/javascript');
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
index.php
<?php
session_start();
$_SESSION['myjskey'] = hash('md5','examplekey');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
this is a test.
<script type="text/javascript"><?php include('js.php'); ?></script>
</body>
</html>
Website js.php
Xampp Js.php
Edit 1: I cannot do session_start(); in js.php as it will give an error index.php
(session already started.) Removing the header didn't work.
<?php
//js
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
make this change no need to use
header('Content: text/javascript');
How i include /master/header.php in /about/index.php?
localhost:8888/about/index.php:
<?php
include '/master/header.php';
?>
<?php
include '../master/header.php';
?>
Two dots mean back to main folder
Or:
include($_SERVER["DOCUMENT_ROOT"] . '/master/header.php');
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'];
?>
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")
?>