I made a website in php and created three include files.
Header.php
Footer.php
and my content files.
about.php, our-services.php, contact-us.php and 404.php
and called these inside my default.php
<?php include('includes/header.php'); ?>
<div id="main">
<?php
$url = '';
$urlX = 'includes/404.php';
if (!empty($_GET['post_slug'])) {
$url .= 'includes/';
$url .= $_GET['post_slug'] . '.php';
$GetPage = $_GET["post_slug"]; // validate string!!
if (!file_exists('includes/' . $GetPage . '.php')) {
include $urlX;
}
else{
include $url;
}
}
?>
</div>
<?php include('includes/footer.php'); ?>
my .htaccess file is coded like this
<IfModule mod_rewrite.c>
RewriteRule ^pages/([A-Za-z0-9-]+)/?$ /default.php?post_slug=$1
[NC,L]
</IfModule>
Now my about.php file has some basic information.
so when i write domain.com/pages/about it shows my about.php and it works :)
the problem is when I make changes inside header or footer or in any file, my changes are not reflected. I have to press ctrl + f5 on every page. If I am at domain.com/pages/about it will reflect the changes on this page only so when i go to another page, the header and footer shows previous cached files and i have to again press ctrl + f5 on every page to see the changes. Every change on any included file doesn't reflect... please let me how to fix this problem.
Thanks
I have tried to add clearstatcache(); like this
<?php
clearstatcache();
$url = '';
$urlX = 'includes/404.php';
if (!empty($_GET['post_slug'])) {
$url .= 'includes/';
$url .= $_GET['post_slug'] . '.php';
$GetPage = $_GET["post_slug"]; // validate string!!
if (!file_exists('includes/' . $GetPage . '.php')) {
include $urlX;
}
else{
include $url;
clearstatcache();
}
}
?>
but doesn't work...
Related
I have a basic semi-static website written in PHP. In the root folder I have a file called posts.php and also a folder called posts, in which there are post1.php, post2.php, and so forth and so on.
the posts.php file is in the root folder. When opened it creates a list from the files inside the posts folder and links to them.
In essence, what I want to do is to open php pages that I create statically and store in the posts/ folder in the browser.
The problem is that when I try to open these posts I am unable to. I can hard-link to them, and this "works", but if I do so my base templating will not work.
When I click links I go from one page to the next, and the URL shows ?p=index or ?p=posts. post1.php should be in something like ?p=posts/post1, but it doesn't work.
There may be a problem in naming, since there is a folder and a php file with the same name (posts), but I'm not sure if that's it, nor how to work around it.
edit: Below are parts of the code that I believe pertain to this problem:
my index.php
<?php
require_once('functions.php');
require_once('header.php');
load_page();
// require_once('init.php');
require_once('footer.php');
?>
My header.php
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf8">
<title>Paulo RSS Alves</title>
</head>
<body>
<div>
<!-- these are working without any issue -->
<h1><a href='?p=index'>Paulo RSS Alves</a></h1>
<div class="bar">
<p>Posts</p>
</div>
posts.php:
<?php
$dir = scandir('./post');
foreach ($dir as $file)
{
$path_parts = pathinfo($file);
if ($path_parts['extension'] == 'php' and ctype_alpha($file[0])){
// the purpose of this code is to only consider files with
// the .php extension and to remove that extension from the url.
$file_f = str_replace('.'.$path_parts['extension'], "", $file);
echo '<li><a href='.'post/'.$file_f.'>'.$file_f.'</li>';
}
}
?>
and functions.php:
<?php
function load_page() {
(isset($_GET['p'])) ? $page = $_GET['p'] : $page = 'index';
if (file_exists($page) && $page != 'index'){
require_once($page);
} else{
require_once('init.php');
}
}
?>
init.php is merely a welcome screen.
and a schema of my filetree:
index.php
init.php
header.php
footer.php
functions.php
posts.php
posts/
post1.php
post2.php
You are not requiring from the posts folder
<?php
function load_page() {
$dir = './posts/';
(isset($_GET['p'])) ? $page = $_GET['p'] : $page = 'index';
if (file_exists($page) && $page != 'index'){
require_once($dir . $page);
// add directory ^^^^
} else{
require_once('init.php');
}
}
?>
I would just like to hear if it is possible to echo something on one page, and not every other page, even though they all get content from another file.
I have 3 pages, and they all have this in code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
include_once($path);
?>
If I want "index.php" to echo something that "about.php" does not echo, can you then do it?
As the title says, I'd guess on something like this:
if filename == index.php then
echo=hello
I wouldn't make it so specific If you add more and more pages what will you do? Handle them as special cases one by one? Now you have to handle two pages, after three months they'll be 10...and so on.
It's not a perfect solution but as starting point you may use a flag. Beginning like this:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
$helloWorld = TRUE;
include_once($path);
?>
And in your index.php include file:
if (helloWorld === TRUE) // or if (!!helloWorld)
echo "Hello world";
if (strpos($_SERVER['SCRIPT_NAME'],'index.php') !== false) {
echo "do stuff";
}
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")
?>
My website's pages are (or will) all be identical, except for the content of the #main div. So I was thinking in the <head> I could do <?php $page = "home.html"?>, where home.html is the bare contents (not a full html page - no <html>, <head> etc.) of the main div for the home page. So then inside <div id="main"> and </div> I could have <?php include($page);?>. Then the reason I'm asking this question is I want to change $page when a link is clicked. How can I do this? Thanks.
You could make the links be like this:
Other Page
Then also in the top of the page where you set page to the value of $_GET['page']
<?php
if (isset($_GET['page']))
{
$page = $_GET['page'] . ".html";
} else {
$page = "home.html";
}
?>
Your link has to be like:
home.php?page=test
You will get the value in your page variable like this:
if(isset($_GET['page']))
$page = $_GET['page'].'.html';
else
$page = 'index.html';
You can do something like:
http://yoursite.com/index.php?page=test.html or http://yoursite.com/index.php?page=test (and append .html later).
For instance:
<?php $page = preg_replace('/[^A-Za-z]/','', $_GET['page']);
$path = 'pages/'.$page.'.html'
if (file_exists($path)){ $output = file_get_contents($path); }
else { header("HTTP/1.0 404 Not Found");
$output = file_get_contents('pages/404.html'); }
?>
(Putting the rest of your file after the php, for the 404 header to work.)
Then, you also can use apache rewrites:
Rewrite Engine On
Rewrite Rule ^([A-Za-z]+)$ index.php?page=$1
Then use links like http://mysite.com/page_name
Then in the main div:
<?php echo $output; ?>