What is the purpose of using stripslashes(addslashes($var)) in PHP code?
When dealing with user input in PHP, it is important to sanitize the data to prevent SQL injection attacks. Using addslashes() adds escape characters to special characters in a string, while stripslashes() removes these escape characters. By using stripslashes(addslashes($var)), we can ensure that the user input is properly sanitized and safe to use in SQL queries.
$var = "User's input with special characters";
$var_sanitized = stripslashes(addslashes($var));
// Now $var_sanitized can be safely used in SQL queries
Keywords
Related Questions
- What are the best practices for handling form validation errors in PHP without unnecessary redirection?
- How can the code snippet be optimized for better readability and maintainability?
- How can PHP scripts be modified to prevent extra spaces from being added to the end of values during CSV import into MySQL?