magic quotes agony

Posted on 2009-02-12
Last Modified: 2012-05-06

I'm sending some data to a PHP script via a POST. It contains single quotes and stuff like that which should NOT be escaped. However it also has backslashes too as valid data. So if I use stripslashes, that will remove my valid backslashes too?

My hosting company doesn't give me a .htaccess file, I set magic quotes to OFF in the php.ini file and copied it into all directories, but it's still adding the backslashes.

Question by:DJ_AM_Juicebox
    LVL 2

    Accepted Solution

    Hi there...

    You can try to encode text before sending to php by using javascript
    check out this site:

    and such encoded (encodeURI) text decode using function (not mine) in php

    function utf8Urldecode($value)
        if (is_array($value)) {
            foreach ($value as $key => $val) {
                $value[$key] = utf8Urldecode($val);
        } else {
            $value = preg_replace('/%([0-9a-f]{2})/ie', 'chr(hexdec($1))', (string) $value);
        return $value;

    Open in new window

    LVL 6

    Assisted Solution

    You could use this, even though its inefficient.  But I guess its better than nothing.

    if (get_magic_quotes_gpc()) {
       function stripslashes_deep($value)
           $value = is_array($value) ?
                       array_map('stripslashes_deep', $value) :
           return $value;
       $_POST = array_map('stripslashes_deep', $_POST);
       $_GET = array_map('stripslashes_deep', $_GET);
       $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
       $_REQUEST = array_map('stripslashes_deep', $_REQUEST);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now