Hello i try this and it works
in index.php :
<?php
define('variable' , 1);
?>
in evry other file :
<?php
defined('variable') or die('Not hacks alloed');
?>
defined
(PHP 4, PHP 5)
defined — 指定した名前の定数が存在するかどうかを調べる
説明
bool defined
( string $name
)
指定した定数が存在し、定義されているかどうかを調べます。
注意: 変数が存在するかどうかを知りたければ、isset() を利用してください。defined() は 定数にしか適用できません。 関数が存在するかどうかを知りたければ、 function_exists() を利用してください。
パラメータ
- name
-
定数名。
返り値
name で指定した名前の定数が定義されている 場合に TRUE、その他の場合にFALSEを返します。
例
例1 定数のチェック
<?php
/* 引用符の使い方に注意してください。これは重要です。この例では
* 文字列 'TEST' が、定数 TEST の名前かどうかを調べています。*/
if (defined('TEST')) {
echo TEST;
}
?>
参考
- define() - 名前を指定して定数を定義する
- constant() - 定数の値を返す
- get_defined_constants() - すべての定数の名前とその値を連想配列として返す
- function_exists() - 指定した関数が定義されている場合に TRUE を返す
- 定数の節
defined
cosmin at yei dot ro
04-Jul-2009 04:33
04-Jul-2009 04:33
tris+php at tfconsulting dot com dot au
27-Mar-2009 02:40
27-Mar-2009 02:40
Before using defined() have a look at the following benchmarks:
true 0.65ms
$true 0.69ms (1)
$config['true'] 0.87ms
TRUE_CONST 1.28ms (2)
true 0.65ms
defined('TRUE_CONST') 2.06ms (3)
defined('UNDEF_CONST') 12.34ms (4)
isset($config['def_key']) 0.91ms (5)
isset($config['undef_key']) 0.79ms
isset($empty_hash[$good_key]) 0.78ms
isset($small_hash[$good_key]) 0.86ms
isset($big_hash[$good_key]) 0.89ms
isset($small_hash[$bad_key]) 0.78ms
isset($big_hash[$bad_key]) 0.80ms
PHP Version 5.2.6, Apache 2.0, Windows XP
Each statement was executed 1000 times and while a 12ms overhead on 1000 calls isn't going to have the end users tearing their hair out, it does throw up some interesting results when comparing to if(true):
1) if($true) was virtually identical
2) if(TRUE_CONST) was almost twice as slow - I guess that the substitution isn't done at compile time (I had to double check this one!)
3) defined() is 3 times slower if the constant exists
4) defined() is 19 TIMES SLOWER if the constant doesn't exist!
5) isset() is remarkably efficient regardless of what you throw at it (great news for anyone implementing array driven event systems - me!)
May want to avoid if(defined('DEBUG'))...
phrank
30-Jan-2009 09:17
30-Jan-2009 09:17
@ ndove at cox dot net
But you receive a Fatal error 'Undefined class constant' if the constant is not defined.:-)
reachmike at hotpop dot com
01-Dec-2008 06:17
01-Dec-2008 06:17
You may find that if you use <?= ?> to dump your constants, and they are not defined, depending on your error reporting level, you may not display an error and, instead, just show the name of the constant. For example:
<?= TEST ?>
...may say TEST instead of an empty string like you might expect. The fix is a function like this:
<?php
function C(&$constant) {
$nPrev1 = error_reporting(E_ALL);
$sPrev2 = ini_set('display_errors', '0');
$sTest = defined($constant) ? 'defined' : 'not defined';
$oTest = (object) error_get_last();
error_reporting($nPrev1);
ini_set('display_errors', $sPrev2);
if ($oTest->message) {
return '';
} else {
return $constant;
}
}
?>
And so now you can do:
<?= C(TEST) ?>
If TEST was assigned with define(), then you'll receive the value. If not, then you'll receive an empty string.
Please post if you can do this in fewer lines of code or do something more optimal than toggling the error handler.
Anonymous
21-Oct-2008 03:36
21-Oct-2008 03:36
Nice one, here's may addition:
index.php:
<?php
// Main stuff here
define('SITE_IN', 1);
include "x.php";
?>
x.php:
<?php
if (!defined('SITE_IN') or !constant('SITE_IN')) die('Direct access not allowed!');
?>
A bit of extra protection, a bit paranoid yes, it's good to be :)
daniel at neville dot tk
14-Jul-2008 05:48
14-Jul-2008 05:48
My preferred way of checking if a constant is set, and if it isn't - setting it (could be used to set defaults in a file, where the user has already had the opportunity to set their own values in another.)
<?php
defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');
?>
Dan.
admin at rune-city dot com
12-Jul-2008 11:39
12-Jul-2008 11:39
If you wish to protect files from direct access I normally use this:
index.php:
<?php
// Main stuff here
define('SITE_IN', 1);
include "x.php";
?>
x.php:
<?php
if (!defined('SITE_IN')) die('Direct access not allowed!');
?>
Shaun H
28-Mar-2008 07:30
28-Mar-2008 07:30
I saw that PHP doesn't have an enum function so I created my own. It's not necessary, but can come in handy from time to time.
<?php
function enum()
{
$args = func_get_args();
foreach($args as $key=>$arg)
{
if(defined($arg))
{
die('Redefinition of defined constant ' . $arg);
}
define($arg, $key);
}
}
enum('ONE','TWO','THREE');
echo ONE, ' ', TWO, ' ', THREE;
?>
Joel
20-Aug-2007 12:35
20-Aug-2007 12:35
If your constants don't show up in your included or required files, then you probably have php safe mode turned on!
I ran into this problem, I forgot to turn of safe mode when I was creating a new site.
ndove at cox dot net
28-Jan-2005 03:20
28-Jan-2005 03:20
In PHP5, you can actually use defined() to see if an object constant has been defined, like so:
<?php
class Generic
{
const WhatAmI = 'Generic';
}
if (defined('Generic::WhatAmI'))
{
echo Generic::WhatAmI;
}
?>
Thought it may be useful to note.
-Nick
Craig at chatspike dot net
30-Nov-2003 07:57
30-Nov-2003 07:57
This can be useful if you want to protect pages which get included from outsiders eyes, on your mail page (the page viewable by people) put define("X", null); then on all your other pages, you can then do something like:
<?php
if (!defined("X")) {
echo "You Cannot Access This Script Directly, Have a Nice Day.";
exit();
}
?>
And your page is a good as protected :)
