Solved

Escaping (') and (") with magic_quotes_gpc on

Posted on 2008-06-10
2
463 Views
Last Modified: 2013-12-13
Server configuration

Directive                              Local Value                Master Value
magic_quotes_gpc                    On                               On
magic_quotes_runtime             Off                               Off
magic_quotes_sybase              Off                               Off

Commercial hosting cannot touch php.ini

Problem, I need to carry posted data from user over 4 different pages on the fifth pages sql query to insert the data, at this point I'm using hidden fields in the form, so basically user fill a form go to the next page information is displayed as well as some other fields, the information displayed is also in the new form as hidden value and then we go to the next page and so on.

This work alright untill somebody use (") than wathever is after the first that is being cut off, I tried mysql_real_escape_string, addslashes, stripslashes etc and I cannot get it right.

Any advice would be greatly appreciated.
0
Comment
Question by:gamebits
2 Comments
 
LVL 2

Accepted Solution

by:
yerfdoggy earned 500 total points
ID: 21757269
You could try using this function at the beginning of each script you are passing the POST vars into. It resolves the confusion of of magic_quotes_on == true/false. Use it like this:

$arrCleanPost = cleanup($_POST);

Thereafter, in each script, use $arrCleanPost instead of $_POST. All slashes will have been removed, so at least you know where you are starting from.

/**
 * FUNCTION: cleanup
 * puts data into standard form irrespective of php.ini magic_quotes_gpc on/off
 **/
function cleanup($arrDirtyData)
{
    $arrCleanData = array();
    if (get_magic_quotes_gpc())
    {
        foreach ($arrDirtyData as $key=>$value)
        {
                  if (is_string($value))
                        $arrCleanData[$key] = stripslashes($value);
                  else
                        $arrCleanData[$key] = $value;
        }
    }
    else
    {
        foreach ($arrDirtyData as $key=>$value)
        {
            $arrCleanData[$key] = $value;
        }
    }
    return $arrCleanData;
}
0
 
LVL 28

Author Comment

by:gamebits
ID: 21778160
Thank you for your reply yerfdoggy although your solution work I found another way to do what I want, for future reference here is what I do

The first time I'm using the posted data I clean up the data by using stripslashes and then I save the data in a session, if on a subsequent page I need to display the data I just echo the session variable, when I'm ready to insert the data in the database I use mysql_real_escape_string() function to sanitize the data in the session prior to send it to the database.

The data will be saved in the db with all the quotation mark escaped as well as the newline and return characters.

To display the data from the db I use nl2br() function.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

776 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