From 9b3abbbbe1c89f6809d3753e1fe4b612cba6c528 Mon Sep 17 00:00:00 2001 From: Carl Alexander Date: Mon, 18 Sep 2023 12:21:42 -0400 Subject: [PATCH] update cloudflare guide with code to have the correct `REMOTE_ADDR` --- docs/guides/cloudflare.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/guides/cloudflare.md b/docs/guides/cloudflare.md index a3a7fb4..c1b165a 100644 --- a/docs/guides/cloudflare.md +++ b/docs/guides/cloudflare.md @@ -68,6 +68,21 @@ You'll then want to add these DNS records to Cloudflare like this: ![Cloudflare DNS records](../../images/cloudflare-dashboard-dns-records.png) +## Configuring WordPress to have the correct remote address + +When using Cloudflare, WordPress will be unable to detect the proper IP address for HTTP requests. `REMOTE_ADDR` will always be pointing to a Cloudflare IP address. This is a problem when tracking IPs for spam, bot detection or any other security scenarios. + +To update `REMOTE_ADDR` to have the proper request IP address, add the following to your `wp-config.php`: + +```php + +if (!empty( $_SERVER['HTTP_CF_CONNECTING_IP'])) { + $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; +} +``` + +This will replace `REMOTE_ADDR` with the IP address that Cloudflare received the request from. + [1]: ../reference/configuration.md#cdn [2]: https://cloudflare.com [3]: https://workers.cloudflare.com/