How to use a PHP variable as an URL for an iframe element

如何使用PHP变量作为一个IFRAME元素一个URL

标签: php forms url iframe

问题 (Question)

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?

我对PHP非常新的(学习在codeacademy)我想做一个Web浏览器之类的使用iframe。这是我到目前为止:

<!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缩短你输入网址。解释好一点在这里

我试着用一$partial = substr($url, x, y)使iframe导致echo $partial因此,原始域裁剪、只输入的URL在IFRAME网址。没有工作。我怎样才能解决呢?

最佳答案 (Best Answer)

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>

检查一下是不是有一个“HTTP URL:/ /”然后
另外加上一个“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>

答案 (Answer) 2

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

当您输入的是什么“谷歌。COM“iframe要看你的当前目录的一个名为“谷歌COM文件。”

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23451162