Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Escaping (') and (") with magic_quotes_gpc on

Posted on 2008-06-10
2
464 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 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 …

792 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