Problem: there is a page view counter, when you enter it (suppose there were 9 views on it before my entry), the counter is updated and puts 10, with the next update it is not updated (as it was intended by me), but there are social buttons below and when you click publish in my world, or facebook, or twitter (no difference), it winds up the counter by 1 and how many times you click publish, it will increase by so much. Checked session id (session does not change). I provide the code below, it may be necessary to somehow write a counter in a different way or to make information differently, wait for a detailed answer with an explanation, thank you all! (in advance).

But actually the code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <!--Авторизационное меню --> <? session_start(); ///////////////////////////////////Файл db.php////////////////////// $db = mysql_connect("localhost","xxxxx","xxxxxxx"); mysql_select_db("xxxxxxx",$db); /////////////////////////////////////////////////////////////////// if (isset($_GET['id'])) {$id = $_GET['id'];} if (!isset($id)) echo("<script>top.location.href='/';</script>") ; ?> <?php $result = mysql_query("SELECT title,date,author,view,id,text FROM news WHERE id='$id'",$db); if (!$result) { echo "<p>Запрос из базы данных не прошел!<br> Код ошибки:</p>"; exit (mysql_error()); } if (mysql_num_rows($result) > 0) { $myrow = mysql_fetch_array($result); if(!isset($_SESSION['view'][$id])){ $views=$myrow['view']; $views++; mysql_query ("UPDATE news SET view = ".$views." WHERE id='".$id."'"); $_SESSION['view'][$id] = $id; } else{ if(isset($_SESSION['view'][$id])){ $views=$myrow['view']; $_SESSION['view'][$id] = $id; } } } else { echo "<p>Информация не может быть извлечена</p>"; exit(); } printf ("<table style='border-radius:10px; border:0px solid #f2efef;' border='0' align='center' class='knews'> <tr class='news_title_head'><td> <div class='news_name'><span class='link_big'>%s</span></div> <div class='post-date'> <p class='news_adds'>Дата добавления: %s</p> </div> <div class='head_info'> <p class='author_adds alignleft'>Автор: <b>%s</b> |<span style='margin-left:10px;'>%s просмотров</span></p> </td></tr><tr><td><table border='0'><tr> <td valign='top' style='text-align:justify; color:gray;'><div style='margin-top:-10px;'>%s</div></p></td></tr></table> </div> </td></tr> <tr><td></td></tr> <tr><td style='padding-top:10px;'> <!-- AddThis Button BEGIN --> <div class='addthis_toolbox addthis_default_style addthis_32x32_style'> <a class='addthis_button_preferred_1'></a> <a class='addthis_button_preferred_2'></a> <a class='addthis_button_preferred_3'></a> <a class='addthis_button_preferred_4'></a> <a class='addthis_button_compact'></a> <a class='addthis_counter addthis_bubble_style'></a> </div> <script type='text/javascript' src='http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f3b99ef2aa35c0f'></script> <!-- AddThis Button END --><div style='width:697px; height:1px; background:transparent url(images/dot.gif) repeat-x left bottom; padding-top:5px;'></div> </td></tr> </table> ",$myrow['title'],$myrow['date'],$myrow['author'],$myrow['view'],$myrow['text'],$myrow['id'],$myrow['title']); print_r($_SESSION);echo session_id(); ?> </body> </html> 
  • one
    How to improve? Throw out and rewrite everything in a new way, and do not use html in php - Alex Kapustin
  • I don’t know, I’m ready to do everything to eliminate this problem, but how to avoid html without it))) - MaximPro

1 answer 1

In the news table, we add the Int 11 field view_count. Next we insert, for example, the following code:

 if($_COOKIE['news']!=$id) { setcookie ("news", $id, time()+3600*48); mysql_query("UPDATE `news` SET `view_count`=`view_count`+1 WHERE `id`='$id'"); } 

The logic is as follows, if the user has opened the news, and he does not have cookies with news id, then the news view counter is updated to 1 and we postpone the news id cookie to him, and the counter will not be updated for any actions on the page. If I correctly understood the task.

  • in fact, so I have done on the blog. 6 hours baking shelf life :) - Artem
  • Shrek you have the same problem as me))) went to your blog checked all the same - MaximPro
  • your answer does not work - MaximPro
  • So you don't do it right - Sharpness