PHP

How do I determine the client IP type (IPv4/IPv6) in PHP

CK CK 3788 
Created at
Updated at  
835 0 0

In the PHP, there are filters which can determine IPv4 and IPv6: FILTER_VALIDATE_IP, FILTER_FLAG_IPV4, FILTER_FLAG_IPV6.

Below code shows how to determine the client protocol:

<?php
// Get the client's IP address
$clientIP = $_SERVER['REMOTE_ADDR'];

// Check if it's an IPv6 address
if (filter_var($clientIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    echo "Client is using IPv6";
} 
// Check if it's an IPv4 address
elseif (filter_var($clientIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    echo "Client is using IPv4";
} 
// If it's neither IPv4 nor IPv6
else {
    echo "Unknown IP address type";
}

This code snippet checks whether the client's IP address is IPv4 or IPv6 using filter_var() function with FILTER_VALIDATE_IP flag. If it's a valid IPv6 address, it prints "Client is using IPv6". If it's a valid IPv4 address, it prints "Client is using IPv4". If it's neither IPv4 nor IPv6, it prints "Unknown IP address type".

Tags FILTER_FLAG_IPV4 FILTER_FLAG_IPV6 FILTER_VALIDATE_IP IPv4 IPv6 REMOTE_ADDR Facebook X
Comments 0
Similar posts
  1. How do I determine the client IP type in Python - IPv4 or IPv6
    763
  2. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,073
  3. Enable/Disable IPv4 on Windows
    1,288
  4. Enable/Disable IPv6 on Windows
    1,736
  5. Public DNS (Domain Name Service) based on IPv4, IPv6 widely used
    1,244
  6. How to ban / block IP addresses
    512
  1. Google Single Sign-On implementation with PHP
    1,374
  2. PHP Superglobal Variables do not require additional syntax to access it
    2,638
  3. Run Python Code with Virtual Environment in PHP
    674
  4. PHP Simplified Operators
    318
  5. PHP Heredoc (aka Nowdoc)
    439
  6. PHP Post Processing by disconnecting HTTP connection and continuing the remaining logics
    1,810
  7. WordPress Remote Posting with PHP
    1,687
  8. Google BigQuery Example to converted Nested FIelds in JSON
    459
  9. Verifying Apple In-App Purchase validation receipt with PHP
    1,787
  10. Useful code for file-based Unit Test in PHP
    500
  11. Function Overloading Implementation in PHP with Magic Mathods
    1,757
  12. PHP Magic - the Null Coalescing Operator (??) - return the value if the give variable is NULL
    1,672
  13. How do I find php.ini on Mac?
    493
  14. Mastering Excel Data Importation in PHP
    555
  15. Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file
    977
  16. What is 302 Found Redirection in HTTP 1.1?
    492
  17. Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records
    620
  18. Building a Basic DNS Server in PHP/Python: A Beginner's Guide
    739
  19. CentOS 7 PHP Apache Kafka: Installation, Examples, and System Diagram
    307
  20. Apache Kafka Explained: Powering Netflix, Building with PHP
    493
  21. Array of arguments passed to script in PHP
    90
  22. Bob Jenkins' One-At-A-Time hashing algorithm in PHP
    54
  23. 32bits simple hash function in PHP
    40
  24. PHP Code Compression Tool by removing whitespace, strips comments - PHP Minifier/Compressor
    1,415
  25. Setup Apache+PHP+MySQL on Centos 6.7
    605
  26. mcrypt missing when using phpMyAdmin on Centos 6.7
    550
  27. PHP Code can know the current OS is windows or not
    584
  28. Checking similarity between two strings in PHP
    675
  29. Simple PHP calendar
    479
  30. Simple PHP code performance measuring example
    793
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,044
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    158
  7. Invalid Auction Storms in Ad Tech
    98
  8. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  9. The Two Clocks of Ad Server Architecture
    71
  10. Frequency Cap Architecture Overview
    515
  11. Collecting Proof-of-Play from 100,000 Screens
    146
  12. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  13. What is RedisVL?
    109
  14. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  15. TPU & GPU Cluster Management Tools
    245
  16. The Concept of Scaffolding in AI Agents
    234
  17. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  18. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  19. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  21. Machine Learning: Training vs. Ranking
    222
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  23. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  24. Designing a Data Curation System Around a Vector Database
    221
  25. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    257
  26. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  27. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  28. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  29. Understanding Data Lineage
    263
  30. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    167