Introduced in SQLPage 0.33.0.

Returns the IP address of the client making the HTTP request.

Example

insert into connection_log (client_ip) values (sqlpage.client_ip());

Details

The function returns:

  • The IP address of the client as a string
  • null if the client IP cannot be determined (e.g., when serving through a Unix socket)

⚠️ Important Notes for Production Use

When running behind a reverse proxy (e.g., Nginx, Apache, Cloudflare):

  • This function will return the IP address of the reverse proxy, not the actual client
  • To get the real client IP, use sqlpage.header: sqlpage.header('x-forwarded-for') or sqlpage.header('x-real-ip')
    • The exact header name depends on your reverse proxy configuration

Example with reverse proxy:

-- Choose the appropriate header based on your setup
select coalesce(
    sqlpage.header('x-forwarded-for'),
    sqlpage.header('x-real-ip'),
    sqlpage.client_ip()
) as real_client_ip;

For security-critical applications, ensure your reverse proxy is properly configured to set and validate these headers.