-2

The reason I ask this question is because I was checking stackoverflow for answer, and since 2012/13 it no longer seems to be a hot topic and all the answers documentation is deprecated. Could you please tell me if we still should be doing this and if so what's a secure way to do so? I'm specifically talking about user defined post data...

Update: the string will be html inputted from user and posted into my dB.

Wickey312
  • 566
  • 5
  • 15
  • Linking the 2012/2013 question can help provide context. There's many types of escaping that you can do on strings. – apokryfos Jun 25 '17 at 08:48
  • Possible duplicate of [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – vascowhite Jul 02 '17 at 19:33

4 Answers4

2

The short answer is yes. Even in 2017 you should be escaping strings in PHP. PHP does not do it by itself because not every developer will want to develop a product / functionality that needs to escape user input (for whatever that reason may be).

If you are echoing user inputted data to a webpage, you should use the function htmlspecialchars() to stop potential malicious coding from executing upon being read by your browser.

When you are retrieving data from a client, you can also use the FILTER_INPUT functions to validate incoming data to validate that the clients data is actually the data you want (e.g checking that no one has bypassed your client side validation and has entered Illegal characters into the data)

From my experience these are two great functions that can be used to 1:) escape output to a client and 2:) prevent the chance of malicious code being stored/processed on your server.

MinistryOfChaps
  • 1,458
  • 18
  • 31
0

It depends entirely on what you are going to do with the string.

If you are going to treat it as code (whether that code is HTML, JavaScript, PHP, SQL or something else) then it will need escaping.

PHP is not able to tell if you trust the source of the data to write safe code.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I'll edit the comments.. The string will work 2 ways.. Passed into my dB and passed back as html to show the user.. So how do I escape it is my q? – Wickey312 Jun 25 '17 at 08:04
  • Look at [the most upvoted PHP questions](https://stackoverflow.com/questions/tagged/php?sort=votes) … number 2 is [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Quentin Jun 25 '17 at 08:05
0

Yes, escaping the strings from the request (and therefore imputable by the user) is a practical requirement because PHP makes available the data actually added to the payload of the request without any modification that could invalidate the data itself (not all the data needs Of escaping), so any subsequent processing on that data must be made and under the developer's control.

The escape of variables in database interaction operations to prevent SQL Injections.

In past versions of PHP there was the "magic_quoteas" feature that filtered every variable in GET or POST. But it is deprecated and is not a best practice. Why Not?

The state of the art in querying DB is predominantly in using the PDO driver with the prepared statement. At the time the variable is bound, the variable will be escaped automatically.

$conn->prepare('SELECT * FROM users WHERE name = :name');
$conn->bindParam(':name',$_GET['username']); //this do the escape too
$conn->execute();

Alternatively, mysql_real_escape_string manages it manually.

Alternatively, mysqli::real_escape_string manages it manually.

Jackie Degl'Innocenti
  • 1,251
  • 1
  • 14
  • 34
  • The mysql_* api has been deprecated for a while now, so it shouldn't be recommended in new answers, hence my -1. – vascowhite Jun 25 '17 at 09:02
  • My main suggestion is to use PDO, but about manual escaping, mysql_real_escape_string was deprecated in PHP 5.5.0, so just a minor ago... Btw my fault, the current way to do that is mysqli::real_escape_string() http://php.net/manual/en/mysqli.real-escape-string.php. I also update my answer. – Jackie Degl'Innocenti Jun 25 '17 at 13:11
0

In 2017 this is what is usually done in the scenario you describe:

  1. The user inputs text in a form, the text is sent to the server, before that the text is url encoded (this is one form or escaping). This is typically done by the browser/javascript so no need to do it manually (but it does happen).

  2. The server receives the text, decodes it and then creates a MySQL insert/update statement to store it in the database. While some people still run the mysqli_real_escape_string on it, the recommended way is to use prepared statements instead. Therefore in this aspect you do not need to do the escaping, however prepared statements delegate escaping to the database (so again escaping does happen)

  3. If the user inputted text is to be presented back on a page then it is encoded via htmlentities or similar (which is itself another form of escaping). This is mostly ran manually although most new view template frameworks (e.g. twig or blade) take care of that for us.

So that's how it is today as far as I know. Escaping is very much required, but the programmer actually doing it is not so much a requirement if modern frameworks and practices are used.

apokryfos
  • 38,771
  • 9
  • 70
  • 114