I have 3 files: header.php, footer.php and index.php.
The header.php and footer.php files are integrated in the index.html file. It works good. But I have a problem. What must I do so users will not be able to see separately the header.php and footer.php files. I want to be seen only in the index.php file.
When I wrote in the URL bar: mydomain.com/header.php I don't want to see it.
What can I do?
You can do it like Joomla!
Joomla defines a variable in their index.php. Joomla uses "JEXEC". In the other files its just checking whether the variable is defined or its not. This means when you defined this variable in your index before including your header.php, it will display its context, otherwise you can display (like Joomla!) "restricted Access" or just return 404-Header and load your 404-Document.
Another Option is to use "#" before the Filename. No browser can open that file directly, since it handle it as an anchor. So just rename it to #header.php and include it like this and no browser can open that file. Not sure about things like curl.
I see two options
You should use the .htaccess file. With it, you can restrict access to certain files by user, authentication, IP Address, or just restrict access permanantley. Use it like
AuthUserFile /home/header.php
AuthGroupFile /dev/null
AuthName "My Protected Directory"
AuthType Basic
<Limit GET POST PUT>usershere</Limit>
More info is available in the link above and by searching around the internet, it's a fairly common question.
Take all of your commonly used php and put it into one file called common.php. Use require("common.php");
in the starts of your other files, and then have common.php be a bunch of functions.
common.php
<?php
function header() {
<!DOCTYPE html>
<html>
<head></head>
<body>
}
function footer() {
</body>
</html>
}
?>
mypage.php
<?php
require("common.php");
header();
//my code here for this page
footer();
?>
common.php won't have any output, so people can call it and nothing will happen, or you can have common.php be a redirect to your main page like
<?php
header("Location: index.php");
die();
//functions
Separating out comments and opinions from the answers above, I like these two options because you don't need extra modules or plugins, they're simple, and you can decide which is best for your use case. If you want a redirect to another page when a user tries to view the common.php, I would suggest the second option and my personal favorite. The first option throws up a server error saying access was denied, which you can have if you really want, but won't redirect anyone anywhere. I tend to use the second option most when coding.
In the end, both options are great and it's up to you.
There are many solutions, some are already mentioned. How I do it, is that I define a variable in my file, and set it to true. You can then use the code below to see if it's been set, and if it has, display the page. This means that it can only be viewed via an include, where that variable is set before including you header.
// In your code, you can put this
$allowInclude = true;
include "header.php";
And then in your header
if (isset($allowInclude) && $allowInclude) {
//Header here
} else {
// do something
}
As I said, there are many ways, depending on what you really want.
(Sorry for any and all poor formatting, writing this on mobile)