PHP和MySQL连接方式
PHP and MySQL connection method
我有一个名为 db_conn.php
的 .php
文件,它建立了到数据库的连接。这是文件的代码。
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: ");
}
else{
}
?>
当我需要从其他文件(例如 login.php
文件)执行一些数据库操作时,我所做的是使用 include_once()
函数提供 db_conn.php
作为参数。我对另一个需要数据库访问的 PHP 文件做同样的事情。我对此 practice/method.
有一些疑问
1.我用了include_once()
函数,多次调用DB连接脚本,这样可以吗?每次都会打开新的连接吗?如果是,应该如何实施?
2. 如果有人试图通过使用 include_once("www.mywebsite.com/php/db_conn.php")
从外部域包含文件,他们能在我的数据库上执行数据库查询吗?
- Is it okay to do this since I use the include_once() function and call the DB connection script more than once? Will it open new
connections each time? If yes how should this be implemented?
是的,这很好。由于您使用的是 include_once
,PHP 会自动识别它已经包含该文件,跳过第二次调用,您将只会获得一个连接。但是,我建议将其更改为 require_once
,因为如果找不到包含的文件,您希望脚本立即失败。
- If someone tries to include the file from an external domain by using include_once("www.mywebsite.com/php/db_conn.php") will they be
able to execute DB queries on my DB?
一般不会。任何请求 URL 的人只会得到一个空白页。但是,最佳做法是将这样的库文件放在 Web 服务器的文档根目录之外。例如:
/path/to/project
/public
index.php
/lib
db_conn.php
在这里,您可以将您的网络服务器指向 /path/to/project/public
,然后在您的 index.php
中,您可以执行如下操作:
require_once '../lib/db_conn.php';
或者也许:
ini_set('include_path', '/path/to/project/lib');
require_once 'db_conn.php';
这样,您自己的代码可以引用 /lib
中的 PHP 个文件,但无法通过 Web 服务器直接请求它们。
1. Is it okay to do this since I use the include_once() function and call the DB connection script more than once? Will it open new
connections each time? If yes how should this be implemented?
你应该使用 require_once("db_conn.php");
http://php.net/manual/en/function.require-once.php
这是include(), include_once(), require()
和require_once()
之间的区别
Difference between require, include, require_once and include_once?
2. If someone tries to include the file from an external domain by using include_once("www.mywebsite.com/php/db_conn.php") will they be
able to execute DB queries on my DB?
如果此设置 disabled/not 在大多数 Web 服务器 (php.ini) 中默认允许,那么您不能使用 include
包含来自远程地址的文件 安全原因.
如果您仍想允许 包含 远程文件,必须在 php.ini[= 中将指令 allow_url_include
设置为 On
24=]
但从面向安全的角度来看,这又是一种不好的做法;因此,它通常被禁用(实际上我从未见过它启用)
如果你想读取远程文件的内容,你可以使用file_get_contents
函数代替BUT 这将作为纯 HTML 标记代码返回,不会有任何 服务器端 代码。
参考:including php file from another server with php
我有一个名为 db_conn.php
的 .php
文件,它建立了到数据库的连接。这是文件的代码。
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: ");
}
else{
}
?>
当我需要从其他文件(例如 login.php
文件)执行一些数据库操作时,我所做的是使用 include_once()
函数提供 db_conn.php
作为参数。我对另一个需要数据库访问的 PHP 文件做同样的事情。我对此 practice/method.
1.我用了include_once()
函数,多次调用DB连接脚本,这样可以吗?每次都会打开新的连接吗?如果是,应该如何实施?
2. 如果有人试图通过使用 include_once("www.mywebsite.com/php/db_conn.php")
从外部域包含文件,他们能在我的数据库上执行数据库查询吗?
- Is it okay to do this since I use the include_once() function and call the DB connection script more than once? Will it open new connections each time? If yes how should this be implemented?
是的,这很好。由于您使用的是 include_once
,PHP 会自动识别它已经包含该文件,跳过第二次调用,您将只会获得一个连接。但是,我建议将其更改为 require_once
,因为如果找不到包含的文件,您希望脚本立即失败。
- If someone tries to include the file from an external domain by using include_once("www.mywebsite.com/php/db_conn.php") will they be able to execute DB queries on my DB?
一般不会。任何请求 URL 的人只会得到一个空白页。但是,最佳做法是将这样的库文件放在 Web 服务器的文档根目录之外。例如:
/path/to/project
/public
index.php
/lib
db_conn.php
在这里,您可以将您的网络服务器指向 /path/to/project/public
,然后在您的 index.php
中,您可以执行如下操作:
require_once '../lib/db_conn.php';
或者也许:
ini_set('include_path', '/path/to/project/lib');
require_once 'db_conn.php';
这样,您自己的代码可以引用 /lib
中的 PHP 个文件,但无法通过 Web 服务器直接请求它们。
1. Is it okay to do this since I use the include_once() function and call the DB connection script more than once? Will it open new connections each time? If yes how should this be implemented?
你应该使用 require_once("db_conn.php");
http://php.net/manual/en/function.require-once.php
这是include(), include_once(), require()
和require_once()
Difference between require, include, require_once and include_once?
2. If someone tries to include the file from an external domain by using include_once("www.mywebsite.com/php/db_conn.php") will they be able to execute DB queries on my DB?
如果此设置 disabled/not 在大多数 Web 服务器 (php.ini) 中默认允许,那么您不能使用 include
包含来自远程地址的文件 安全原因.
如果您仍想允许 包含 远程文件,必须在 php.ini[= 中将指令 allow_url_include
设置为 On
24=]
但从面向安全的角度来看,这又是一种不好的做法;因此,它通常被禁用(实际上我从未见过它启用)
如果你想读取远程文件的内容,你可以使用file_get_contents
函数代替BUT 这将作为纯 HTML 标记代码返回,不会有任何 服务器端 代码。
参考:including php file from another server with php