本章我们将了解CakePHP中的环境变量、常规配置、数据库配置和邮件配置。
配置 CakePHP 默认自带一个配置文件,我们可以根据自己的需要进行修改。为此,有一个专用文件夹 “config”。 CakePHP 具有不同的配置选项。
让我们从了解 CakePHP 中的环境变量开始。
环境变量
环境变量使您的应用程序在不同环境下的工作变得容易。例如,在开发服务器、测试服务器、登台服务器和生产服务器环境上。对于所有这些环境,您可以使用 env() 函数 读取所需环境的配置并构建您的应用程序。
在您的 config 文件夹中,您将看到 config/.env.example。该文件包含将根据您的环境进行更改的所有变量。首先,您可以在 config 文件夹中创建一个文件,即 config/.env 并定义这些变量并使用它们。如果您需要任何其他变量,可以将其放入该文件中。
您可以使用 env() 函数读取环境变量,如下所示 -
示例
$debug = env('APP_DEBUG', false);
第一个是您想要的环境变量的名称,第二个值是默认值。如果没有找到环境变量的值,则使用默认值。
常规配置
下表描述了各种变量的作用以及它们如何影响您的 CakePHP 应用程序。
先生No | 变量名称和描述 | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 |
|
||||||||||||||||||||||||||||||
2 | App.namespace 用于在其下查找应用程序类的命名空间。 | ||||||||||||||||||||||||||||||
3 | App.baseUrl 如果您不打算将 Apache 的 mod_rewrite 与 CakePHP 一起使用,请取消注释此定义。不要忘记也删除您的 .htaccess 文件。 | ||||||||||||||||||||||||||||||
4 | App.base 应用程序所在的基本目录。如果为 false,则会自动检测到。 | ||||||||||||||||||||||||||||||
5 | App.encoding 定义您的应用程序使用的编码。此编码用于生成布局中的字符集并对实体进行编码。它应该与为您的数据库指定的编码值匹配。 | ||||||||||||||||||||||||||||||
6 | App.webroot webroot 目录。 | ||||||||||||||||||||||||||||||
7 | App.wwwRoot webroot 的文件路径。 | ||||||||||||||||||||||||||||||
8 | App.fullBaseUrl 应用程序根目录的完全限定域名(包括协议)。 | ||||||||||||||||||||||||||||||
9 | App.imageBaseUrl webroot 下公共图像目录的 Web 路径。 | ||||||||||||||||||||||||||||||
10 | App.cssBaseUrl webroot 下公共 css 目录的 Web 路径。 | ||||||||||||||||||||||||||||||
11 | App.jsBaseUrl webroot下公共js目录的Web路径。 | ||||||||||||||||||||||||||||||
12 | App.paths 配置非基于类的资源的路径。支持插件、模板、语言环境、子项,它们允许分别定义插件、视图模板和语言环境文件的路径。 | ||||||||||||||||||||||||||||||
13 | Security.salt 用于散列的随机字符串。在进行对称加密时,该值也用作 HMAC salt。 | ||||||||||||||||||||||||||||||
14 | 资产.时间戳
使用正确的帮助程序时,在资产文件 URL(CSS、JavaScript、图像)末尾附加一个时间戳,即特定文件的最后修改时间。有效值为 -
|
Host | The database server’s hostname (or IP address). |
---|---|
username | Database username |
password | Database password. |
database | Name of Database. |
Port | The TCP port or Unix socket used to connect to the server. |
config/app.php
'Datasources' => [ 'default' => [ 'className' => Connection::class, 'driver' => Mysql::class, 'persistent' => false, 'timezone' => 'UTC', //'encoding' => 'utf8mb4', 'flags' => [], 'cacheMetadata' => true, 'log' => false, 'quoteIdentifiers' => false, //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'], ], ]
Let us understand each parameter in detail in config/app.php.
Sr.No | Key & Description |
---|---|
1 |
className The fully namespaced class name of the class that represents the connection to a database server. This class is responsible for loading the database driver, providing SQL transaction mechanisms and preparing SQL statements among other things. |
2 |
driver The class name of the driver used to implement all specificities for a database engine. This can either be a short classname using plugin syntax, a fully namespaced name, or a constructed driver instance. Examples of short classnames are Mysql, Sqlite, Postgres, and Sqlserver. |
3 |
persistent Whether or not to use a persistent connection to the database. |
4 |
encoding Indicates the character set to use, when sending SQL statements to the server like ‘utf8’ etc. |
5 |
timezone Server timezone to set. |
6 |
init A list of queries that should be sent to the database server as and when the connection is created. |
7 | log
log Set to true to enable query logging. When enabled queries will be logged at a debug level with the queriesLog scope. |
8 |
quoteIdentifiers Set to true, if you are using reserved words or special characters in your table or column names. Enabling this setting will result in queries built using the Query Builder having identifiers quoted when creating SQL. It decreases performance. |
9 |
flags An associative array of PDO constants that should be passed to the underlying PDO instance. |
10 |
cacheMetadata Either boolean true, or a string containing the cache configuration to store meta data in. Having metadata caching disable is not advised and can result in very poor performance. |
Email Configuration
Email can be configured in file config/app.php. It is not required to define email configuration in config/app.php. Email can be used without it. Just use the respective methods to set all configurations separately or load an array of configs. Configuration for Email defaults is created using config() and configTransport().
Email Configuration Transport
By defining transports separately from delivery profiles, you can easily re-use transport configuration across multiple profiles. You can specify multiple configurations for production, development and testing. Each transport needs a className. Valid options are as follows −
Mail − Send using PHP mail function
Smtp − Send using SMTP
Debug − Do not send the email, just return the result
You can add custom transports (or override existing transports) by adding the appropriate file to src/Mailer/Transport. Transports should be named YourTransport.php, where 'Your' is the name of the transport.
Following is the example of Email configuration transport.
'EmailTransport' => [ 'default' => [ 'className' => 'Mail', // The following keys are used in SMTP transports 'host' => 'localhost', 'port' => 25, 'timeout' => 30, 'username' => 'user', 'password' => 'secret', 'client' => null, 'tls' => null, 'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null), ], ],
Email Delivery Profiles
Delivery profiles allow you to predefine various properties about email messages from your application, and give the settings a name. This saves duplication across your application and makes maintenance and development easier. Each profile accepts a number of keys.
Following is an example of Email delivery profiles.
'Email' => [ 'default' => [ 'transport' => 'default', 'from' => 'you@localhost', ], ],
以上是CakePHP 项目配置的详细内容。更多信息请关注PHP中文网其他相关文章!

防止会话固定攻击的有效方法包括:1.在用户登录后重新生成会话ID;2.使用安全的会话ID生成算法;3.实施会话超时机制;4.使用HTTPS加密会话数据,这些措施能确保应用在面对会话固定攻击时坚不可摧。

实现无会话身份验证可以通过使用JSONWebTokens(JWT)来实现,这是一种基于令牌的认证系统,所有的必要信息都存储在令牌中,无需服务器端会话存储。1)使用JWT生成和验证令牌,2)确保使用HTTPS防止令牌被截获,3)在客户端安全存储令牌,4)在服务器端验证令牌以防篡改,5)实现令牌撤销机制,如使用短期访问令牌和长期刷新令牌。

PHP会话的安全风险主要包括会话劫持、会话固定、会话预测和会话中毒。1.会话劫持可以通过使用HTTPS和保护cookie来防范。2.会话固定可以通过在用户登录前重新生成会话ID来避免。3.会话预测需要确保会话ID的随机性和不可预测性。4.会话中毒可以通过对会话数据进行验证和过滤来预防。

销毁PHP会话需要先启动会话,然后清除数据并销毁会话文件。1.使用session_start()启动会话。2.用session_unset()清除会话数据。3.最后用session_destroy()销毁会话文件,确保数据安全和资源释放。

如何改变PHP的默认会话保存路径?可以通过以下步骤实现:在PHP脚本中使用session_save_path('/var/www/sessions');session_start();设置会话保存路径。在php.ini文件中设置session.save_path="/var/www/sessions"来全局改变会话保存路径。使用Memcached或Redis存储会话数据,如ini_set('session.save_handler','memcached');ini_set(

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然后使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

在PHP会话中可以存储数组。1.启动会话,使用session_start()。2.创建数组并存储在$_SESSION中。3.通过$_SESSION检索数组。4.优化会话数据以提升性能。

PHP会话垃圾回收通过概率机制触发,清理过期会话数据。1)配置文件中设置触发概率和会话生命周期;2)可使用cron任务优化高负载应用;3)需平衡垃圾回收频率与性能,避免数据丢失。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1
好用且免费的代码编辑器