Page 1 of 1

Parsing PHP through an <img> tag

Posted: Mon Nov 22, 2004 5:38 pm
by Archy
OK, I am still having problems with this, I have tried and tested numerous scripts which I have made, and google'd for suggestions, ideas, all to no avail.

All I require is a small script that will display an image on any extension page (PHP, ASP, HTML, HTM etc) which will increment a database. Because of this, I am unable to actual PHP as people may be using free sites which do not allow PHP to be parsed, thus causing error's.

I know I need to start it like this:

Code: Select all

<img src="http://www.domain-name.com/pictureScript.php">
Although I do not know what to add in pictureScript.php to get the image to show.

Any help would be greatly appreciated.
Thanks,

Archy.

Posted: Mon Nov 22, 2004 5:45 pm
by timvw
they can include the image as you showed it....
but in that case the server domainname has to support php.



we have various clients that have an
<img src="http://example/banner.php" /> tag in their site


and we at http://example have a script banner.php that goes like

Code: Select all

/*
 * Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
 * Update: 2004-04-12 03:44
 *
 * This script shows a banner, based on the country associated with the visitor's address.
 *
*/

// we use this to initialise a connection with our database
require_once(FRAMEWORK_ROOT.'mysql.php');

// at the maxmind.com site you find a php driver, geoip.inc
require_once(FRAMEWORK_ROOT.'geoip.inc');

// lookup the country for the visitor's address
$address = $_SERVER['REMOTE_ADDR'];
$gi = geoip_open(FRAMEWORK_ROOT.'GeoIP.dat',GEOIP_STANDARD);
$country = geoip_country_code_by_addr($gi,$address);
geoip_close($gi);

//  lookup the url for the country
$result = mysql_query("SELECT url FROM partnerbanners WHERE country='$country'");
if ($row = mysql_fetch_assoc($result)) {
  // we have found an url for the country
  $url = $row['url'];
} else {
  // we haven't found an url for the country, so we lookup the default url
  $result = mysql_query("SELECT url FROM partnerbanners WHERE country='DEFAULT'");
  $row = mysql_fetch_assoc($result);
  $url = $row['url'];
}

// redirect to the url
header('Location: '.$url);

Posted: Mon Nov 22, 2004 5:52 pm
by Archy
So, that script basically redirects to a .jpg, .gig etc file?

Edit: Thank you, it works very well : )

Posted: Mon Nov 22, 2004 8:10 pm
by josh
If you want to do it without redirecting:

image.php

Code: Select all

<?php
header('Content-type: image/jpg');
readfile("file.jpg");
?>

Then:
<img src="image.php"> would show file.jpg!

Posted: Tue Nov 23, 2004 3:11 am
by timvw
That is exactly why i wrote a script with redirection in it...

Because all other scripts seemed to lookup content-type and then output the data, and i didn't want to spend my time on looking up the content-type ;)

Posted: Tue Nov 23, 2004 6:10 am
by Archy
Hmm, I was having a small problem with using the redirection script. As I am linking to a page that updates a database with every hit the script used, I have found that readfile() works best. However, I have hit a snag.

Once testing the script, I noticed that the database does not update when used in Firefox, but it does in IE, and Netscape (havent tested other, but I assume they will work for now). The code that I am using is below:

Code: Select all

<?PHP
header("Content-type: image/gif");

include('connection.php'); // Include connection to database

// Update the hits
$sql = "UPDATE database SET hits=hits+1 WHERE num='$num'";
$rs = mysql_query($sql) or die(mysql_error());

// Get the right image to display
$sql2 = "SELECT banner FROM database WHERE num='$num'";
$rs2 = mysql_query($sql2) or die(mysql_error());

$get = mysql_fetch_assoc($rs2);
     $banner = $get['banner'];


switch ($banner) {
     case '1' : readfile("file1.gif");
              break;
     case '2' : readfile("file2.gif");
              break;
     default : readfile("file3.gif");
               break;
}
?>
I get the banner fine, the image shows perfectly, but for some reason, the database does, as I said, not update.

Does anybody have any idea's why this might be?

Thanks,
Archy

Posted: Tue Nov 23, 2004 6:40 am
by John Cartwright
Not sure if firefox is more greedy on queries, I know this is a long shot but try

Code: Select all

$sql = "UPDATE `database` SET `hits`= (hits+1) WHERE `num` = '$num'";

Posted: Tue Nov 23, 2004 10:24 am
by Archy
Hmm, I have just got home and tried it out on my version of Firefox (Version 1.0) and it works fine. The version in University is 0.9.2, so it may have been a previous bug which has been fixed.

Thanks for all your help,
Archy

Posted: Thu Nov 25, 2004 8:12 pm
by josh
hmm that is really wierd the browser should not have anything to do with how the PHP/SQL is parsed, if anything I would guess that the image would not display at all if there was an error.

hmm strange