/    Sign up×
Community /Pin to ProfileBookmark

Stripslashes & Magic Quotes

Quick question about these two terms. I have magic quotes on my server, so when INSERT data in database via a php form on the web is it best to:

  • Add data to my db via a form without running stripslashes on each data field I enter
    Or

  • Adding my data to my database with stripslashes and then running the stripslashes funtion when I want my data to be outputted on my website?
  • Just wondered which was percieved the best way?

    Thanks

    to post a comment
    PHP

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @sanchez_1960May 31.2007 — I always use addslashes and stripslashes no matter what.
    Copy linkTweet thisAlerts:
    @MrCoderMay 31.2007 — Look up mysql_real_escape_string()
    Copy linkTweet thisAlerts:
    @chrisbauthorMay 31.2007 — Thanks, but is it best to use this when INSERTING or SELECTING? As the example gives both...
    Copy linkTweet thisAlerts:
    @NogDogMay 31.2007 — Since magic_quotes_gpc is essentially deprecated now and likely won't even be available in PHP 6, I always code so that I do not depend on it. As mysql_real_escape_string() is better suited than addslashes() for sanitizing data for use in MySQL queries, it should be used when inserting data into the database (assuming MySQL is the DBMS being used).

    In either case, there should not be a need to use stripslashes() when retrieving data, unless you have been (mistakenly) adding slashes on top of "magic quotes" slashes when you did the insert/update to the database.

    When inserting user-supplied values into a query, you can do something like the following for portability between systems regardless of the magic quotes setting:
    [code=php]
    function sanitize($value)
    {
    if(get_magic_quotes_gpc()) // get rid of magic_quotes escapes
    {
    $value = stripslashes($value);
    }
    if(!is_numeric($value)) // quote the value and escape special SQL char.'s
    {
    $value = "'" . mysql_real_escape_string($value) . "'";
    }
    return($value);
    }

    $sql = sprintf(
    "SELECT * FROM table_name WHERE col_1 = %s AND col_2 = %s",
    sanitize($_POST['field1']),
    sanitize($_POST['field2'])
    );
    mysql_query($sql) or die("SQL error ($sql): " . mysql_error());
    [/code]
    ×

    Success!

    Help @chrisb spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...