How to hide header and footer.php - php

Hi guys i m building a website in php i created my first 3 page
-header
-index
-footer
in index.php i use:
<?php include("header.php"); ?>
<?php include("footer.php"); ?>
My question is:
if i go to my url http://localhost:8888/project/index.php
every its'ok
but if i write http://localhost:8888/project/header.php or footer
How i can not display the content it s in this two pages?
Because if i m using a redirect i have a blank page in index.php because the index.php include header and footer
Thanks a lot

Define a constant in your scripts before including header or footer:
index.php:
define('MY_CONSTANT', 1);
include 'header.php';
//
include 'footer.php';
Then, in header.php and footer.php check if constant is not defined:
if(!defined('MY_CONSTANT')) {
// You can show a message
die('Access not allowed!');
// Or send user to index.php, first delete above line (die)
header('Location:index.php');
exit; // This line is needed to stop script execution
}
// rest of header and footer code

go to your footer and header scripts, and make then a variable, for example:
$header = "<!doctype html><html><head>#head Content</head>";
and replace every qoutes with \" (look at the div class in $footer)
$footer = "<footer><div class=\"class\">#footer Content</div></footer></html>";
then in the index.php make it echo the variables
<?php
require_once("header.php");
require_once("footer.php");
echo $header;
echo $body;
echo $footer;
?>
then you can add this code to make it look like the file doesn't exist
<?php
header('HTTP/1.0 404 Not Found');
readfile('www.link.to/error-page/404');
exit();
?>
Note: it's all up to you how you make your website, but this is the method I use, and it works fine.
extra:
you can make your $body like this
let's say you have a folder call scripts, you create a file called pager.php
and you add the code
<?php
$dir = $_SERVER['PATH_INFO'];
if(empty($dir)) {
require_once("templates/mainpage.php");
} elseif ($dir == "page2") {
require_once("templates/page2.php");
} else {
require_once("templates/pages-doesnt-exist.php");
}
?>
remember to make a folder called "templates" and add all your pages in there,
then you'll be able to access all page from http://example.com/index.php/{pagename}
you can also add this code to your .htaccess
RewriteEngine On
RewriteRule ^page2$ /index.php/page2 [L] --page wise condition
so http://example.com/page2 will access http://example.com/index.php/page2

Compare $_SERVER['REQUEST_URI'] to make sure the requested page/file is neither header.php nor footer.php

Related

Including CSS file depending on php include variable

I am trying to include a specific css file depending on the URL provided by the browser.
My website has the following format:
Header information
php include for content
Footer information
I am doing this so when I change my header menu links, I don't have to change every page on the website.
The php include code is as follows:
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page)) {
if(in_array($page,$pages)) {
$page .= '.php';
include($page);
}
else {
include('404.php');
}
}
else {
include('main.php');
}
?>
The URL is made up as follows
index.php?page= for example index.php?page=contact
As part of the php include code, when the page=???? element isn't within the array it loads page 404.php which is an error page.
As can be seen, the array currently contains two pages, main (the home page content) and contact (the contact form). If a user tries to load a page called test (index.php?page=test) my webpage will display the 404 page (however doesn't load index.php?page=404 - the URL in browser stays as index.php?page=test.
What I'm trying to achieve is to load 404.css file when page=test(or any other page name not included in the array) is loaded.
Does anyone know how i'd go about achieving this? I've tried writing an if statement in the header to load 404.css however I was using strstr that fetches the browser URL (so test, not 404), therefore the correct css file doesn't get loaded.
Any help is much appreciated (or a better way to achieve what i'm trying to do).
Thanks in advance
Iain
An easy way of doing this is to make use of the ob_* php functions like bellow:
PHP get all the information from within the ob_start and the ob_get_clean functions and concatenate them all together as a single variable $_page_content, that variable can be used anywhere you need.
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page))
{
if(in_array($page, $pages))
{
ob_start('ob_gzhandler');
?>
<link rel="stylesheet" href="css/<?=$page?>.css" type="text/css">
<?
include($page . '.php');
$_page_content = ob_get_clean();
echo $_page_content;
}
else
{
include('404.php');
}
}
else
{
include('main.php');
}
?>

Security Issue on Apache/PHP, root and external folders can be accesed

I am learning PHP and Apache configurations, i am using XAMPP Version: 5.6.19. I have a page named default.php, this page will load a header, a footer and a content subpage between them, this content subpage will be saved on a folder named "content". So to load the site showing the content subpage i would write:
localhost/misite/default.php?page=subpage
to do so i have coded the default.php the following way:
<!--INCLUDE HEADER-->
<?php include 'includes/header.php';?>
<!--LOAD CONTET-->
<?php
/*if page is not set, redirect to index*/
if(!isset($_GET['page']) || strlen($_GET['page']) == 0)
{
header("Location: index.php");
die();
}
/*if page is set*/
else
{
/*search page on contet directory*/
$page = 'content/'.$_GET['page'].'.php';
/*if page not found on content directory, redirect to index*/
if(!glob($page))
{
header("Location: index.php");
die();
}
/*if page is found, load as subpage*/
else
{
?>
<main class = "<?php echo basename($page, '.php'); ?>">
<?php include $page;?>
</main>
<?php
}
}
?>
<!--INCLUDE FOOTER-->
<?php include 'includes/footer.php';?>
I coded it this way in order to only allow load of php files of content folder and most important to prevent access from there to another folder by doing the ../ trick, so if someone try that the result would be:
default?page=../.php
and that will produce a redirect to index page instead.
The problem emerged when i tried to have the same functionality with SEF URL's. I coded the following on the .htaccess:
RewriteEngine On
RewriteBase /misite/
RewriteRule ^([a-zA-Z0-9-]*)$ default.php?page=$1
So i could be able to write
localhost/misite/subpage
So i was expecting the same result as the parameter $1 would be now the value for the "page" parameter on default.php and expected that everything else would be exaclty the same, so i didn't expect to be able to access another folder. But when i tried the following:
localhost/../
As result i got to the dashboard page (localhost/dashboard/) which is a page located outside of mi site folder, and if i try
localhost/..img/ i can access the img folder and so on. It looks like it is not even executing my default.php conditional validations.
I am worried if this could be a security issue and worried if i could get hacked by this. And i don't know why this happens. In case this is a security issue, how can i fix it?
The security risk is with this line:
$page = 'content/'.$_GET['page'].'.php';
Without checking, you're allowing any input to search your disk with the subsequent call to glob. Relative paths will be evaluated, letting the user reference any PHP file on your server.
If all of your PHP files are within 'content' and not in deeper directories, one solution is
// Check "page" only contains letters, numbers, underscore
if (!preg_match('/^\w+$/', $_GET['page'])) {
header("HTTP/1.0 404 Not Found");
exit();
}
$page = 'content/'.$_GET['page'].'.php';
Then you can be confident the path in $page is only referencing a PHP file in your content directory.

How can I make php pages appear inside a div?

How can I make PHP pages appear inside a div called contenct, whenever a menu button is pressed?
For example I should bring product menu product.php page to the document body and head, footer will continue well without causing alteration.
I've tried include, but how could it put the link from appearing on the container div?
I hope I have understood!
leave a sample image:
You can use PHP's include() for that purpose.
An example:
<?php
include('head.php');
include('menu.php');
//your normal body code goes here
include('footer.php');
?>
include('file.php'); will read the contents of the php file and it will add / paste the contents in the main file. In this particular example, it will load the contents of head.php, menu.php, and footer.php.
Hope this helps!
Based on your question, im assuming you would like to page to NOT refresh when the navigation button is pressed, and to simply load the contents of a php file into the content area.
The smart way to do this would be to look into using jquery with AJAX, or LOAD to load external content as needed.
PHP has no direct way to manipulate the clients browser without refreshing or loading another page.
if you must do this in php, a very simple approach would be to have each menu item link to a query string. I.E.
HTML: menu.php
Home
Products
Clients
Contact
and to have the index.php file itself run a switch and case I.E.
PHP: index.php
<?php
if(isset($_GET['page']) && $_GET['page'] != '' ){
$page = $_GET['page']; // page being requested
}else{
$page = 'home'; // default page
}
include('head.php');
include('menu.php');
// Dynamic page based on query string
include($page.'.php');
include('footer.php');
?>
This is a very basic example, but might get you pointed in the right direction.
hope this helps
In the php file the user is viewing, put:
include('home.php');
this will execute the contents of the home.php file in the current php file
You could use AJAX and on the event's finish use result $('#content').empty().append(result)
I'd like to add AMAL's answer...
<?php
require('function.php');
include('head.php');
include('menu.php');
//Create a function here to fetch pages
echo '<div id="content">';
echo get_page();
echo '</div>';
include('footer.php');
?>
Then create a function page and add a function that parse the url and gets the php page. (quickly made function, but should do the trick, or help)
function get_page()
{
//http://example.com?page=home
$page = $_GET("page");
$directory = "pages";
if($page){
//security check (anti-hack)
if (!strpos($page,".")&&!strpos($page,"/")&&!strpos($page,"\\")&&!strpos($page,";")){
$path= $directory."/".$page.".php";
if (file_exists($path)){
require ($path);
}else{
echo "<p>Sorry, but that page does not exist.</p>";
}
}else{
echo "<p>Sorry, but those characters aren't allowed !</p>";
}
}else{
$path = $directory."/home.php";
require ($path);
}
}
Then your php page should look like this:
<p>This is the home page</p>
Add this piece of code in the content area of your content area in main file...
<li>BLOG</li>
<div class="container content">
<?php
if(isset($_GET['page']))
{
$page_name = $_GET['page'];
include("/".$page_name);
}
?>
</div>

hide div in a certain html page

apology for this newbie question.
I have created an html page (dash.html) that uses the same header as the other pages.
it calls this PHP function <?php include 'header.php'; ?>
the dash.html contains a special <div> made specially for that page; and it must be placed inside the header.php
im trying to figure out how to enable/disable a certain div on a certain html page.
will it require a PHP conditional statement?
Yes. The easiest way is to include a conditional line in the header, and pass the checked variable from each page that calls header. So, in your dash.php (it can't be a .html if it calls php, can it?):
<?php
$includediv = true; // set to false, or leave out, if you don't want the div
include('header.php');
?>
and in the header.php:
<?php
...some other code...
if($includediv){
...code to include div...
}
?>
This will continue to work as before for all other pages that call header.php.
You are trying to show the div only if header.php is present, right?
So, just set a variable inside header.php and use a conditional inside the HTML page.
Try this code
<?php
$path=explode('/',$_SERVER['REQUEST_URI']);
$page=end($path);
if($page=='dash.php')
{
?>
<div>your div for dash.php page</div>
<?php } ?>

PHP how to open page1, redirect to page2 and show page1 in a div dom?

I have many sub pages, there urls like www.domain.com/sub/page1.php www.domain.com/sub/page2.php... Now when I type there url in browser. they all redirect to www.domain.com/sub/index.php, and the current sub page will show in a div dom in the index.php.
My knowledge is very limited. I only know jqeury.load and php header Location. but it is difficult redirect to index.php then tell index.php, which is the current sub-pages then do a jqeury.load.
One more note: Also should think SEO. maybe jqeury.load is bad for a search spider. maybe should use hash url.
So I ask for a help. is there anybody could give me some good suggestion? or simple worked examples?
Thanks.
In every PHP file (e.g page1.php) use this code:
<?php
if(!defined('INCLUDE')) {
header('Location: index.php?site=' . $_SERVER['PHP_SELF']); //Send the user to the index.php page
die();
}
?>
Insert the content you want here...
And in the index.php file:
<?php
define('INCLUDE', true);
if(isset($_GET['site'])) {
$site = $_GET['site'];
} else {
$site = 'page100.php'; //Put the name of the default page if the user visits index.php here
}
?>
<html>
<head><!-- HEAD CONTENT HERE --></head>
<body>
<div><?php
include($site); //Put the page requested into the div
?></div>
</body>
</html>

Categories