In general, the essence is as follows:

through PHP, I take a record out of the database, sorting them by "ASC" view count

SELECT src, name FROM `image` ORDER BY `shows` ASC LIMIT 1 

And here I, add this record 1 view

 UPDATE `image` SET `shows` = shows + 1 WHERE `id` = :id; 

Everything is wonderful, displays 1 entry and adds 1 view to it.

But the problem is this, if I have an img tag in the markup with the wrong src attribute, I have a request to add a view 2 times, and it’s not 1 and 2.

Why it works so - I do not understand.

So if in the markup - adds 1 view

 <img src="<?php echo $result['src']; ?>" alt=""> 

And if it's worth it, it adds 2 views.

 <img src="<?php echo $result['src']; ?>" alt=""> <img src="fsdfsd" alt=""> 

How it works?

  • What is the script path to the image demonstration file and what path is inserted into the src attribute - do they coincide? If they match, the browser is trying to get the images, once again jerking your script and UPDATE is triggered two times. - cheops
  • Perhaps you have all requests go through index.php and there as it does not redirect to 404 but a second request is set up if the legal entity is not found - Ivan Turcan
  • Perhaps at 404 reologs, but still it’s not clear ... - user206303

1 answer 1

Go to the browser at http://yoursite.com/fsdfsd and refresh the page - additional views will also be charged. Because it shows page 404 Not Found , which is also the page that displays the views.

It is better not to show pictures if they are not:

 <img src="<?php echo sanitize($result['src']); ?>" alt=""> 

And in the sanitize function, delete invalid characters and check for the presence of a file ( file_exists or is_file ). Because otherwise someone can add a picture test.jpg"><script>alert('Я взломал ваш сайт');</script><img .

More about the XSS attack - on Wikipedia .

If there is no file, then return the link to the dummy picture or output an inline transparent pixel:

 data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7