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 &lt;, > becomes &gt;).

  • 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: &lt;script&gt;alert('Hacked!');&lt;/script&gt;

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

  • Sanitizes all $_GET keys 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

circle-check

Last updated

Was this helpful?