将PHP页面显示为图像

时间:2015-09-02 12:54:27

标签: php ip country

您好我有一些PHP脚本根据访问者IP

显示国家/地区标志

flags.php

<?
require 'ip.php';
$ip=$_SERVER['REMOTE_ADDR'];
$two_letter_country_code=ip_info("$ip", "Country Code");

//header('Content-type: image/gif');
$file_to_check="flags/$two_letter_country_code.gif";
if (file_exists($file_to_check)){
    print "<img src=$file_to_check width=30 height=15><br>";
}
else{
    print "<img src=flags/noflag.gif width=30 height=15><br>";
}

然后在我的索引页面中我做了

的index.php

<img src="http://ip.dxing.si/flags/flags.php">

当我单独运行脚本时,它显示标记就好了,但是当我想在另一个页面上显示它时它不起作用。

Here是脚本,here是索引页面,图片已损坏。

3 个答案:

答案 0 :(得分:1)

你不应该将你的php文件包含在img标签中的src属性中,只需包含php文件(它将回显img标签)

的index.php

include 'flags.php'; //this will echo the entire img-tag

或者让你的flags.php读取并回显整个标志图像。或者让它回显url(但是你可以将它作为一个函数运行,而不需要将它作为一个单独的文件保存)

答案 1 :(得分:0)

这是因为flags.php文件实际输出的内容,因为路径适用于包含的文件而不是index.php

它正在创建一个完整的img标记,而在索引文件中,您只将其用作src属性。

将flags.php更改为     $ file_to_check =&#34;标志/标志/ $ two_letter_country_code.gif&#34 ;;     if(file_exists($ file_to_check)){                 print $ file_to_check&#34 ;;                 }其他{                 print&#34; flags / flags / noflag.gif&#34 ;;                 }

答案 2 :(得分:0)

Flags.php返回img标签,而不是src。

在index.php中,您不得添加<img src="http://ip.dxing.si/flags/flags.php">,而只需添加http://ip.dxing.si/flags/flags.php

相关问题