2014-05-04 78 views
1

我对PHP很陌生(在CodeAcademy学习),我想用iframe做一个网页浏览器。这是我到目前为止:如何使用PHP变量作为iframe元素的URL

<!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> 

上面的代码块是一个.php文件。我的问题是,iframe会导致我的域名的URL输入URL。更好的解释is here

我试图用一个$partial = substr($url, x, y),并使得IFRAME导致echo $partial使原来的域名被切断了,只有你输入的URL是在iframe网址。这没有用。我怎样才能解决这个问题?

回答

1

只是检查网址有一个“HTTP://”,然后继续
别人添加的“http://”,由你的脚本

<!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> 
+0

这适用于某些网站,但不是全部。不工作的网站(例如谷歌或YouTube)是否阻止了这一点?谢谢btw! – GShocked

+1

你的权利。谷歌有SSL(https://),所以你也必须检查这些URL,并为每个地址单独处理。 YouTube的作品whith http://但不知何故不显示。 - >我认为YouTube会有一个解释。 - >抱歉没有更好的解决方案 – ratmalwer

1

,只要您输入的是一样的东西“google.com “iFrame将在当前目录中查找名为”google.com“的文件。

+0

看看代码,它解释更多。 – GShocked