How to make page blank when user access php file directly? - php

So I'm trying to make a theme from my Html file. And using require('file.php') where file.php is a series of different components of my theme. Now when I access the file directly, I still see the html. How do I make it display a blank page when user accesses the file.php directly?
Explanation
So let's say I make index.php and I want to include the header file(header.php)
When I require('header.php'), everything works perfect. Now when I try to access the header.php, I can see the html content of it. How to I make this appear blank instead of seeing the header.php piece?
Thanks

In that case, If you want to execute header.php inside the index.php or etc. You need to add or define a flag in parent files (wherever you want header.php to be executed or simply adding in to the common file which is called in all parent files), In header.php file you need check the defined flag is set or has some value. If it is not set, then stop execution by using die(); function.
In index.php,
<?php $HeaderAllow ='1'; ?>
In header file,
<?php
if($HeaderAllow=='' or !isset($HeaderAllow)){
die();
}
?>

you can use $_SERVER['SCRIPT_FILENAME'] here to check and then make page blank in header.php.
<?php
if($_SERVER['SCRIPT_FILENAME'] == 'header.php') {
exit();
}

Putting the include files in a seperate folder and protectinng with .htaccess is good idea.
Same issue here.
deny direct access to a folder and file by htaccess

The easiest way is to put your includes in a directory and deny access to that directory in your .htaccess file

Related

Maintain central header in deeper directories

On my site within /public_html I have a file called /public_html/header.php. This file is called on every page using;
<?php echo file_get_contents("./header.php"); ?>
which works for any pages within the /public_html directory.
Same goes for the index page of the blog at /public_html/blog/ and its index page. It just uses;
<?php echo file_get_contents("../header.php"); ?>
instead. The issue arises when including the header within the articles subdirectory at /public_html/blog/articles/ariclename.php. I'd tried;
<?php echo file_get_contents(".../header.php"); ?>
but that doesn't work. I've also tried different ./././, ../../../ combinations but can't seem to get it to work.
Any ideas would be greatly appreciated. Thanks in advance :)
You can use a constant to set your path globally across all your pages.
This is how you would do that.
Create a file to configure the constant with the path to the header.
config.php
<?php
define('MY_HEADER', 'full/path/to/header.php');
?>
Generally you want to include the config.php in a file that gets called every time your script loads so that it always gets called.
In a file that gets called every time your script loads you would require your config.php file like so.
require 'path/to/config.php';
Then on any page where you need to use header.php all you need to do to get the header is this:
<?php echo file_get_contents(MY_HEADER); ?>

header.php include only working on home (index.html) file

I am trying to add an include of "header.php" to my inside pages BUT my PHP syntax is showing up as a comment in chrome's editor. The SAME "header.php" works fine on my home/ "index.html" page but will not on any other. Some points to note:
The route of "index.html" and "inside-page-example.html" are the same, so it's not a route problem.
The code is not written as a comment in the file. It reads:
<?php include ('inc/header.php'); ?>
So frustrating! :(
instead of
<!--?php ... ?-->
Try:
<?php ... ?>
and also change your file extension to .php (everywhere you are using php)
You do need to take out the comments, so that it's just <?php and ?>. The green text in Chrome indicates that it is being read as a comment.
You'll also need to save the file as .html with filetype as "Hypertext Markup Language" not just .txt, but I have a feeling you have that covered.
Also, you'll need to make sure that every file is in the same folder. If "inside-page-example.php" is inside a different folder, then you will need to add a forward slash or /../ for every folder you want to go up. So if you have header.php and index.php and a folder called Resume all inside "Home" folder, then when you are on Resume/index.php, you'll need to say include_once("/header.php");. A really great tool that I use when using includes is to replace the slashes with $_SERVER[DOCUMENT_ROOT]. So the include would read like this: include_once("$_SERVER[DOCUMENT_ROOT]/header.php");. This will grab header.php from the folder at the root of the server. Does that make more sense?

Combining 2 PHP scripts

I have 2 web pages. Both have PHP scripts. Playing.php has a table showing the last 20 songs played on my shoutcast server. I'm trying to get that displayed on my index page.
Index.html: http://www.deamon.org
The page I'm trying to add is
http://www.deamon.org/scxml/playing.php
Can I do this with include function and echo or is there a better way.
The quickest and easiest way is to use an iframe:
<iframe src="http://www.deamon.org/scxml/playing.php"></iframe>
My best suggestion would simply be to include it with a simple include statement or using an iframe
<?php include("link to file"); ?>
<iframe src="link to file"></iframe>
You could use a PHP include you can do this by adding this line of code into the index file.
First you would have rename index.html to index.php and then place the following code in-between the tags where you would like the page displayed.
<?php include '/scxml/playing.php'; ?>
My suggestion would be to include the .php file into your index page. However including PHP code into an HTML type page requires reconfiguring your server. Instead, do the following:
Change your index.html file to an index.php file. It will be read and rendered the same way without requiring any changes. (make sure that the index.html file doesn't exist on the server so that the index.php file automatically gets picked up
add the following line of code into your index.php file:
<?php include('path/to/playing.php'); ?>

included files php do not appear in a browser

I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.

Is it possible to use .htaccess to make custom Index Of pages? If so how?

You know how when you go to a url on a server and the directory doesn't have an index.* file or a default.* file it shows you a list of the directorie's contents? I was wondering if there is any way to customize the way that index looks or theme it to fit your site. For instance I'd want to add the php
<?
include 'template.php';
head();
?>
Before the listing. And
<?php
foot();
?>
After. Can this be done?
I have never done this before, but this shows how to do it: http://www.webmasterworld.com/apache/3589651.htm via .htaccess.
Edit: Maybe this (snapshot) shows exactly what you want, it's a walk-through of how to embed header and footer in the listing page.
Sure, you can add your own DirectoryIndex file which can even be PHP.
In you .htaccess file:
DirectoryIndex my-dir-listing.php
Do inside the my-dir-listing.php file whatever you need to do the directory listing.
If you want to modify the header of the existing listing, take a look into the HeaderName directive.

Categories