php strchar,PHP 转义 text/char 类型的字符串

用户评论:

strata_ranger at hotmail dot com (2011-08-07 20:20:56)

This may seem obvious, but remember that pg_escape_string escapes values for use as string literals in an SQL query -- if you need to escape arbitrary strings for use as SQL identifiers (column names, etc.), there doesn't seem to be a PHP function for that so you'll have to do that escaping yourself.  (PostgreSQL has an in-database function, quote_ident(), that does this.)

This can be an issue if your database contains mixed-case (or otherwise unusual) column names and you have a class interface managing your database/query interactions (for connecting to different types of databases).  If you don't double-quote your column names then postgreSQL will match them case-insensitively, but will label the results in all-lowercase (which differs from MySQL).

For example:

// Escaped column identifier$res=pg_query("Select \"columnName\" from table");$row=pg_fetch_assoc($res);var_dump($row['columnName']);// Worksvar_dump($row['columnname']);// Doesn't?>

ppp (2011-06-30 12:55:52)

pg_escape_string() won't cast array arguments to the "Array" string like php usually does; it returns NULL instead. The following statements all evaluate to true:

$a= array('foo','bar');"$a"=='Array';

(string)$a=='Array';$a.''=='Array';is_null(pg_escape_string($a));?>

strata_ranger at hotmail dot com (2010-07-22 10:40:33)

Forthose curious, the exact escaping performed on the string may vary slightly depending on your database configuration.

For example, if your database's standard_conforming_strings variable is OFF, backslashes are treated as a special character and pg_escape_string() will ensure they are properly escaped. If this variable is ON, backslashes will be treated as ordinary characters, and pg_escape_string() will leave them as-is. In either case, the behavior matches the configuration of the database connection.

Nathan Bruer (2008-02-08 13:23:10)

If your database is a UTF-8 database, you will run into problems trying to add some data into your database...

for securty issues and/or compatability you may need to use the: utf_encode() (http://php.net/utf8-encode) function.

for example:

$my_data=pg_escape_string(utf8_encode($_POST['my_data']));?>

Gautam Khanna (2007-08-29 07:55:14)

Security methods which you use depend on the specific purpose. For those who dont know, take a look at the following built-in PHP functions:

strip_tags() to remove HTML characters

(also see htmlspecialchars)

escapeshellarg() to escape shell commands etc

escapeshellcmd()

mysql_real_escape_string() to escape mySQL commands.

Enjoy!

web dot expert dot panel at gmail dot com

johniskew2 at yahoo dot com (2006-05-30 09:43:07)

For those who escape their single quotes with a backslash (ie \') instead of two single quotes in a row (ie '') there has recently been a SERIOUS sql injection vulnerability that can be employed taking advantage of your chosen escaping method. More info here: http://www.postgresql.org/docs/techdocs.50

Even after the postgre update, you may still be limited to what you can do with your queries if you still insist on backslash escaping. It's a lesson to always use the PHP functions to do proper escaping instead of adhoc addslashes or magic quotes escaping.

meng (2006-05-27 16:21:57)

Since php 5.1 the new function pg_query_params() was introduced. With this function you can use bind variables and don't have to escape strings. If you can use it, do so. If unsure why, check the changelog for Postgres 8.0.8.

otix (2006-04-24 15:43:46)

Creating a double-tick is just fine. It works the same as the backslash-tick syntax. From the PostgreSQL docs:

The fact that string constants are bound by single quotes presents an obvious semantic problem, however, in that if the sequence itself contains a single quote, the literal bounds of the constant are made ambiguous. To escape (make literal) a single quote within the string, you may type two adjacent single quotes. The parser will interpret the two adjacent single quotes within the string constant as a single, literal single quote. PostgreSQL will also allow single quotes to be embedded by using a C-style backslash.

rich at dicksonlife dot com (2005-07-19 05:38:00)

Here's some code I knocked up to turn an array of values into a string representation of an array. Note that I also add the external single quotes to make it a full string literal.

//$t is array to be escaped. $u will be string literal.

$tv=array();

foreach($t as $key=>$val){

$tv[$key]="\"" .

str_replace("\"",'\\"', str_replace('\\','\\\\',$val)) . "\"

";

}

$u= implode(",",$tv) ;

$u="'{" . pg_escape_string($u) . "}'";

There's probably a better way of doing this. That's why I'm posting this here :)

tsharek at o2 dot pl (2005-03-02 03:34:56)

IMO the stripslashes in this case is not very usefull. Because pg_escape_string change ' into '' (double ' - not "). I use in add to database this:

pg_escape_string(stripslashes($_GET['var'])) and is in 100% safe (i hope).

If I use addslashes in this case that well be lost space in database (\''' - this is 3 bytes)

ps. sorry for my english:)

(2003-07-16 11:30:26)

Here with 'abc'efg' the middle ' terminates the string, however 'abc\'def' is one big string with a ' character in the middle.

If the user can terminate the string he can then put in the bad sql. When prompted for Barcode the user could put in DROP TABLE foo; SELECT '1

$query = sprintf ("SELECT * FROM a.tblcards WHERE barcode='%s'", pg_escape_string($barcode));

So you have to "clean" your variable coming in to prevent that.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值