Can I make a php file become a package or library?
I have a utility.php file, but I would like to build it become a library, or something else, just don't want people can't access the utility.php directly, but they can access it via other page, for example, a register.php, can use the utility.php, how can 开发者_运维问答I do so? Thank you.
Something like this:
<?php //utility.php
if(!defined('UTILITY_INCLUDED'){
die('Grrrr...');
}
?>
and then like this:
<?php //yourFile.PHP
define('UTILITY_INCLUDED', TRUE);
include ("utility.php";
?>
You have many other options to protect utility.php
- Put it some other directory, and prevent access through .htaccess file
- Use
count(
get_included_files())==1
.. etc
You could include utility.php into any other PHP file. For example:
//Register.php
include('utility.php');
//Use any functions from the utility.php file
utility_function();
精彩评论