I have a django application, this application contains a form and i have another server which running php. In the PHP server, i created a html template which includes an form as iframe from my Django server.
When i tried to get PHP server's IP Adress, Django returns me the IP of Client. But i want to server's IP adress. Can anyone to help me? I shared the middleware codes which i use for get server's IP Address below.
from django.http import HttpResponseForbidden
import time
class IframeHostMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
allowed_hosts = [
'172.16.3.55',
'111.111.111.111',
'localhost',
]
headers_order = [
'HTTP_X_REAL_IP', # Standard header for the real IP
'HTTP_X_FORWARDED_FOR', # Forwarded IP (could be a list)
'HTTP_X_CLIENT_IP', # Alternate header for client IP
'HTTP_CF_CONNECTING_IP', # Cloudflare header for client IP
'HTTP_X_CLUSTER_CLIENT_IP', # Other standard header for client IP
'HTTP_FORWARDED', # Standard forwarded header (RFC 7239)
'REMOTE_ADDR', # Fall back to the web server's connection
]
ip = None
for header in headers_order:
value = request.META.get(header)
if value:
# If it's the X-Forwarded-For header, it could contain multiple IPs
if header == 'HTTP_X_FORWARDED_FOR':
value = value.split(',')[0]
ip = value.strip()
break
referer = request.META.get('HTTP_X_FORWARDED_FOR')
client = request.META.get('REMOTE_ADDR')
print(f"Iframe Requested from (IP): {ip}")
if not ip or not any(ip.startswith(host) for host in allowed_hosts):
print("Iframe Status: Deny")
return HttpResponseForbidden('Access denied')
print("Iframe Status: Accept")
return self.get_response(request)