INCLUDE_CHECK 在 php 中做什么?

发布于 2024-10-05 04:04:41 字数 99 浏览 0 评论 0原文

你好,我是 php 新手,当我遇到 Define('INCLUDE_CHECK',true); 时我无法理解它的用途是什么?

请。解释。

谢谢! 萨加尔

Hi I am new in php and when I came across define('INCLUDE_CHECK',true); I was not able to understand what it is for?

Pls. explain.

Thanks!
Sagar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-10-12 04:04:41

这是该应用程序稍后将使用的常量。 Define() 用于创建这些常量。通常这用于配置数据。要稍后使用此常量,您只需在代码中使用字符串 INCLUDE_CHECK ,就像它是一个不带 $ 的变量一样。

所以

if(INCLUDE_CHECK)
{
// Do code that only should happen if you want it to.
}

This is a constant that is being used by that application later. Define() is used to create these constants. Typically this is used for configuration data. To use this constant later, you simply use the string INCLUDE_CHECK in code like it were a variable without the $.

so

if(INCLUDE_CHECK)
{
// Do code that only should happen if you want it to.
}
南汐寒笙箫 2024-10-12 04:04:41

只是对 DampeS8N 答案的补充,这是正确的。

我阅读了 php.net 的链接 https://www.php.net/manual/ en/function.define.php
下面的示例清楚地表明,可以在定义函数内部使用任何值来将变量定义为常量,并且 INCLUDE_CHECK 只是一个变量,我认为它是某种 php 功能;)

<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.

define("GREETING", "Hello you.", true);
echo GREETING; // outputs "Hello you."
echo Greeting; // outputs "Hello you."

?>

@DampeS8N 请。如有错误请评论!

感谢大家的回答!

Just an addition to DampeS8N Answer which is correct one.

I read php.net's link https://www.php.net/manual/en/function.define.php
and below example makes it clear that any value can be used inside define function for defining variable as constant and INCLUDE_CHECK was just a variable which I thought as some kind of php feature ;)

<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.

define("GREETING", "Hello you.", true);
echo GREETING; // outputs "Hello you."
echo Greeting; // outputs "Hello you."

?>

@DampeS8N pls. comment if wrong!

Thanks to all of you for your answers!

梦言归人 2024-10-12 04:04:41

INCLUDE_CHECK 不是 PHP 本身的特性,只是某人用 PHP 编写的一些代码的特性。 Google 上的第一个点击向我们展示了用户试图通过检查变量是否已定义来保护的配置文件。

我在谷歌上找到的狙击中采取的方法就像这样

  • 一个可以直接引用的文件(例如 http:// example.com/foo.php) 将定义 INCLUDE_CHECK,然后包含一个检查 INCLUDE_CHECK 的文件,例如 config.php;因为 foo.php 已经定义了它,所以
  • 如果用户直接访问 config.php (例如 http:// /example.com/config.php) 那么 INCLUDE_CHECK 将不会被定义,因此会发生错误,阻止用户运行脚本。

以下示例应该使这一点更清楚

foo.php

<?php
defined('INCLUDE_CHECK', true);
require_once 'config.php';
?>

config.php

<?php
if (!defined('INCLUDE_CHECK')) { die ('invalid access'); }
echo 'Hello World!';
?>

如果用户直接访问 config.php,则未定义 INCLUDE_CHECK,因此脚本将因无效访问消息而终止。如果用户访问 foo.php,它将定义 INCLUDE_CHECK 然后包含 config.php,然后它将回显 Hello World。

顺便说一句,这是一种非常糟糕的保护配置文件的方法;请参阅我的帖子以获得更简单、更可靠的方法。

INCLUDE_CHECK is not a feature of PHP itself, only of some code that someone has written in PHP. The first hit on google shows us a configuration file that a user is attempting to protect by checking if the variable is defined.

The approach taken in the sniped I found on google is like this

  • a file which can be referenced directly (e.g. http://example.com/foo.php) will define INCLUDE_CHECK and then include a file that checks INCLUDE_CHECK, e.g. config.php; because foo.php has already defined it, this will work correctly
  • if a user accesses config.php directly (e.g. http://example.com/config.php) then INCLUDE_CHECK will not have been defined, so an error will occur, preventing the user from running the script.

The following examples should make this clearer

foo.php

<?php
defined('INCLUDE_CHECK', true);
require_once 'config.php';
?>

config.php

<?php
if (!defined('INCLUDE_CHECK')) { die ('invalid access'); }
echo 'Hello World!';
?>

If a user accesses config.php directly, then INCLUDE_CHECK is not defined, so the script will die with the message invalid access. If a user accesses foo.php, it will define INCLUDE_CHECK then include config.php, which will then echo Hello World.

This is a pretty crap way of protecting a configuration file, btw; see my post here for a more simple and reliable approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文