반응형

PHP에서 Form의 데이터를 POST 또는 GET 방식으로 전송 시 

데이터에 원하지 않게 역슬래시(\)가 들어가는 경우가 있습니다.

데이터베이스에 저장시 '(작은 따옴표)가 들어가서 문제가 생기는 것을 방지하기 위해

PHP에서 제공하여 주는 \' 이렇게 처리를 해 주는 좋은 기능입니다.

 

하지만 이런 내용을 정확히 모르고 접근을 하게 되면,

이유없이 추가되는 역슬래시로 문제 해결에 어려움을 겪을 수가 있습니다.

더욱이 이 기능은 PHP 프로그램 내에서는 변경 할 수가 없고

서버 config값에서 처리를 해 줘야 합니다.

 

이에 대한 대처 방안으로는 get_magic_quotes_gpc(), stripslashes 2개 함수를 이용하는 방법입니다.

get_magic_quotes_gpc()를 이용하여, 해당 옵션이 켜져 있는지 확인을 하고,

만약 켜져 있다면 stripslashes 함수를 이용하여 역슬래시를 제거하는 것입니다.

아래와 같이 소스 코딩을 하시게 되면 해결이 가능합니다.

	$form_data = $_REQUEST['form_data'];
	if (get_magic_quotes_gpc()) {
	  $form_data = stripslashes($from_data);
	}

PHP Form 데이터 POST, GET 전송시 역슬래쉬(\) 자동 추가 현상 해결 방안 - get_magic_quotes_gpc(), stripslashes

# 참고 자료 (출처 : php net)

get_magic_quotes_gpc

(PHP 4, PHP 5, PHP 7)

get_magic_quotes_gpc Gets the current configuration setting of magic_quotes_gpc

Description

get_magic_quotes_gpc ( void ) : bool

Returns the current configuration setting of magic_quotes_gpc

Keep in mind that attempting to set magic_quotes_gpc at runtime will not work.

For more information about magic_quotes, see this security section.

Return Values

Returns 0 if magic_quotes_gpc is off, 1 otherwise. Or always returns FALSE as of PHP 5.4.0.

Changelog

VersionDescription

5.4.0 Always returns FALSE because the magic quotes feature was removed from PHP.

Examples

Example #1 get_magic_quotes_gpc() example

<?php
// If magic quotes are enabled
echo $_POST['lastname'];             // O\'reilly
echo addslashes($_POST['lastname']); // O\\\'reilly

// Usage across all PHP versions
if (get_magic_quotes_gpc()) {
    $lastname = stripslashes($_POST['lastname']);
}
else {
    $lastname = $_POST['lastname'];
}

// If using MySQL
$lastname = mysql_real_escape_string($lastname);

echo $lastname; // O\'reilly
$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
?>

 

stripslashes

(PHP 4, PHP 5, PHP 7)

stripslashes Un-quotes a quoted string

Description

stripslashes ( string $str ) : string

Un-quotes a quoted string.

Note:

If magic_quotes_sybase is on, no backslashes are stripped off but two apostrophes are replaced by one instead.

An example use of stripslashes() is when the PHP directive magic_quotes_gpc is on (it was on by default before PHP 5.4), and you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from an HTML form.

Parameters

str

The input string.

Return Values

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

Examples

Example #1 A stripslashes() example

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>
반응형

+ Recent posts