Signature
Parameters
This function takes no parameters.Return Value
The IP address of the client as a string (IPv4 or IPv6), or NULL if the IP cannot be determined
Description
Theclient_ip() function returns the IP address of the client making the request. This is useful for logging, rate limiting, geolocation, and security purposes.
Important: When running behind a reverse proxy, this function returns the proxy’s IP address, not the actual client IP. See the Reverse Proxy section below.
Examples
Log Connection Attempts
Record client IP addresses when users log in:Rate Limiting
Limit requests per IP address:IP-Based Access Control
Restrict access to specific IP ranges:Audit Trail
Maintain an audit log with IP addresses:Reverse Proxy Configuration
When running behind a reverse proxy (Nginx, Apache, Cloudflare, etc.), you need to read forwarding headers to get the real client IP:Get Real Client IP
Handle X-Forwarded-For
TheX-Forwarded-For header can contain multiple IPs (client, proxy1, proxy2, …):
Common Proxy Headers
| Header | Used By | Description |
|---|---|---|
X-Forwarded-For | Nginx, Apache | Comma-separated list of IPs |
X-Real-IP | Nginx | Single client IP |
CF-Connecting-IP | Cloudflare | Original client IP |
True-Client-IP | Akamai, Cloudflare | Original client IP |
X-Client-IP | Various | Client IP |
Security Considerations
Validating Proxy Headers
Warning: Proxy headers can be spoofed if not properly configured. Only trust these headers if:- Your reverse proxy is properly configured to set them
- The proxy strips any existing headers from the client
- Direct client connections are blocked (only proxy can reach SQLPage)
Example: Trust Proxy Headers Safely
Return Value Details
IPv4 Address
IPv6 Address
NULL Value
Returns NULL when:- Connection is through a Unix socket
- IP address cannot be determined
- No network connection info is available
Common Use Cases
- Security Logging - Track who accessed what
- Rate Limiting - Prevent abuse from single IPs
- Geolocation - Show region-specific content
- Access Control - IP whitelist/blacklist
- Fraud Detection - Identify suspicious patterns
- Analytics - Track visitor locations
- Session Management - Bind sessions to IP addresses
Related Functions
- sqlpage.header() - Read proxy forwarding headers
- sqlpage.request_method() - Get HTTP method
- sqlpage.path() - Get request path