svgmx5 Posted August 18, 2011 Share Posted August 18, 2011 My current project requires me to save the number of views an image (banner ad) has appeared on users website. The problem here is that the image will be appearing on several users website and i want to be able to save the number of times it has appeared on my database. I know how to do this if the image was on the same server as the database, but not when the image and database are on completely different servers. This is somewhat like analytic where it tracks the visitors to a page. Does anyone how i can accomplish this? i figured i would need to create a JavaScript code to provide to the user so they can place it on the site but how do i get that code to connect to my database? Quote Link to comment https://forums.phpfreaks.com/topic/245148-save-number-of-views-of-an-image-on-database/ Share on other sites More sharing options...
TeNDoLLA Posted August 18, 2011 Share Posted August 18, 2011 You can get the JS script to connect your server/db with AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/245148-save-number-of-views-of-an-image-on-database/#findComment-1259155 Share on other sites More sharing options...
Zane Posted August 18, 2011 Share Posted August 18, 2011 You would need for the clients to access your images through a PHP script... that acts as the image src For instance, you clients won't have an image path, but a path to a PHP script with query variable in it. Let's say you have a PHP file on your server called.... advert.php if(isset($_GET['src']) { $imagepath=$_GET['src']; /* Here you could have either an index number or a filename.. whatever you have in your database. The point is that you use what is in the $_GET['src'] variable to return an image object. */ $image=imagecreatefromjpeg($imagepath); // get image height $imgheight=imagesy($image); //allocate color for image caption (white) $color=imagecolorallocate($image, 255, 255, 255); //Add text to image bottom imagestring($image, 5, 100, $imgheight-50, "September 2005", $color); header('Content-Type: image/jpeg'); imagejpeg($image); } ?> Then, you can give your client an img source of something like http://mysite.com/advert.php?src=bigoleadvertisement Or if you're using the index way, http://mysite.com/advert.php?src=555 Quote Link to comment https://forums.phpfreaks.com/topic/245148-save-number-of-views-of-an-image-on-database/#findComment-1259157 Share on other sites More sharing options...
svgmx5 Posted August 22, 2011 Author Share Posted August 22, 2011 but how could i achieve this using a javascript script? Quote Link to comment https://forums.phpfreaks.com/topic/245148-save-number-of-views-of-an-image-on-database/#findComment-1260435 Share on other sites More sharing options...
codefossa Posted August 22, 2011 Share Posted August 22, 2011 but how could i achieve this using a javascript script? You would still need to use PHP because JS cannot interact with the server. Create a PHP page that adds one to the database column, and echos an image. In JQuery, you would simply use the load() function to call the PHP page. This would put the contents of that page into the container of your choice, and would run the PHP script to add the +1. Little side note. If you echo anything else on that page, it will show up when you load() the contents, so I would make sure you only put the image. Quote Link to comment https://forums.phpfreaks.com/topic/245148-save-number-of-views-of-an-image-on-database/#findComment-1260453 Share on other sites More sharing options...
svgmx5 Posted August 22, 2011 Author Share Posted August 22, 2011 Alright, so after a little more digging around i found a good example of this and was able to get it to work. Thanks to everyone who gave me some guidance and help! Quote Link to comment https://forums.phpfreaks.com/topic/245148-save-number-of-views-of-an-image-on-database/#findComment-1260638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.