Do not create variables for nothing. You already have the DB connection variables, just use them. The db connection does not belong where you have it. You will end up duplicating it countless times. Put it in a config file accessible to the files that would need it. Additionally, do not output internal system errors to the user.
Whether you htmlencode the data on the way in or out, makes no difference to the database. A string is a string. Itâs processing the data to be stored.
I will grant you that it takes more space to store them encoded and may not be recommended, but thereâs nothing to say that it does not belong.
I agree, but with an addition. This would appear to be an API reference, given that the positive outcome is a JSON string. What I would say is be consistent in your output; if your âgoodâ output is a json, your âbadâ output should be too; whateverâs calling your page is expecting JSON as the result, so donât cause a failure in the fetching script as well.
Log the error somewhere (if itâs not already being logged) and report a descriptive-but-not-exposing negative outcome to the âuserâ.
True the database doesnât care, but the concept naively assumes the data will always be used in an HTML output context so I stand by what I said. htmlspecialchars is an OUTPUT function ONLY for use in an HTML context which you donât know what that will be UNTIL you output it. You donât change data when you save it.
this function is sufficient to prepare input for inclusion in most contexts of an HTML document
Not trying to beat a dead horse, but I agree. Thereâs that saying
Sanitize or validate on input, escape on output.
I donât know why people put it that way, it should just be âvalidate on input, escape on outputâ since I rarely see people sanitize at all.
People tend to confuse this process a lot. Itâs not really that complicated. When you validate, all youâre really doing is making sure the input has a set standard that you want. So for instance, a phone number. Do you think itâs appropriate to allow letters in your phone number? Not really. So thatâs where you validate and make sure the structure of the input matches a legitimate phone number structure. If not, you give them an error message telling them to correct the input. You shouldnât modify the users input at all. Thatâs why you let them do it themselves. That way, they know exactly what they put instead of guessing what they put.
Mostly because sanitization by and large has been absolved by improved/proper handling of queries and retrieval (Parameterizing the query to sanitize against injection, output escaping rendering output safe). Everything else sort of falls under the Validation umbrella. The phrase simply predates the more common adoption of these improved practices.
@spaceshiptrooper, @m_hutley is exactly correct. It is a leftover relic from the olden days before we had cell phones and Prepared Statements. âSanitationâ was the way us old guys used to do things. I still have some old databases with jacked up data caused by sanitation.