php 函数参数内的空格问题

发布于 2024-08-22 07:32:14 字数 820 浏览 3 评论 0 原文

抱歉没有格式化我的代码。工具栏不见了...

我想将一些数据插入 mysql 数据库。我在 php 中编写了一个函数:

function add_ID($ID, $token)  {
 $add = "INSERT INTO ids (ID, token) VALUES ('$ID', '$token')";
 mysql_query($add);
 echo 'added successfully';
}  
if(isset($_GET['addDeviceID'])) {
 add_ID($_GET['ID'], $_GET['token']);
}

在我的 Browswe 的 URL 字段中,我像这样调用该函数: http://www.justanexample.com/example.php?ID= 123123123&token=qwertzuiop

有效。

如果我在任一参数中添加一个空格,例如: http://www.justanexample.com/example.php?ID=123123 123&token=qwertzuiop

我的 mysql 数据库中没有添加任何内容。

如果能得到一些帮助就太好了:) 谢谢你!

Sorry for not formatting my code. the toolbar was gone...

I want to insert some data into a mysql db. I've wrote a function in php:

function add_ID($ID, $token)  {
 $add = "INSERT INTO ids (ID, token) VALUES ('$ID', '$token')";
 mysql_query($add);
 echo 'added successfully';
}  
if(isset($_GET['addDeviceID'])) {
 add_ID($_GET['ID'], $_GET['token']);
}

In the URL-Field of my Browswe I'am calling the function like that:
http://www.justanexample.com/example.php?ID=123123123&token=qwertzuiop

That works.

If I put a space into either one of the parameters for example like that:
http://www.justanexample.com/example.php?ID=123123 123&token=qwertzuiop

Nothing was added to my mysql db.

Would be great to get some help :)
Thank you!

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

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

发布评论

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

评论(5

余生再见 2024-08-29 07:32:14

您应该在将输入发送到数据库之前验证您的输入。或者,如果无法进行验证,请过滤和/或转义该值。

验证

如果您希望 ID 是大于零的整数:

if (!ctype_digit($ID)) {
    // invalid ID
}

如果您希望 token 是字母数字字符串:

if (!ctype_alnum($token)) {
    // invalid token
}

过滤

过滤会删除输入中的无效部分,使其变为有效:

if (!ctype_digit($ID)) {
    $ID = preg_replace('/\D+/', '', $ID);
    // $ID does now only contain digits
}
if (!ctype_alnum($token)) {
    $token = preg_replace('/\D+/', '', $token);
    // $token does now only contain alphanumeric characters
}

转义

转义正在替换某个特定上下文的元字符,某些字符串应放置在其中。对于 MySQL 查询,您应该使用一个函数来转义 MySQL 中的上下文字符串声明。为此,PHP 有 mysql_real_escape_string 函数

$add = "INSERT INTO ids (ID, token) VALUES ('".mysql_real_escape_string($ID)."', '".mysql_real_escape_string($token)."')";

You should validate your input before sending it to the database. Or, if validation is not possible, filter and/or escape the value.

Validation

If you expect ID to be a integer greater than zero:

if (!ctype_digit($ID)) {
    // invalid ID
}

If you expect token to be an alphanumeric string:

if (!ctype_alnum($token)) {
    // invalid token
}

Filtering

Filtering is removing invalid parts of the input so that it becomes valid:

if (!ctype_digit($ID)) {
    $ID = preg_replace('/\D+/', '', $ID);
    // $ID does now only contain digits
}
if (!ctype_alnum($token)) {
    $token = preg_replace('/\D+/', '', $token);
    // $token does now only contain alphanumeric characters
}

Escaping

Escaping is replacing the meta characters of a specific context some string is meant to be placed in. For MySQL queries you should use a function that escapes the meta characters of the context string declaration in MySQL. PHP has the mysql_real_escape_string function for that purpose:

$add = "INSERT INTO ids (ID, token) VALUES ('".mysql_real_escape_string($ID)."', '".mysql_real_escape_string($token)."')";
窗影残 2024-08-29 07:32:14

您的函数容易受到 SQL 注入的攻击。您应该在 SQL 查询中使用所有用户接收的参数之前验证它们,并通过 mysql_real_escape_string 传递任何字符串,因为这样我就可以传入类似 example.php?token='; 的内容。删除数据库; 并彻底搞砸您的应用程序。

在您的情况下,您应该首先检查接收到的参数是否符合您期望的形式,如果不是,则向用户返回错误,然后才将它们传递到 SQL 查询中。

function add_ID($ID, $token)  {
  $id = mysql_real_escape_string($id);
  $token = mysql_real_escape_string($token);

  $add = "INSERT INTO ids (ID, token) VALUES ('$ID', '$token')";
  mysql_query($add);
  echo 'added successfully';
}  

if(isset($_GET['addDeviceID'])) {
  $id    = isset($_GET['id']) ? $_GET['id'] : 0; // in case no ID has been passed in
  $token = isset($_GET['token']) ? $_GET['token'] : '';

  if (!is_numeric($id) {
    die('ID is not a number');
  } 

  // validate token here as well

  add_ID($id, $token);
}

您还应该研究参数化查询,总体而言,这是一种比仅使用字符串连接更好的参数执行 SQL 查询的方法。为此,请考虑使用 mysqli 扩展而不是 mysql,或者在更高级别上使用 PDO。

Your function is vulnerable to SQL injection. You should validate all user-received parameters before using them in an SQL query, and pass any strings through mysql_real_escape_string, because then I could just pass in something like example.php?token='; DROP DATABASE; and royally screw up your application.

In your case, you should make a check that the received parameters are in the form that you expect first, return an error to the user if they don't, and only then pass them into the SQL query.

function add_ID($ID, $token)  {
  $id = mysql_real_escape_string($id);
  $token = mysql_real_escape_string($token);

  $add = "INSERT INTO ids (ID, token) VALUES ('$ID', '$token')";
  mysql_query($add);
  echo 'added successfully';
}  

if(isset($_GET['addDeviceID'])) {
  $id    = isset($_GET['id']) ? $_GET['id'] : 0; // in case no ID has been passed in
  $token = isset($_GET['token']) ? $_GET['token'] : '';

  if (!is_numeric($id) {
    die('ID is not a number');
  } 

  // validate token here as well

  add_ID($id, $token);
}

You should also look into parametrized queries, which are an overall much better way of doing SQL queries with parameters than just using string concatenation. For that, look into using the mysqli extension instead of mysql, or at a higher level, PDO.

野稚 2024-08-29 07:32:14

使用 str_replace 函数删除其中的空格,例如:

 $ID = str_replace(' ', '', $ID);
 $token= str_replace(' ', '', $token);

 $add = "INSERT INTO ids (ID, token) VALUES ('$ID', '$token')";

另外,我怀疑您的 $ID 是表中的整数字段,因此您可以在不指定引号的情况下运行查询,例如:

 $add = "INSERT INTO ids (ID, token) VALUES ($ID, '$token')";

Remove space from them using str_replace function eg:

 $ID = str_replace(' ', '', $ID);
 $token= str_replace(' ', '', $token);

 $add = "INSERT INTO ids (ID, token) VALUES ('$ID', '$token')";

Also, I suspect your $ID is a integer field in your table so you can run your query without specifying quotes eg:

 $add = "INSERT INTO ids (ID, token) VALUES ($ID, '$token')";
樱花落人离去 2024-08-29 07:32:14

您的代码假设查询成功完成,而不检查是否存在错误。我猜由于空格,这将是一个语法错误。如果你的ID字段是整数类型,那么执行ID=123 123将会出现语法错误。包括其他答案中的所有 SQL 注入和数据清理建议,您应该按如下方式重写您的 add_ID 函数:

function add_ID($ID, $token) {
  $query = 'blah blah blah';
  mysql_query($query);
  if (mysql_error()) {
       echo 'ruhroh, someone set us up the bomb: ', mysql_error();
  } else {
       echo 'woohoo, it worked!';
  }
}

至少这会告诉您查询是否真的成功,以及如果没有成功,会发生什么。永远不要假设任何类型的数据库查询都会成功。有太多的方法会导致它崩溃(服务器死机、事务死锁、连接池耗尽、磁盘空间不足等),甚至不能像上面那样进行一些简单的错误处理。

Your code is assuming the query successfully completes without ever checking if there was an error. I'm guessing it'll be a syntax error due to the spaces. If your ID field is an integer type, then doing ID=123 123 will be the syntax error. Including all the SQL injection and data sanitizing advice in the other answers, you should rewrite your add_ID function as follows:

function add_ID($ID, $token) {
  $query = 'blah blah blah';
  mysql_query($query);
  if (mysql_error()) {
       echo 'ruhroh, someone set us up the bomb: ', mysql_error();
  } else {
       echo 'woohoo, it worked!';
  }
}

At least this will tell you if the query REALLY did succeed, and what blew up if it didn't. Never assume that a database query of any sort will succeed. There's far too many ways for it to blow up (server died, transaction deadlock, connection pool exhausted, out of disk space, etc...) to NOT have even some simplistic error handling as above.

南风几经秋 2024-08-29 07:32:14

您可以使用 str_replace 删除空格。但这不是一个好的做法。
怎么修改URL呢?在正常情况下,这是不真实的。
相反,您应该测试来自用户的所有输入值(ID 必须是整数,Token 不应包含“'”符号和其他检查)。阅读有关 sql 注入的信息。

You can use str_replace to remove spaces. But it's not a good practice.
How can URL's be modified so? In normal cases it's unreal.
Contrary, you should test all input values from user(ID must be an integer, Token shouldn't contains "'" symbol and other checks). Read about sql-injections.

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