Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Escaping (') and (") with magic_quotes_gpc on

Posted on 2008-06-10
2
Medium Priority
?
470 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
[X]
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
2 Comments
 
LVL 2

Accepted Solution

by:
yerfdoggy earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

730 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