Security_helper
This Helper defines several security functions to sanitize user input and prevent security vulnerabilities such as XSS (Cross-Site Scripting), SQL Injection, and Directory Traversal attacks.
Prevent XSS
Converts special characters into HTML entities (e.g.,
<becomes<,>becomes>).Prevents malicious JavaScript from being injected into web pages.
function xss_clean($data) {
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}Example:
$user_input = "<script>alert('Hacked!');</script>";
$clean_input = xss_clean($user_input);
echo $clean_input;
// Output: <script>alert('Hacked!');</script>Clean Input Data to Prevent Attacks
Removes unnecessary spaces and backslashes.
Strips out single (
') and double (") quotes to prevent SQL Injection.Removes any special characters except letters, numbers, spaces, and punctuation.
Example:
Security Benefit: Protects against SQL Injection and XSS attacks.
Secure
These functions sanitize user input from URL parameters ($_GET) and form data ($_POST).
Example:
Secure URIs to Prevent Attacks
Removes dangerous characters from URLs.
Prevents directory traversal attacks by stripping out
..and....Allows only alphanumeric characters, underscores (
_), hyphens (-), and dots (.) in URL segments.
Example using uri_security():
Security Benefit: Prevents unauthorized file access and path traversal attacks.
Secure All $_GET Parameters
$_GET ParametersSanitizes all
$_GETkeys and values.Removes dangerous characters while keeping alphanumeric characters, underscores (
_), and dashes (-).Prevents GET parameter manipulation attacks.
Example using sget_security():
Security Benefit: Prevents parameter-based attacks.
Security_helper in Controller
Conclusion
These functions improve security by preventing XSS, SQL Injection, and Path Traversal attacks
Last updated
Was this helpful?