我想使用 cookie 获取页数
I Want to get the page count using cookie
我想在我的索引页面中获取页数,使用 cookie.So 目前我已经完成 like.Now 我的问题 is:If 我刷新页面,浏览器显示计数 2它不会增加下一个 refresh.I 不知道我的 code.Further 有什么问题 我想知道如何处理下一页中的 cookie 还是我可以在同一页中处理 cookie?任何人都可以指导please.but这是我的要求。
<?php
$cookie = 1;
setcookie("count", $cookie);
if (!isset($_COOKIE['count']))
{
}
else
{
$cookie = ++$_COOKIE['count'];
}
echo "The Total visit is".$cookie;
?>
我决定为此使用本地存储,因为我真的不喜欢 cookie,一些用户完全阻止它们。
您可以设置回显。使用此
这是我想出的:http://jsfiddle.net/azrmno86/
// Check browser support
if (typeof(Storage) != "undefined") {
//check if the user already has visited
if (localStorage.getItem("count") === "undefined") {
//set the first time if it dfoes not exisit yet
localStorage.setItem("count", "1");
}
//get current count
var count = localStorage.getItem("count");
//increment count by 1
count++;
//set new value to storage
localStorage.setItem("count", count);
//display value
document.getElementById("result").innerHTML = count
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support";
}
更新经过更多的澄清
此样式使用存储在服务器上的 .txt 文件。 Cookie 不可靠。如果有人清除它们,你就完成了。如果你使用变量,任何服务器重启都会杀死你的计数。要么用数据库,要么用这个方法。
<?php
//very important
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
}
echo "You are visitor number $counterVal to this site";
我想在我的索引页面中获取页数,使用 cookie.So 目前我已经完成 like.Now 我的问题 is:If 我刷新页面,浏览器显示计数 2它不会增加下一个 refresh.I 不知道我的 code.Further 有什么问题 我想知道如何处理下一页中的 cookie 还是我可以在同一页中处理 cookie?任何人都可以指导please.but这是我的要求。
<?php
$cookie = 1;
setcookie("count", $cookie);
if (!isset($_COOKIE['count']))
{
}
else
{
$cookie = ++$_COOKIE['count'];
}
echo "The Total visit is".$cookie;
?>
我决定为此使用本地存储,因为我真的不喜欢 cookie,一些用户完全阻止它们。
您可以设置回显。使用此
这是我想出的:http://jsfiddle.net/azrmno86/
// Check browser support
if (typeof(Storage) != "undefined") {
//check if the user already has visited
if (localStorage.getItem("count") === "undefined") {
//set the first time if it dfoes not exisit yet
localStorage.setItem("count", "1");
}
//get current count
var count = localStorage.getItem("count");
//increment count by 1
count++;
//set new value to storage
localStorage.setItem("count", count);
//display value
document.getElementById("result").innerHTML = count
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support";
}
更新经过更多的澄清
此样式使用存储在服务器上的 .txt 文件。 Cookie 不可靠。如果有人清除它们,你就完成了。如果你使用变量,任何服务器重启都会杀死你的计数。要么用数据库,要么用这个方法。
<?php
//very important
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
}
echo "You are visitor number $counterVal to this site";