drjk87189 2015-06-24 20:24
浏览 22

PHP专柜问题

I have a counter that should be incrementing by 1 but yet it is incrementing by 2. I have the controller and view. It's a simple exercise but I'm not sure why it's incrementing by 2. Anyone have any ideas?

http://screencast.com/t/w9w7GndQK

CONTROLLER

function setCount($fileName = 'counter.txt') {
if (file_exists($fileName)) {

    //read the value
    $handle = fopen($fileName, 'r');

    // increment it by one
    $count = (int) fread($handle, 20) + 1;

    // write the new value
    $handle = fopen($fileName, 'w');
    fwrite($handle, $count);

    // close the file
    fclose($handle);
} else {

    // create the file
    $handle = fopen($fileName, 'w+');
    $count = 1;

    // set a default value of 1
    fwrite($handle, $count);
    fclose($handle);
}


return $count;
}

$count = setCount();

require('index.tmpl.php');

VIEW:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Counter</title>
    <link rel="stylesheet" href="#">
</head>
<body>
    <p>You are the <?php echo $count; ?>visitor to this website.</p>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dtnpzghys01643322 2015-06-24 21:09
    关注

    Using a text-file for this is bad practice and can result in inconsestent behavior. For example, when two visitors access the site at the same time: Visitor 1 open the file, increase the stored value lets say 5 to 6, and before it can save its changes another visitor open the file and do the same. Now a visitor is dropped because in both cases the counter will only incease once thought there where two visitors.

    Instead you should use a database like MySQL to store the data. This make it also easy to build a more detailed counter. For example, you can get stats about how much people visited the page per day or which are the most frequented hours. It is also possible to track details about the users system (e.g. useragent which show what browser + version and operating system the user has) or log the referer to get informed from which pages your visitors came to you.

    A simple counter-table could be consist of the timestamp and the ip-adress of the visitor.

    CREATE TABLE pageviews(
        datetime INT UNSIGNED NOT NULL,
        ip VARCHAR(15) NOT NULL
    );
    

    To track a visitor you need to run the following query:

    INSERT INTO pageviews
    SET datetime = UNIX_TIMESTAMP(),
        ip = "' . $db->escape( $_SERVER['REMOTE_ADDR'] ) . '"
    

    Now you can extract the data like you want. For a total-counter you can simpy do a COUNT(*) on the table:

    SELECT COUNT(*) AS totalViews
    FROM pageviews
    

    You're free to display what you want, e.g. group by day as I mentioned before.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?