php CodeIgniter 基础路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26990196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:08:03  来源:igfitidea点击:

CodeIgniter BASEPATH

phpcodeigniter

提问by John

Hi I am new to Codeigniter. From my understanding of the BASEPATH, it is used to activate the Codeigniter framework. The code

嗨,我是 Codeigniter 的新手。根据我对 的理解BASEPATH,它是用来激活 Codeigniter 框架的。编码

defined('BASEPATH') OR exit('No direct script access allowed');

is used at top of the page to prevent direct access to the controller. But it is not working in my code. When I try to directly access a view via controller, the view gets loaded. I have checked index.phpand BASEPATHis defined there. Please advice.

用于页面顶部以防止直接访问控制器。但它在我的代码中不起作用。当我尝试通过控制器直接访问视图时,视图被加载。我已经检查index.phpBASEPATH在那里定义。请指教。

回答by Rwd

defined('BASEPATH') OR exit('No direct script access allowed');is used to make sure that the request has gone through index.phpin your root dir. This is for reasons such as making sure that all Codeigniter base classes are being loaded and making sure certain vars have been set etc.

defined('BASEPATH') OR exit('No direct script access allowed');用于确保请求已index.php在您的根目录中通过。这是出于诸如确保正在加载所有 Codeigniter 基类并确保已设置某些变量等原因。

So, yes you will be able to access a view file if you're going through a controller.

所以,是的,如果您通过控制器访问视图文件。

Hope this helps!

希望这可以帮助!

回答by Rwd

defined('BASEPATH') OR exit('No direct script access allowed');
  • defined: Checks whether a given named constant exists
  • BASEPATH: it′s a constant from codeigniter reserved names
  • OR exitprints the string: 'No direct script access allowed'
  • exit— Output a message and terminate the current script.
  • defined: 检查给定的命名常量是否存在
  • BASEPATH:它是来自 codeigniter保留名称的常量
  • OR exit打印字符串: 'No direct script access allowed'
  • exit— 输出消息并终止当前脚本。

Other reference: PHP 5 Constants

其他参考:PHP 5 常量

回答by Seyfemichael Teklemariam

User accesses the framework through a controller (http://example.com/index.php/controllername). The respective controller is activated through the URL parameters. As PHP assumes that classes within the framework are already declared, this line assures that the framework is properly booted up and the server is not running a single PHP file. Not using this can be a potential security breach, and if the framework is not booted up, the controller file would run on its own and fail.

用户通过控制器 ( http://example.com/index.php/controllername)访问框架。相应的控制器通过 URL 参数激活。由于 PHP 假定框架内的类已经声明,因此该行确保框架正确启动并且服务器没有运行单个 PHP 文件。不使用它可能是一个潜在的安全漏洞,如果框架没有启动,控制器文件将自行运行并失败。

Details of the statement.

声明的详细信息。

  1. defined('BASEPATH') OR exit('No direct script access allowed');The function defined() checks if the constant exists
  2. BASEPATH is a constant defined in Codeigniter reserved names, which contains the path to the system folder.
  3. exit() function just prints the string and terminates the script execution.
  1. defined('BASEPATH') OR exit('No direct script access allowed');函数defined()检查常量是否存在
  2. BASEPATH 是在 Codeigniter 保留名称中定义的常量,其中包含系统文件夹的路径。
  3. exit() 函数只是打印字符串并终止脚本执行。

source-->https://www.quora.com/profile/Gursharan-Singh-Dhanjal

来源--> https://www.quora.com/profile/Gursharan-Singh-Dhanjal

回答by Akshay Kumar

Actually codeigniter won't allow any its files direct accessible All the request are processed by index.php which is present in root folde of code igniter. whenever we request any codeigniter url index.php process it and define a constant . defined('BASEPATH') OR exit('No direct script access allowed');check whether constant defined or not if not then php script exit and won't process further.

实际上,codeigniter 不允许直接访问它的任何文件。所有请求都由 index.php 处理,它存在于 code igniter 的根文件夹中。每当我们请求任何 codeigniter url index.php 处理它并定义一个常量。defined('BASEPATH') OR exit('No direct script access allowed');检查是否定义了常量,如果没有,则 php 脚本退出并且不会进一步处理。

if you call any view file from controller then it work and not exit your script because your request already processed by index.php when you hit any controller. if you try to access direct view file then it won't work and php script exit

如果您从控制器调用任何视图文件,那么它会工作并且不会退出您的脚本,因为当您点击任何控制器时,您的请求已经由 index.php 处理。如果您尝试访问直接查看文件,则它将无法工作并且 php 脚本退出

回答by Ronow

defined('BASEPATH') OR exit('No direct script access allowed');does the following:

defined('BASEPATH') OR exit('No direct script access allowed');执行以下操作:

  1. defined('BASEPATH')=> Checks whether CodeIgniter constant called BASEPATHis defined. If it is defined, it returns TRUEand nothing happens further. If it is not defined, it returns FALSEand the preceding code which is exit('...');is thereby executed with the information 'No direct script allowed'.
  1. defined('BASEPATH')=> 检查是否BASEPATH定义了调用的 CodeIgniter 常量。如果它被定义,它会返回TRUE并且不会再发生任何事情。如果它没有被定义,它会返回FALSE并且前面的代码exit('...');因此与信息一起被执行'No direct script allowed'

When you say it is not working on your code. Which code are you referring to? Can you share the said code? View files are usually loaded by the controllers.

当您说它不适用于您的代码时。你指的是哪个代码?你能分享一下上面的代码吗?视图文件通常由控制器加载。

回答by Zeeshan Ahmad

Use this code before Class start, for example:

在类开始之前使用此代码,例如:

defined('BASEPATH') OR exit('No direct script access allowed');

class Dataentry extends CI_Controller { }