Jump to content

PHP Post is returning single quote character


zander1983

Recommended Posts

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?

The magic_quotes_gpc setting is enabled on your server. If you have access to the php.ini file you can turn this off. If you are on a shared server you will not be able to do this.

 

Depending on your situation it is probably better to "cleanse" your user input in a server independent manner - i.e. using a process that will work no matter what the server setting is. there is an example process in the PHP manual for this - along with other useful information on the subject.

 

http://www.php.net/manual/en/function.get-magic-quotes-gpc.php

Put this script in a file, and include it into all processing scripts.  It will strip the slashes automatically.

<?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);
}
?>

 

*note* NOT MY SCRIPT, can't remember where it came from, but it is in my library.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.