Reputation: 3
I tried to make a script where when a user types in the url and presses Go, it will go to that web page. I made it, but I noticed that it only worked with
http://
in front of it. So i used the strpos function to check whether
http://
is in the string and if it isn't it will add it to the url and go to the website. It works but now when a user types in the url with 'Http://' it will double it so it would be something like:
'http://http://www.example.com'.
How can I fix this? This is the code:
<?
if ($_POST['submit']) {
$urlgo = $_POST['url'];
if (strpos($urlgo,'http://') == false) {
$url = "http://$urlgo";
header("Location: $url "); /* Redirect browser */
} else {
$urlgo = $_POST['url'];
header("Location: $urlgo "); /* Redirect browser */
exit;
}
}
?>
Upvotes: 0
Views: 22
Reputation: 360762
if (strpos($urlgo,'http://') == false) {
is the problem. strpos
can return a LEGITIMATE 0
if the "needle" string (http://
) is at the start of the "haystack" ($urlgo
). 0 == false
is TRUE in PHP. You need to use the strict comparison operator:
if (strpos(...) === false) {
(note, 3 =
signs) which compares type AND value. 0 === false
is FALSE in php.
Upvotes: 1