Overview
S-PHP provides a collection of helper functions to simplify common development tasks such as asset management, security, redirects, and environment configuration.Asset Management
asset()
Generates a URL path for public assets.The relative path to the asset file
The full path to the asset prefixed with
/public/Usage Example
Security
sanitizeHtml()
Sanitizes HTML input by removing dangerous content while preserving safe tags.The input to sanitize. Can be a string or array of strings.
The sanitized input with dangerous content removed
Allowed Tags
<p>, <br>, <b>, <strong>, <i>, <em>, <ul>, <ol>, <li>, <a>, <img>, <blockquote>, <span>, <div>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>
Usage Example
Features
- Removes
<script>and<style>tags - Strips event handlers (onclick, onload, etc.)
- Removes inline styles
- Blocks dangerous protocols (javascript:, data:) except for valid base64 images
- Recursively sanitizes arrays
csrf()
Generates and outputs a hidden CSRF token input field.Echoes a hidden input field containing the CSRF token
Usage Example
validateCsrfToken()
Validates a CSRF token against the session token.The CSRF token to validate (typically from form submission)
Returns
true if the token is valid, false otherwiseUsage Example
Routing
redirect()
Redirects the user to a specified URL with an optional flash message.The URL to redirect to
Optional flash message to store in the session
Sends a Location header to redirect the browser
Usage Example
Environment Configuration
loadEnv()
Loads environment variables from a .env file.The path to the .env file
Returns
true on success, or calls dd() if the file is not found.env File Format
Usage Example
env()
Retrieves an environment variable value.The environment variable name
The default value to return if the variable is not set
The environment variable value or the default value
Usage Example
Debugging
dd()
Dump and die - outputs a variable’s contents and terminates script execution.The variable to dump (can be any type, not just arrays)
Outputs the variable contents in a formatted
<pre> block and stops executionUsage Example
Output Example
Notes
- All helper functions are globally available without needing to import or use namespaces
- CSRF tokens are stored in the session and should be validated on all state-changing requests
- The
sanitizeHtml()function is designed for content that needs to preserve some HTML formatting while being safe from XSS attacks - Always use
env()to access environment variables rather than directly accessing$_ENVor$_SERVER