Re: stripslashes etc.
| From: | El Bekko | Date: | Thu, 02 Mar 2006 20:53:37 +0000 |
| Subject: | Re: stripslashes etc. | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-41371@lists.php.net to get a copy of this message | ||
Andrew Darby wrote:
Hello, all. I'm getting a bit confused figuring out the best way to insert data into a db via web forms and then pull it out again safely. I know there's lots of information out there, but I was hoping someone could suggest a current, [easy!] best practice. Specifically, I have the following surely common set of situations: 1. user submits info to db<?php $text = addslashes(htmlspecialchars($text)); ?> When extracting, use this: <?php $text = stripslashes($text); ?> htmlspecialchars does this:--how best to screen out html / escape special characters on insert2. info is publicly displayed-- how best to unescape special characters for display3. user edits their submission in form populated with their existing data--again, to screen, but not have the escape characters multiply crazily4. user updates db--again, without additional of exciting new escape charactersI'm getting a bit mixed up through all the stages, and if someone would take pity and walk me through this, I, um, would thank you sincerely. TIA, Andrew When inserting, use this:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'addslashes does this: Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte). stripslashes strips all slashes added by addslashes.