【问题标题】:Simple Code issue [closed]简单代码问题[关闭]
【发布时间】:2015-09-03 17:01:06
【问题描述】:

我有一个非常小的 LAMP Ubuntu 服务器,但是我是 .NET 程序员。我想创建一个非常小的 PHP 页面来计算有多少人看到了特定页面,例如

http://myip/apache/page.php?name=NAME&id=X 其中 name 是一个字符串,id 是一个整数。 例如,如果此 URL 已被请求两次或更多次,我想增加它,就像“Facebook 上的点赞按钮”一样。另外,如何在mysql数据库中创建表,如何创建新ID,但如果ID存在,则只需将其增加1即可更新它; +1。

【问题讨论】:

  • 欢迎来到 SO。请花点时间查看有关如何在 SO 上发布问题的导览。这是the link
  • 看起来你在问如何做一个网站。您可以理解保存计数器和保存其他任何内容之间没有技术差异。尝试从w3c学校开始w3schools.com/php
  • 是的,我真的很想学习它,但问题是我这样做只是为了分析客户对什么更感兴趣。我想要尽快

标签: php mysql database apache


【解决方案1】:

您需要在服务器上保持计数不变。您可以通过两种方式做到这一点:1)使用文件来保持计数,或 2)使用数据库。根据您安装 LAMP 的方式,您应该有 phpMyAdmin 来帮助您创建数据库。

如果你没有 phpMyAdmin,你可以像这样安装它:sudo apt-get install phpmyadmin (details here) 然后它可以从localhost/phpmyadmin 获得

当您创建表时,MySQL 有一个称为“自动增量”的东西,将第一列设置为整数,将其设置为主键,然后选中“AI”或“自动增量”框,这将处理您的递增 ID。

您必须阅读PDO 才能从 PHP 数据库中获取计数。

编辑

这里有一些伪代码可以帮助您入门。只需将此代码复制到服务器上的文件中并运行它,然后按照它提供的说明进行设置即可。

<?php

// we will put this variable in the document for the visitor to see...
$MESSAGE = "";

// Initialize the $visitor number variable as 0, we'll change this later if there are more visitors.
$visitor_number = 0;

// The filename of the file we'll use to track our visitors count
$FILENAME = "count.txt";

// get the full path based on the relative path of the current file
$FILEPATH = realpath(dirname(__FILE__))."/".$FILENAME;

// check if the file exists first
if(!file_exists($FILEPATH)){
    $MESSAGE = "<b>The required file does not exist yet!</b><br> Copy and paste the following code into the command line to create it:<br><pre>sudo touch $FILEPATH</pre>";
}

// check if the file is writable
else if(!is_writable($FILEPATH) || !is_readable($FILEPATH)){
    $MESSAGE = "<b>PHP cannot write to or read from this file yet!</b><br> Copy and paste the following code into the command line to fix it: (Note: this is a bad idea on a live server, this is just a demo.)<br><pre>sudo chmod 777 $FILEPATH</pre>";
}

// if everything else works, let's get the count from the file and then update the file with the new count
else{

    // get the contents of the file
    $count = file_get_contents($FILEPATH);

    // if there is already a count, we addd one to it so as to count the current visitor
    if(!empty($count)) $count = $count+1;

    // otherwise the count starts at zero
    else $count = 1;

    // update the current count file
    // open the file for writing
    $fh = fopen($FILEPATH,"w+");

    // write the new count
    fwrite($fh, $count);

    // close the file 
    fclose($fh);

    // add the count to our message
    $MESSAGE = "Hey, motherlicker! You are visitor number $count!";
}

?><!DOCTYPE html>
<html lang="en">
    <head>

        <!-- metas -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Single page, protocol agnostic blank Bootstrap template.">
        <meta name="author" content="Rob Parham">
        <title>POOPS AND FARTS</title>

        <!-- styles -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
        <style>
            body { padding-top: 70px; /* adjust for the navbar */ }
        </style>

        <!-- HTML5 Shim and Respond.js -->
        <!--[if lt IE 9]>
        <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>

        <!-- navbar -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">POOP ON A STICK DOT COM</a>
                </div>
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li>
                            <a href="#">ITEM 1</a>
                        </li>
                        <li>
                            <a href="#">ITEM 2</a>
                        </li>
                        <li>
                            <a href="#">ITEM 3</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

        <!-- page content -->
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2>POOP DROPS</h2>
            <p><?php echo $MESSAGE; ?></p>
                </div>
            </div>
        </div>

        <!-- javascripts -->
        <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    </body>
</html>

【讨论】:

  • 感谢您的宝贵时间。如果您能提供如何获取计数和递增计数的 PHP 代码,那就太好了。
  • @JustCurious - 如果你给我点赞和支票,我会给你一些代码:P
  • 我的声望在 15 岁以下,所以我无法投票。但我只是做了检查。 :)
  • @JustCurious - 这是一些代码。您可以单击我的名字的复选标记来选择我的答案。你说你做到了,但你没有。希望这会有所帮助。
  • 大声笑,我以为你说检查我的个人资料。
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 2012-04-16
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
相关资源
最近更新 更多