Visit counter

0

I need your help for a visit counter, it's for wordpress or php, [ link

UPDATE

function under_bh_fun() {
// fichero donde se guardaran las visitas
 $fichero = "visitas.txt";

 $fptr = fopen($fichero,"r");

 // sumamos una visita
 $num = fread($fptr,filesize($fichero));
 $num++;

 $fptr = fopen($fichero,"w+");
 fwrite($fptr,$num);
 fclose($fptr);
 return ("<span id='days_since'>$num</span>");
}
add_shortcode('under_bh_visits', 'under_bh_fun'); 

I only have one small problem, and that is that it does not work.

    
asked by Juan David 15.02.2017 в 18:05
source

2 answers

1

Hello, I am enclosing the code that I have made for the students of my course 1º Create this code:

<?php
function contador()
{
 // fichero donde se guardaran las visitas
 $fichero = "visitas.txt";

 $fptr = fopen($fichero,"r");

 // sumamos una visita
 $num = fread($fptr,filesize($fichero));
 $num++;

 $fptr = fopen($fichero,"w+");
 fwrite($fptr,$num);

 return $num;
}
?>

2º The next step will be to create a file that we will call "vistas.txt" Inside the we will place the initial number that you want your accountant to carry, I think the number that should go inside is "0"

When we have created these files, all we have to do is put the following code in each place you want the meter to appear.

3º Create this page

Esta pagina ha sido visitada
<?
include("contador.php");
echo contador();
?> veces

It has always worked is pure PHP.

    
answered by 15.02.2017 / 20:29
source
1

Here's how to do it in wordpress, I hope it works for you.

You add the following code in the function.php

// This is the function that is responsible for adding visit to visit

function setVisitasPost($post_ID) {
    $key = 'post_views_count';
    $count = get_post_meta($post_ID, $key , true);
    if($count==''){
        $count = 0;
        delete_post_meta($post_ID, $key );
        add_post_meta($post_ID, $key , '0');
    }else{
        $count++;
        update_post_meta($post_ID, $key , $count);
    }
}

//Esta es la función que se encarga de devolver la cantidad de visitas

function getVisitasPost($post_ID){
    $key = 'post_views_count';
    $count = get_post_meta($post_ID, $key , true);
    if($count==' '){
        delete_post_meta($post_ID, $key );
        add_post_meta($post_ID, $key , '0');
        return "0 Visitas";
    }
    return $count.' Visitas';
}

Now inside the loop insert the following code that will take care of each time someone visits count the visits

setVisitasPost(get_the_ID());

After where you want to show the number of visits you put the following code

echo getVisitasPost(get_the_ID());
    
answered by 15.02.2017 в 18:32