How can the retrieved IP address be stored in a database or text file for later use?

To store the retrieved IP address in a database or text file for later use, you can create a table in a database with a column to store IP addresses or simply write the IP address to a text file. When retrieving the IP address, you can insert it into the database using SQL queries or write it to a text file using file handling functions in PHP.

// Store IP address in a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve IP address
$ip_address = $_SERVER['REMOTE_ADDR'];

// Insert IP address into database
$sql = "INSERT INTO ip_addresses (ip_address) VALUES ('$ip_address')";

if ($conn->query($sql) === TRUE) {
    echo "IP address stored successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();