4

Hi I have some PHP script that show country flag based on visitors 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>";
}

Then in my index page I did

index.php

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

When I run the script alone it shows flag just fine, but when I want to show it on another page it doesn't work.

Here is the script and here is index page with broken image.

Philio
  • 3,675
  • 1
  • 23
  • 33
LaCalienta
  • 43
  • 2
  • 13

3 Answers3

1

You shouldnt include your php file as the src attribute in the img tag, just include the php-file (and it will echo the img tag)

index.php

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

Alternatively make your flags.php read and echo the entire flag-image. Or make it echo the url (but then you can just run it as a function and dont need to keep it as a separate file)

BobbyTables
  • 4,481
  • 1
  • 31
  • 39
0

It's because of what the flags.php file actually outputs and because the paths are right for the included file but not the index.php

It is creating an entire img tag, whereas in the index file you are using it as just the src attribute.

Change flags.php to $file_to_check="flags/flags/$two_letter_country_code.gif"; if (file_exists($file_to_check)){ print $file_to_check"; }else{ print "flags/flags/noflag.gif"; }

Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23
  • it doesn't work it returns: flags/SI.gif when I visit script alone but when I visit index page it's still broken image (I wonder how they did this here: http://www.shorter.in/#flag) – LaCalienta Sep 04 '15 at 11:58
  • Ah, then that's down to the paths being wrong. I've edited my answer to correct for that, but if you want it to work on both files you'll need to use absolute paths for the image files. – Geoff Atkins Sep 04 '15 at 12:34
0

Flags.php returns the img tag, not the src.

In index.php you must not add <img src="http://ip.dxing.si/flags/flags.php">, but just http://ip.dxing.si/flags/flags.php

Cosmin
  • 1,482
  • 12
  • 26