Problem addslashes function with magic quotes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Myrosy
Forum Newbie
Posts: 1
Joined: Mon Mar 16, 2009 9:23 am

Problem addslashes function with magic quotes

Post 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,
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Problem addslashes function with magic quotes

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply