【问题标题】:The best way or code to create counter hits using session, PHP and MySQL [closed]使用会话、PHP 和 MySQL 创建计数器命中的最佳方法或代码 [关闭]
【发布时间】:2014-02-22 16:22:06
【问题描述】:

下面,我粘贴了我发现使用 PHP、MySQL 和会话进行反击的最佳方法的代码。但我想知道有没有其他方法可以完成这个过程?或者这种方式只是在会话中进行反击的方式?我也想学习其他方法,如果你们知道,那将非常有帮助。

<?php
session_start();
include_once"db.inc.php";
$webpage=htmlspecialchars($_SERVER["REQUEST_URI"]);

$sql= "CREATE TABLE IF NOT EXISTS counter (
id int(4) NOT NULL auto_increment,
webpage varchar(90) NOT NULL,
visitors int(11) NOT NULL default '1',
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1"; 

mysql_query($sql);

$result=mysql_query("SELECT * FROM counter WHERE webpage='$webpage'");
$num_rows= mysql_num_rows($result);
if ($num_rows== 0){
mysql_query("INSERT INTO counter (id, webpage, visitors)
VALUES ('','$webpage','1')");

}else{

if (!isset($_SESSION['webpage'])){$_SESSION['webpage']= 0;
mysql_query("UPDATE counter SET visitors=visitors+'1' WHERE webpage='$webpage'");}}
?>

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Just a test </title>

</head>
<body> 

<?php
$result=mysql_query("SELECT * FROM counter WHERE webpage='$webpage'");
$row=mysql_fetch_assoc($result);
$n=$row['visitors'];
$l=9-strlen($n);
$z=substr('',0,$l);
$c="Counter: ".$z.$n;

echo "<div class='counter'>".$c."</div>";

mysql_close();
//session_unset();
//session_destroy();

?>
</body>
</html> 

【问题讨论】:

标签: php mysql session counter


【解决方案1】:

我认为记录页面点击时没有使用会话:

if ($num_rows== 0){
mysql_query("INSERT INTO counter (id, webpage, visitors) VALUES ('','$webpage','1')");

}else{

// if (!isset($_SESSION['webpage'])){$_SESSION['webpage']= 0; /* you can remove this line */
mysql_query("UPDATE counter SET visitors=visitors+'1' WHERE webpage='$webpage'");}

在查看计数时,删除一些行,因为这些行没用...

$n=$row['visitors'];
//$l=9-strlen($n); /* you can remove this line */
//$z=substr('',0,$l); /* you can remove this line */
//$c="Counter: ".$z.$n; /* you can remove this line */
$c="Counter: ".$n;  /* removed $z  */

echo "<div class='counter'>".$c."</div>";

希望这会有所帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-13
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2012-01-15
    相关资源
    最近更新 更多