Page 1 of 1

PHP Post is returning the single quote character

Posted: Mon Mar 28, 2011 2:35 pm
by Zander1983
Hi
I have a simple form and when the user submits, php is putting a \ before every single quote entered in the field. So for example, if a user enters O'Neill, once i do
$lastName = $_POST["lastname"];

$lastName comes back as: O\'Neill

is there some way I can turn this off?

Re: PHP Post is returning the single quote character

Posted: Mon Mar 28, 2011 2:47 pm
by fugix
try this....

Code: Select all

$lastName = stripslashes($_POST["lastname"]);

Re: PHP Post is returning the single quote character

Posted: Tue Mar 29, 2011 5:45 am
by Zander1983
I found this solution. I put this script in a file, and included it into all my processing scripts. It strips the lashes automatically so no need to do striplashes() for each post.


<?php
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function GPCStrip($arr)
{
if (is_array($arr))
{
foreach ($arr AS $arrKey => $arrVal)
{
$arr[$arrKey] = GPCStrip($arrVal);
}
}
else if (is_string($arr))
{
$arr = stripslashes($arr);
}
return $arr;
}
$_GET = GPCStrip($_GET);
$_POST = GPCStrip($_POST);
$_COOKIE = GPCStrip($_COOKIE);
if (is_array($_FILES))
{
foreach ($_FILES AS $key => $val)
{
$_FILES[$key]['tmp_name'] = str_replace('\\', '\\\\', $val['tmp_name']);
}
}
$_FILES = GPCStrip($_FILES);
}
if (function_exists('set_magic_quotes_runtime'))
{
set_magic_quotes_runtime(0);
}
?>