1

I'm very new to PHP (Learning it at CodeAcademy) and I wanted to make a web browser kind of thing using an iframe. This is what I have so far:

<!DOCTYPE html>
<html>
<body>
<?php
$url = $_GET['url'];
print $url;
?>

<form name="input" method="get">
Url: <input type="text" name="url" action="<?php echo $url; ?>">
<input type="submit" value="Go">
</form>

<iframe src="<?php echo $url; ?>">
  <p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>

The block of code above is a .php file. My problem with this is that the iframe leads to the URL of my domain slash the URL you enter. A little better explanation is here.

I tried using a $partial = substr($url, x, y) and making the iframe lead to echo $partial so that the original domain is cut out and only the URL you entered is in the iframe URL. That didn't work. How can I fix this?

2 Answers 2

1

just check if the URL has a "http://" then proceed
else add a "http://" by your script

<!DOCTYPE html>
<html>
<body>
<?php
$url = $_GET['url'];
print $url;
if (strpos($url,'http') === 0) { //found at position 0
    //ok
} else {

    if ((strpos($url,'google') === 0) or
       (strppos($url,'some.other.site1') === 0) or 
       (strppos($url,'some.other.site2') === 0))  // here all the knowns SSL - Sites (this can not be the ultimate solution)
    {
        $url = 'https://'.$url;
    } else {
        $url = 'http://'.$url;
    }

}
print '<br/>'.$url; 
?>

<form name="input" method="get">
Url: <input type="text" name="url" action="<?php echo $url; ?>">
<input type="submit" value="Go">
</form>

<iframe src="<?php echo $url; ?>">
  <p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

This works for some websites, but not all. Are the websites that don't work (E.g. Google or YouTube) preventing this on their side? Thanks btw!
Your right. Google has SSL (https://) so you have to check these URLs too and handle it separately for each adress. Youtube works whith http:// but somehow does not display. -> I think YouTube will have a explanation. --> sorry have no better solution
1

whenever your input is something like "google.com" the iFrame is going to look in your current directory for a file named "google.com"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.