Solved

Escaping (') and (") with magic_quotes_gpc on

Posted on 2008-06-10
2
461 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

12 Experts available now in Live!

Get 1:1 Help Now