Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of PHP's stripslashes function?

Tags:

php

mysqli

Suppose you have the following:

<?php

    $connection = mysqli_connect("host", "root", "passwd", "dbname");

    $habits = mysqli_real_escape_string($connection, $_POST['habits']);

?>

Lets say you entered, for a field called 'habits', the value 'tug o' war'. Therefore, the mysqli_real_escape_string function will escape the second quote symbol and the database engine won't be fooled into thinking that the value is 'tug o'. Instead, it will know that the value is actually 'tug o' war'. My question is, why then do you need a stripslashes function? A stripslashes function will simply strip away the good work that was done by the mysqli_real_escape_string function. Stripping away a backslash will simply return you to where you were, and the database will be fooled again. Am I to assume that the stripslashes function is NOT used for database purposes? That is, would this piece of code be completely nonsensical?:

<?php

    $connection = mysqli_connect("host", "root", "passwd", "dbname");

    $habits = mysqli_real_escape_string($connection, $_POST['habits']);

    $undosomething = stripslashes($habits);

    echo '$undosomething';

?>

If stripslashes is NOT used for database purposes, what exactly is it used for?

like image 823
Jason12 Avatar asked Dec 25 '14 19:12

Jason12


People also ask

What does Addslashes do in PHP?

Definition and Usage The addslashes() function returns a string with backslashes in front of predefined characters. The predefined characters are: single quote (') double quote (")

How do you escape a slash in PHP?

Escape Sequences In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.

How do you remove a string slash?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.


2 Answers

(As of 2014) It still has it's uses. It's not really used with a database but even with this, there are use cases. Let's assume, or send data from form using JavaScript and you have a string with a quote such as "It's". Than getting the value through the $_GET supervariable, you'll get the value "It's". If you than use mysqli_real_escape_string() or similar (which you must use for security), the quote will be double encoded and the backslash will be saved to the DB. For such scenarios, you would use this function. But it is not for security and not necessarily used with a DB.

like image 135
2ndkauboy Avatar answered Oct 29 '22 18:10

2ndkauboy


Per the Manual, the purpose of stripslashes() is to unquote a quoted string, meaning to remove a backslash quoting a character. Note, PHP lacks a character data type, so all single characters are strings. You may wish to peruse this example code which demonstrates that this function leaves forward slashes as well as escape sequences involving non-printable characters unaltered. Stripslashes() arose to counter excessive quoting caused by a feature that PHP formerly supported: magic quotes.

In the early days of PHP when Rasmus Lerdorf worked with databases that necessitated escaping the single quote character, he found a way to avoid the tedium of manually adding a backslash to quote or escape that character; he created magic quotes:

    ... useful when mSQL or Postgres95 support is enabled
    since ... single quote has to be escaped when it is 
    part of [a] ... query ... 

                        (See php.h in PHP/FI [php-2.0.1] )

While magic quotes saved developers from having to use addslashes(), this feature could automatically quote inappropriately. A form's input with a value of O'Reilly would display as O\'Reilly. Applying stripslashes() fixed that issue. Though stripslashes() lived up to its name and stripped away backslashes, it was inadequate for addressing all the problems associated with magic quotes, a feature that would ultimately be deprecated in PHP5.3 and then removed as of PHP5.4 (see Manual).

You may view quoted data if you are still using a version of PHP that supports magic quotes. For example, consider the case of a $_GET variable that originates from a JavaScript containing a url-encoded string, as follows:

location.href="next_page.php?name=O%27Riley"; // $_GET['name'] == O\'Riley 

If you were to apply to $_GET['name'] either addslashes() or mysqli_real_escape_string(), this variable's value would expand, containing two more backslashes, as follows:

O\\\'Riley

Suppose the variable were next used in an insert query. Normally the backslash of a quoted character does not get stored in the database. But, in this case, the query would cause data to be stored as:

O\'Riley

The need for stripslashes() is much less likely nowadays, but it's good to still retain it. Consider the following JavaScript, containing a flagrant typo:

location.href="http://localhost/exp/mydog.php
?content=My%20dog%20doesn\\\\\\\\\\\\\%27t%20
like%20to%20stay%20indoors."

Using stripslashes(), one may dynamically correct the code as follows:

function removeslashes($string)
{
    while( strpos( $string, "\\" ) !== FALSE ) {
      $string = stripslashes( $string );
    }
    return $string;
}

$text = htmlentities($_GET['content']);
if (strpos($text,"\\") !== FALSE ) {
    echo removeslashes( $text );
}

Note: stripslashes() is not recursive.

like image 39
slevy1 Avatar answered Oct 29 '22 18:10

slevy1