You may ask yourself, why do I need to check if a url contains http://?. The answer is that when you are dealing with a script that calls for user to enter http:// with their url, not everyone will. This will in turn cause broken links and images. We can fix that with this simple tutorial.
Steps :
Let's say we have a form that requires users to enter a url to their avatars[image]. We could use a method to strip http:// before entering it into the database, then re-add it after but why go through so much codes. Let them enter what they want then use the code below to check if the http:// is there and if not then add it.
We run our database query and have our url in a variable $avatar . Now let's check if that variable contains the http:// .
<?
$avatar = $row['myurl']; //URL Taken from database
if (substr($avatar, 0, 7) != 'http://') {// check the first 7 characters of the variable to see if they are h t t : / / [7].
$url = "http://".$avatar.""; //If http:// does not exist we add it to the url.
}else{ // Since the url already contains the http:// we use it as is.
$url = $avatar;
}
echo '<img src="$url" border="0">'; //Show the image
?>
Simple, wasn't it. you can apply this simple method to other things than url.