Page 1 of 1

Problem addslashes function with magic quotes

Posted: Mon Mar 16, 2009 9:27 am
by Myrosy
Hi,

I am developing open source portal script, and for security reason I am using addslashes function with magic quotes and trim for inserting the inputs in the database…etc

This is my code:

Code: Select all

<?php
    function add_slashes($Str)
    {
        if (@get_magic_quotes_gpc()){
            if ( is_array($Str) ){
                foreach ($Str as $k => $v){
                    $Str[$k] = trim($v);
                }
            }else{
                $Str = trim($Str);
            }
        }else{
            if ( is_array($Str) )
            {
                foreach ($Str as $k => $v)
                {
                    $Str[$k] = addslashes(trim($v));
                }
            }
            else
            {
                $Str = addslashes(trim($Str));
            }
        }
       return $Str;
    }
?>
Some people who is using my script; the function does not work properly, and they complained that they are getting (rn) and (\r\n) instead of new line after inserting the text in MySQL...

Example:
First Line
Second Line
The result:
First Line rn Second Line
Or
First Line \r\n Second Line

I fond that the Magic quotes gpc is turned on in my server and the function working fine with me, and for those people who complained from that problem the Magic quotes gpc is turned off

How to write a function that checks if magic quotes is running and then runs addslashes () based on the results?

Kind regards,

Re: Problem addslashes function with magic quotes

Posted: Mon Mar 16, 2009 10:02 am
by pickle