[FAQ] addslashes() and striplashes()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Pieter Kunst

    [FAQ] addslashes() and striplashes()

    Q. How do I use addslashes() and stripslashes() when dealing with HTML
    forms and database INSERTs, UPDATEs and SELECTs?

    A. It depends on the setting of the php.ini directive
    "magic_quotes_g pc". By default, magic_quotes_gp c is On.

    If magic_quotes_gp c is On:
    Use stripslashes() for data which originates from an HTML form and is
    shown in an HTML page after a roundtrip to the server.
    Do not use addslashes().

    If magic_quotes_gp c is Off:
    Use addslashes() when INSERTing or UPDATEing data into the database.
    Do not use stripslashes().

    You can check the magic_quotes_gp c setting with the
    get_magic_quote s_gpc() function.

    Contrary to popular belief, it should never be necessary to use
    stripslashes() on data which originates from a database SELECT. The
    slashes which are added manually with addslashes(), or automagically
    when magic_quotes_gp c is On, are only used to properly escape the data
    before an INSERT or UPDATE and are themselves not stored in the database.

    Refer:




    +++++
    @todo grammar checking by native English speaker. Maybe the 'Q' should
    be something like "Why do I have backslashes in my form data"? Not sure.

    JP
    --
    Sorry, <devnull@cauce. org> is a spam trap.
    Real e-mail address unavailable. 5000+ spams per month.
  • Kenneth Downs

    #2
    Re: [FAQ] addslashes() and striplashes()

    Jan Pieter Kunst wrote:
    [color=blue]
    > Q. How do I use addslashes() and stripslashes() when dealing with HTML
    > forms and database INSERTs, UPDATEs and SELECTs?
    >
    > A. It depends on the setting of the php.ini directive
    > "magic_quotes_g pc". By default, magic_quotes_gp c is On.
    >
    > If magic_quotes_gp c is On:
    > Use stripslashes() for data which originates from an HTML form and is
    > shown in an HTML page after a roundtrip to the server.
    > Do not use addslashes().
    >
    > If magic_quotes_gp c is Off:
    > Use addslashes() when INSERTing or UPDATEing data into the database.
    > Do not use stripslashes().
    >
    > You can check the magic_quotes_gp c setting with the
    > get_magic_quote s_gpc() function.
    >
    > Contrary to popular belief, it should never be necessary to use
    > stripslashes() on data which originates from a database SELECT. The
    > slashes which are added manually with addslashes(), or automagically
    > when magic_quotes_gp c is On, are only used to properly escape the data
    > before an INSERT or UPDATE and are themselves not stored in the database.
    >
    > Refer:
    > http://www.php.net/manual/en/function.addslashes.php
    > http://www.php.net/manual/en/function.stripslashes.php
    > http://www.php.net/manual/en/functio...quotes-gpc.php
    >
    > +++++
    > @todo grammar checking by native English speaker.[/color]

    Better than most Americans.
    [color=blue]
    > Maybe the 'Q' should
    > be something like "Why do I have backslashes in my form data"? Not sure.[/color]

    Probably yes. Actually there may be several questions that could all route
    to the same answer, users could be having a variety of problems that the
    information above is relevant to, so how about:

    Q: I have heard you should always use "addslashes " before putting user input
    into a database. Why is that?

    Q: How do I handle single quotes in user input, I am getting errors trying
    to INSERT and UPDATE.

    Q: Do I need Addslashes on data coming from a database server and going to
    the browser?

    Q: Why do I have backslashes in my form data?

    Q: What do I need to do to "sanitize" user input so it is database-safe?
    [color=blue]
    >
    > JP[/color]

    --
    Kenneth Downs
    Secure Data Software, Inc.
    (Ken)nneth@(Sec )ure(Dat)a(.com )

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: [FAQ] addslashes() and striplashes()

      Q: How do I use addslashes() and stripslashes() when dealing with HTML
      forms and database INSERTs, UPDATEs and SELECTs?
      Q: How do I handle single quotes in user input, I am getting errors
      trying
      to INSERT and UPDATE.
      Q: Why do I have backslashes in my form data?
      Q: What do I need to do to "sanitize" user input so it is
      database-safe?

      A. It depends on the setting of the php.ini directive
      "magic_quotes_g pc". By default, magic_quotes_gp c is On.

      If magic_quotes_gp c is On:
      Use stripslashes() for data which originates from an HTML form and is
      shown in an HTML page after a roundtrip to the server.
      Do not use addslashes().

      If magic_quotes_gp c is Off:
      Use addslashes() when INSERTing or UPDATEing data into the database.
      Do not use stripslashes().

      You can check the magic_quotes_gp c setting with the
      get_magic_quote s_gpc() function.

      Contrary to popular belief, it should never be necessary to use
      stripslashes() on data which originates from a database SELECT. The
      slashes which are added manually with addslashes(), or automagically
      when magic_quotes_gp c is On, are only used to properly escape the data
      before an INSERT or UPDATE and are themselves not stored in the
      database.

      Refer:


      Gets the current configuration setting of magic_quotes_gpc


      +++++
      @revision 1 Initial
      @revision 2 Merged Kenneth Downs comments. Shortened document URLs
      @author Jan Pieter Kunst, Kenneth Downs
      @todo grammar checking by native English speaker. Maybe the 'Q' should
      be something like "Why do I have backslashes in my form data"? Not
      sure.
      @todo Bring this question to main FAQ Thread. (missed this one in Rev 5)

      Comment

      • R. Rajesh Jeba Anbiah

        #4
        Re: [FAQ] addslashes() and striplashes()

        Q: How do I use addslashes() and stripslashes() when dealing with HTML
        forms and database INSERTs, UPDATEs and SELECTs?
        Q: How do I handle single quotes in user input, I am getting errors
        trying
        to INSERT and UPDATE.
        Q: Why do I have backslashes in my form data?
        Q: What do I need to do to "sanitize" user input so it is
        database-safe?

        A. It depends on the setting of the php.ini directive
        "magic_quotes_g pc". By default, magic_quotes_gp c is On.

        If magic_quotes_gp c is On:
        Use stripslashes() for data which originates from an HTML form and is
        shown in an HTML page after a roundtrip to the server.
        Do not use addslashes().

        If magic_quotes_gp c is Off:
        Use addslashes() when INSERTing or UPDATEing data into the database.
        Do not use stripslashes().

        You can check the magic_quotes_gp c setting with the
        get_magic_quote s_gpc() function.

        Contrary to popular belief, it should never be necessary to use
        stripslashes() on data which originates from a database SELECT. The
        slashes which are added manually with addslashes(), or automagically
        when magic_quotes_gp c is On, are only used to properly escape the data
        before an INSERT or UPDATE and are themselves not stored in the
        database.

        Refer:


        Gets the current configuration setting of magic_quotes_gpc


        ------------------
        Q. I'm getting extra \ in my form field
        A. You are using Get/Post/Cookie (GPC) data without stripping the
        magically [1] added quotes (which are on by default).
        If aren't getting the data from GPC magic_quotes_ru ntime might be
        on (off by default)

        Q. When to escape?
        A. Only use the right escape method at the moment it is needed.
        What the right escape method actually is depends on where the data
        will be used. If you are inserting the string $bar into eg mysql
        you should escape it with mysql_real_esca pe_string() [2]
        $query="UPDATE foo SET bar='".mysql_re al_escape_strin g($bar)."'";
        the same base shoule be htmlescaped [3] when used in html
        echo "<a
        href='foo.php?b ar=".urlencode( $bar)."'>".html specialchars($b ar,ENT_QUOTES). "</a>";
        (also note that $bar needs to be urlescaped [4] if used in an URL)
        So in oorder to make this work you should always keep the raw
        unescaped values (this is why (IMHO) magic_quotes is evil). To make
        sure you are actually working with the raw values you should
        sanitize
        GPC data with something like this (untested and incomplete code):
        function slashed($t)
        {
        if(get_magic_qu otes_gpc())
        {
        if(is_array($t) && count($t))
        for($i=0;$i<cou nt($t);$i++)
        $t[$i]=slashed($t[$i]);
        else
        $t=stripslashes ($t);
        }
        return $t;
        }
        $_GET=slashed($ _GET);
        $_POST=slashed( $_POST);
        $_REQUEST=slash ed($_REQUEST);
        $_COOKIE=slashe d($_COOKIE);

        Q. When and how to sanitize data
        A. Like escaping it depends on usage. If know data in a sql row should
        be an int, you could do something like this:
        $query="UPDATE foo SET bar='".((int)$b ar)."'";
        If the data should contain a dutch style postalcode:
        if(!preg_match( "/^\d{4}\s*[A-Za-z]{2}$/",trim($zip ))
        die("error in zip");
        But IMHO you shouldn't try to fix obviously wrong data.

        [1] http://www.php.net/magicquotes
        [2] http://www.php.net/mysql_real_escape_string
        [3] http://www.php.net/htmlspecialchars
        [4] http://www.php.net/urlencode

        +++++
        @revision 1 Initial
        @revision 2 Merged Kenneth Downs comments. Shortened document URLs
        @revision 3 Shortened document URLs. Brining Daniel Tryba's
        <news:423ec0f5$ 0$154$c5fe704e@ news6.xs4all.nl > entry here so that it
        won't be get orphaned
        @author Jan Pieter Kunst, Kenneth Downs, Daniel Tryba
        @todo grammar checking by native English speaker. Maybe the 'Q' should
        be something like "Why do I have backslashes in my form data"? Not
        sure.
        @todo Bring this question to main FAQ Thread. (missed this one in Rev
        5). Merge the Q&A's appropriately

        Comment

        • Daniel Tryba

          #5
          Re: [FAQ] addslashes() and striplashes()

          R. Rajesh Jeba Anbiah <ng4rrjanbiah@r ediffmail.com> wrote:[color=blue]
          > If aren't getting the data from GPC magic_quotes_ru ntime might be
          > on (off by default)[/color]

          Time for a zpellchecker/thesaurus:

          Should be:
          When not getting the data

          [color=blue]
          > the same base shoule be htmlescaped [3] when used in html[/color]
          ^^^^ $bar
          [color=blue]
          > So in oorder to make this work you should always keep the raw[/color]
          ^^^^^^ order

          Comment

          Working...