PHP和SQL Server,如何保证安全?

发布于 2024-11-26 06:58:52 字数 560 浏览 1 评论 0原文

http://www.php.net/manual/en/book.mssql.php

我正在使用它从 LAMP 环境连接到 SQL Server。我注意到我没有像 prepared statements 和 real_escape_string 这样的 neet 函数。

如何使我的查询尽可能安全?任何帮助表示赞赏。

不要建议我使用 ODBC 或 PDO,我没有那个选项。我必须使用我拥有的东西来运行,那就是 MSSQL。

$con = mssql_connect ('xxx', 'xxx', 'xxx');

mssql_select_db('xxx', $con);

$qry = "SELECT 
            firstname
    FROM 
            person
    where firstname = '{$firstname}'";

$query = mssql_query($qry, $con);

http://www.php.net/manual/en/book.mssql.php

I am using that to connect from a LAMP environment to SQL Server. I noticed I don't have the neet functions like prepared statement and real_escape_string.

How can I make my query as secure as possible? Any help is appreciated.

Don't suggest me to use ODBC or PDO, I don't have that option. I have to run with what I have, and that's MSSQL.

$con = mssql_connect ('xxx', 'xxx', 'xxx');

mssql_select_db('xxx', $con);

$qry = "SELECT 
            firstname
    FROM 
            person
    where firstname = '{$firstname}'";

$query = mssql_query($qry, $con);

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

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

发布评论

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

评论(4

起风了 2024-12-03 06:58:52

不要使用文字,使用参数并将它们绑定到您的查询:

$con = mssql_connect ('xxx', 'xxx', 'xxx');
mssql_select_db('xxx', $con);
$qry = 'SELECT firstname
     FROM person
    where firstname = @firstname';
mssql_bind ($qry, '@firstname', $firstname, SQLVARCHAR);
$query = mssql_query($qry, $con);

Don't use literals, use parameters and bind them to your query:

$con = mssql_connect ('xxx', 'xxx', 'xxx');
mssql_select_db('xxx', $con);
$qry = 'SELECT firstname
     FROM person
    where firstname = @firstname';
mssql_bind ($qry, '@firstname', $firstname, SQLVARCHAR);
$query = mssql_query($qry, $con);
菩提树下叶撕阳。 2024-12-03 06:58:52

您可以使用 htmlentities() 来将 html 元素转换为 html 实体,并且该函数接受第三个参数,用于转义单引号和双引号。

这是函数的签名:

string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

以及第二个参数可能采用的参数:

ENT_COMPAT 将转换双引号并保留单引号。

ENT_QUOTES 将转换双引号和单引号。

ENT_NOQUOTES 将保留双引号和单引号不转换。

ENT_IGNORE 默默地丢弃无效的代码单元序列,而不是返回空字符串。 PHP 5.3.0 中添加。这是为了向后兼容而提供的;避免使用它,因为它可能会产生安全隐患。

并且

您可以简单地将addslashes()htmlentities()一起使用

,还有另一个函数可以从字段中清除html标签,即< code>filter_var () ,这样的示例如下:

$return_value = filter_var($data_to_be_filtered,FILTER_SANITIZE_STRING);

重要

不要忘记检查 magic_quotes 是否启用。您可以通过编写以下内容来做到这一点:

if(get_magic_quotes_gpc())
    //do something

有关 magic_quotes 的更多信息: http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

编辑:

您可以使用准备好的-进行更安全的交易声明。它们可以防止 SQL 注入。

示例代码:

$db = new mysqli();
$db->real_connect($host,$username,$password,$db) or die("Cannot connect");
$query = "select name from users where id = ?";
$st = $db->prepare($query); //faster than normal query run
$st->bind_param("d",$id);
$st->execute();
$st->bind_result($name);
$st->fetch();
echo $name;

You can use htmlentities() to convert html elements into html entities and this function accepts a third argument which is for escaping single and double quotes.

Here is the signature of the function:

string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

and the arguments that second parameters may take:

ENT_COMPAT Will convert double-quotes and leave single-quotes alone.

ENT_QUOTES Will convert both double and single quotes.

ENT_NOQUOTES Will leave both double and single quotes unconverted.

ENT_IGNORE Silently discard invalid code unit sequences instead of returning an empty string. Added in PHP 5.3.0. This is provided for backwards compatibility; avoid using it as it may have security implications.

And

You can simply use addslashes() with htmlentities()

and also there is another function with cleans html tags out from the fields which is filter_var () and such example look would be:

$return_value = filter_var($data_to_be_filtered,FILTER_SANITIZE_STRING);

Important

Don't forget to check whether magic_quotes are enabled or not. You can do that by writing :

if(get_magic_quotes_gpc())
    //do something

More about magic_quotes: http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

Edit:

You can do more secure transaction by using prepared-statements. They prevent SQL-Injection.

Sample code:

$db = new mysqli();
$db->real_connect($host,$username,$password,$db) or die("Cannot connect");
$query = "select name from users where id = ?";
$st = $db->prepare($query); //faster than normal query run
$st->bind_param("d",$id);
$st->execute();
$st->bind_result($name);
$st->fetch();
echo $name;
简单 2024-12-03 06:58:52

Mssql 不提供转义查询的函数。一种选择是使用“addslashes()”,尽管它有点难看(并且不包含所有内容)

这可能会有所帮助:如何使用 PHP 在 SQL Server 中转义字符串?

Mssql doesn't supply a function to escape your query. One option is to use "addslashes()" instead, although it is somewhat ugly (and doesn't encompass everything)

This might be helpful: How to escape strings in SQL Server using PHP?

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