What is the best practice for ensuring that website addresses entered in a form always include "http://" in PHP?

To ensure that website addresses entered in a form always include "http://" in PHP, you can use a simple validation function to check if the address starts with "http://" or "https://". If it does not, you can prepend "http://" to the address before processing it further.

// Function to ensure website addresses include "http://"
function validateWebsiteAddress($url) {
    if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
        $url = 'http://' . $url;
    }
    return $url;
}

// Example usage
$websiteAddress = 'example.com';
$websiteAddress = validateWebsiteAddress($websiteAddress);
echo $websiteAddress;