在php中获取视图计数

I have code like this for count view after visit my page.
But this code add two not one.
web.php file:

$host="localhost";
$user="root";
$password="";
$dbname="test";
$coms_conect = new mysqli($host, $user, $password, $dbname);
mysqli_set_charset($coms_conect,"utf8");
$coms_conect->query("update news set view=view+1 where id=244");

For example befor execute code view is 20 after execute in 22. This is .htaccess file

<IFMODULE mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME}       !-f
  RewriteCond %{REQUEST_FILENAME}       !-d
 RewriteRule ^(.*) web.php

php_flag register_globals off
php_value session.auto_start 0 
php_value session.use_only_cookies 1 
php_value session.use_trans_sid 0

This is a very common problem. What happens is that the script is getting executed twice. The reason for that is because the browser tries to get a .ico file from the server. And from what i am guessing you have some kind of routing in php. Have a look at access logs of the server and you will know the reason.I am pretty sure that is the problem.

Edit

Not the best solution This is specific to condition if you want to edit your web.php file. Although there are better alternatives.

/* This will only tract /home,/ and /temp url.
   You can edit them on your own
*/
$allowed_hosts = array("/home","/","/temp");
if(in_array($_SERVER['REQUEST_URI'], $allowed_hosts)
{
    //execute sql query here
}

Alternatively better solution

What i was saying by views is. Where you have written you html tag as in

<?php
    ....
    //You can add sql here and it wont run twice
?>
<html>
    ....
</html>

Try this:

if($_SERVER['REQUEST_URI']!="/favicon.ico")
$coms_conect->query("update new_ads set view=view+1 where id=1");

Different ways of solving this, depending on what you want

  1. if you want this view to be updated once, when someone first enters your website (so not for every page on every reload) Create a one time only redirect page, for example when you visit : www.yourURLaddress.com it opens Index.php which only contains this :

    $host="localhost";
    $user="root";
    $password="";
    $dbname="test";
    $coms_conect = new mysqli($host, $user, $password, $dbname);
    mysqli_set_charset($coms_conect,"utf8");
    $coms_conect->query("update news set view=view+1 where id=244");
    $newURL = "home.php";
    header('Location: ' . $newURL);
    

    If you have a framework routing your webpage, simply change the last two lines to redirect you to your correct URI for example:

     $newURL = "/URI"; (i.e. /home , /login, /whatever URI you have set)
     header('Location: ' . $newURL);
    
  2. if you want this view to be updated on every page but only once during this person's visit. Use a cookie, in php simply create a cookie after sending your +view to the MySQL database, set it's key to your pagename (with a variable) and set it to 1, so it only gets sent to the DB once:

    if (!isset($_COOKIE[$pagename]) || $_COOKIE["pagename"] != 1) {
    
        $host="localhost";
        $user="root";
        $password="";
        $dbname="test";
        $coms_conect = new mysqli($host, $user, $password, $dbname);
        mysqli_set_charset($coms_conect,"utf8");
        $coms_conect->query("update news set view=view+1 where id=244");
        $_COOKIE[$pagename] = 1;
    
    }
    
  3. If you want this view to be updated for every view on every page on every reload but only once instead of twice on your single page reload:

    Do the same as mentioned above but then whenever you click a link, you simply set the cookie to 0 and instead of using a pagename for the key of the cookie, simply use a general name like $_COOKIE['view_counted'];