dsnw2651 2013-12-01 19:44
浏览 26

PHP查看计数器

I am trying to make a website and it's almost completed but I want to add a view counter so when someone visit the page it count the view and save it into the database. My script is working fine but the problem is that it continue view count even visitor is viewing anyother page

My pages url show like this pictures.php?ID=13

I have added this PHP code in *count.php*

<?php
session_start();
if (isset($_SESSION['views'])){
    $_SESSION['views']++;
    } else {
        $_SESSION['views'] =0;
        }
        //echo $_SESSION['views'];


?>

Page *views.php*

<?php
session_start();
if (isset($_SESSION['$post_id'])){
    $_SESSION['$post_id']++;
    } else {
        $_SESSION['$post_id'] =0;
        }
        //echo $_SESSION['views'];


?>

<?php
    echo "<hr><div align=\"center\">";



    echo $_SESSION['$post_id'];
    ?>

    <?php
    $save = $_SESSION['$post_id'];

$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"UPDATE save_data SET Views='$save' WHERE ID='$page_id'");

mysqli_close($con);
?> 

And added this line in Pictures.php where I want to show and count visits

<?php  include("views.php"); ?>

Problem:

When someone visits page pictures.php?ID=8 it will show him page view 1 and save this view in database where ID=8, when he visit page pictures.php?ID=12 it will show him view 2 and save this 2 in database where ID=12. My point is that it is continuously counting instead of each page view.

Thanks in advance

Here is Pictures.php

<?php 
include("connection.php");

if(isset($_GET['ID'])){

$page_id = $_GET['ID'];

    $select_query = "select * from save_data where ID='$page_id'";

$run_query = mysql_query($select_query);

while($row=mysql_fetch_array($run_query)){

    $post_id = $row['ID']; 
    $post_title = $row['Title'];
    $post_image = $row['Name'];



?>

<h3>
<a href="pictures.php?ID=<?php echo $post_id; ?>">

<?php echo $post_title; ?>

</a>

</h3><center>
<form id="search-form" action="javascript:void(0);">
<input type="text" id="dimen" name="dimension" />
<input type="submit" value="Resize" Onclick ="splitString()"/></form>
<div id="sizet">
Type size like 200*300 in box
</div></center>

<div id="img"><img id="myImage" src="uploads/<?php echo $post_image; ?>"  /></div>



<?php } }?>
<center>
<div id="postdetails">
<?php  include("posted_by.php"); ?></center>
</div>
<?php  include("views.php"); ?>
<html>
<link href="css/Pictures.css" rel="stylesheet" type="text/css">
<body>
<head>
<script type="text/javascript">
function splitString()
{
var myDimen=document.getElementById("dimen").value;
var splitDimen = myDimen.split("*");
document.getElementById("myImage").width=splitDimen[0];
document.getElementById("myImage").height=splitDimen[1];
}
</script>
</head>

</body>

  • 写回答

2条回答 默认 最新

  • duancan65665 2013-12-01 20:29
    关注

    Try to use structure like this

    $_SESSION['view'][{resource_name}_{resource_id}]
    

    E.g. for picture with id 8 it will be

    $_SESSION['views']['picutures_8']++
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100