What are the differences between public and private IP addresses and how do they affect networking?

Public IP addresses are used to identify devices on the internet, while private IP addresses are used within a local network to identify devices. Public IP addresses are unique and assigned by the Internet Service Provider (ISP), while private IP addresses are typically assigned by a router using the Dynamic Host Configuration Protocol (DHCP). When devices communicate over a network, they use both public and private IP addresses to route data between them.

// Get the public IP address of the server
$public_ip = $_SERVER['SERVER_ADDR'];

// Get the private IP address of the client
$private_ip = $_SERVER['REMOTE_ADDR'];

echo "Public IP Address: " . $public_ip . "<br>";
echo "Private IP Address: " . $private_ip;