PHP - Contador visitas

 
Vista:
sin imagen de perfil
Val: 54
Ha aumentado su posición en 38 puestos en PHP (en relación al último mes)
Gráfica de PHP

Contador visitas

Publicado por Carlos (26 intervenciones) el 15/04/2021 16:33:18
Hola tras probar varios códigos, estoy usando un contador, la única pega es que cada 10 minutos o menos me cuenta otra vez al refrescar la pagina. Me gustaría poder indicarle que al menos pasen 2 horas para volver a contarme a la misma IP o persona.
¿Es posible aumentar dicho tiempo?
Solo veo la opción de aumentar el tiempo que te cuenta online.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
 ?>
 <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
 
<?php
/*
Text Counter by http://www.free-php-counter.com
You are allowed to remove advertising after you purchased a licence
*/
 
// settings
 
// ip-protection in seconds
$counter_expire = 600;
$counter_filename = "counter.txt";
 
// ignore agent list
$counter_ignore_agents = array('bot', 'bot1', 'bot3');
 
// ignore ip list
$counter_ignore_ips = array('127.0.0.2', '127.0.0.3');
 
 
// get basic information
$counter_agent = $_SERVER['HTTP_USER_AGENT'];
$counter_ip = $_SERVER['REMOTE_ADDR'];
$counter_time = time();
 
 
if (file_exists($counter_filename))
{
   // check ignore lists
   $ignore = false;
 
   $length = sizeof($counter_ignore_agents);
   for ($i = 0; $i < $length; $i++)
   {
	  if (substr_count($counter_agent, strtolower($counter_ignore_agents[$i])))
	  {
	     $ignore = true;
		 break;
	  }
   }
 
   $length = sizeof($counter_ignore_ips);
   for ($i = 0; $i < $length; $i++)
   {
	  if ($counter_ip == $counter_ignore_ips[$i])
	  {
	     $ignore = true;
		 break;
	  }
   }
 
 
 
   // get current counter state
   $c_file = array();
   $fp = fopen($counter_filename, "r");
 
   if ($fp)
   {
      //flock($fp, LOCK_EX);
	  $canWrite = false;
      while (!$canWrite)
	     $canWrite = flock($fp, LOCK_EX);
 
	  while (!feof($fp))
      {
         $line = trim(fgets($fp, 1024));
		 if ($line != "")
		    $c_file[] = $line;
      }
      flock($fp, LOCK_UN);
	  fclose ($fp);
   }
   else
   {
      $ignore = true;
   }
 
 
   // check for ip lock
   if ($ignore == false)
   {
      $continue_block = array();
      for ($i = 1; $i < sizeof($c_file); $i++)
      {
         $tmp = explode("||", $c_file[$i]);
 
		 if (sizeof($tmp) == 2)
		 {
		    list($counter_ip_file, $counter_time_file) = $tmp;
	        $counter_time_file = trim($counter_time_file);
 
	        if ($counter_ip == $counter_ip_file && $counter_time-$counter_expire < $counter_time_file)
	        {
	           // do not count this user but keep ip
		       $ignore = true;
 
			   $continue_block[] = $counter_ip . "||" . $counter_time;
	        }
		    else if ($counter_time-$counter_expire < $counter_time_file)
	        {
	           $continue_block[] = $counter_ip_file . "||" . $counter_time_file;
		    }
		 }
	  }
   }
 
   // count now
   if ($ignore == false)
   {
      // increase counter
	  if (isset($c_file[0]))
	     $tmp = explode("||", $c_file[0]);
	  else
	     $tmp = array();
 
	  if (sizeof($tmp) == 8)
	  {
	     // prevent errors
		 list($day_arr, $yesterday_arr, $week_arr, $month_arr, $year_arr, $all, $record, $record_time) = $tmp;
 
         $day_data = explode(":", $day_arr);
	     $yesterday_data = explode(":", $yesterday_arr);
 
         // yesterday
	     $yesterday = $yesterday_data[1];
	     if ($day_data[0] == (date("z")-1))
	     {
	        $yesterday = $day_data[1];
	     }
	     else
	     {
	        if ($yesterday_data[0] != (date("z")-1))
		    {
		       $yesterday = 0;
		    }
	     }
 
	     // day
	     $day = $day_data[1];
	     if ($day_data[0] == date("z")) $day++; else $day = 1;
 
	     // week
	     $week_data = explode(":", $week_arr);
	     $week = $week_data[1];
	     if ($week_data[0] == date("W")) $week++; else $week = 1;
 
	     // month
	     $month_data = explode(":", $month_arr);
	     $month = $month_data[1];
	     if ($month_data[0] == date("n")) $month++; else $month = 1;
 
	     // year
	     $year_data = explode(":", $year_arr);
	     $year = $year_data[1];
	     if ($year_data[0] == date("Y")) $year++; else $year = 1;
 
	     // all
	     $all++;
 
	     // neuer record?
	     $record_time = trim($record_time);
	     if ($day > $record)
	     {
	        $record = $day;
		    $record_time = $counter_time;
         }
 
	     // speichern und aufräumen und anzahl der online leute bestimmten
	     $online = 1;
 
	     // write counter data (avoid resetting)
	     if ($all > 1)
	     {
	        $fp = fopen($counter_filename,"w+");
		    if ($fp)
            {
		       //flock($fp, LOCK_EX);
		   	   $canWrite = false;
               while (!$canWrite)
			      $canWrite = flock($fp, LOCK_EX);
 
			   $add_line1 = date("z") . ":" . $day . "||" . (date("z")-1) . ":" . $yesterday . "||" . date("W") . ":" . $week . "||" . date("n") . ":" . $month . "||" . date("Y") . ":" . $year . "||" . $all . "||" . $record . "||" . $record_time . "\n";
		       fwrite($fp, $add_line1);
 
		       $length = sizeof($continue_block);
			   for ($i = 0; $i < $length; $i++)
               {
                  fwrite($fp, $continue_block[$i] . "\n");
			      $online++;
	           }
 
			   fwrite($fp, $counter_ip . "||" . $counter_time . "\n");
		       flock($fp, LOCK_UN);
		       fclose($fp);
	        }
         }
	     else
	     {
		    $online = 1;
	     }
	  }
	  else
	  {
		 // show data when error  (of course these values are wrong, but it prevents error messages and prevent a counter reset)
 
		 // get counter values
         $yesterday = 0;
         $day = $week = $month = $year = $all = $record = 1;
         $record_time = $counter_time;
         $online = 1;
	  }
   }
   else
   {
      // get data for reading only
	  if (sizeof($c_file) > 0)
	     list($day_arr, $yesterday_arr, $week_arr, $month_arr, $year_arr, $all, $record, $record_time) = explode("||", $c_file[0]);
	  else
		 list($day_arr, $yesterday_arr, $week_arr, $month_arr, $year_arr, $all, $record, $record_time) = explode("||", date("z") . ":1||" . (date("z")-1) . ":0||" . date("W") . ":1||" . date("n") . ":1||" . date("Y") . ":1||1||1||" . $counter_time);
 
	  // day
	  $day_data = explode(":", $day_arr);
      $day = $day_data[1];
 
	  // yesterday
	  $yesterday_data = explode(":", $yesterday_arr);
      $yesterday = $yesterday_data[1];
 
	  // week
	  $week_data = explode(":", $week_arr);
	  $week = $week_data[1];
 
	  // month
	  $month_data = explode(":", $month_arr);
	  $month = $month_data[1];
 
	  // year
	  $year_data = explode(":", $year_arr);
	  $year = $year_data[1];
 
	  $record_time = trim($record_time);
 
	  $online = sizeof($c_file) - 1;
	  if ($online <= 0)
	     $online = 1;
   }
}
else
{
   // create counter file
   $add_line = date("z") . ":1||" . (date("z")-1) . ":0||" . date("W") . ":1||" . date("n") . ":1||" . date("Y") . ":1||1||1||" . $counter_time . "\n" . $counter_ip . "||" . $counter_time . "\n";
 
   // write counter data
   $fp = fopen($counter_filename,"w+");
   if ($fp)
   {
      //flock($fp, LOCK_EX);
	  $canWrite = false;
      while (!$canWrite)
	     $canWrite = flock($fp, LOCK_EX);
 
      fwrite($fp, $add_line);
	  flock($fp, LOCK_UN);
      fclose($fp);
   }
 
   // get counter values
   $yesterday = 0;
   $day = $week = $month = $year = $all = $record = 1;
   $record_time = $counter_time;
   $online = 1;
}
?>
<div style="width:150px;">
   <div style="border:1px solid #000000;padding:2px;width:72%;font-size:80%;font-weight:bold;" align="center">
     Estad&iacute;sticas
   </div>
 
   <div style="border:1px solid #000000;padding:2px;width:72%;font-size:80%;text-align: left">
      &raquo; <?php echo $online; ?> En l&iacute;nea<br />
      &raquo; <?php echo $day; ?> Hoy<br />
      &raquo; <?php echo $yesterday; ?> Ayer<br />
      &raquo; <?php echo $week; ?> Esta semana<br />
      &raquo; <?php echo $month; ?> Este mes<br />
      &raquo; <?php echo $year; ?> Este a&ntilde;o<br />
      &raquo; <?php echo $all; ?> Total
   </div>
 
   <div style="border:1px solid #000000;padding:2px;width:72%;font-size:80%;"align="center">
      Record: <?php echo $record; ?> (<?php echo date("d.m.Y", $record_time) ?>)
      <div style="font-size:75%;" align="center"><a href="http://es.free-php-counter.com" target="_blank">Contador PHP gratuito</a></div>
   </div>
</div>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder