我有一个非常小的LAMP Ubuntu服务器,但我是.NET程序员。我想创建一个非常小的PHP页面来计算看到特定页面的人数,例如。
http://myip/apache/page.php?name=NAME&id=X 其中name是一个字符串,id是一个整数。 如果这个URL已被请求两次或更多次,我想增加它就像'Facebook上的类似按钮'。另外,如何在mysql数据库中创建表,如何创建新ID,但如果存在ID,则只需将其递增1即可更新; +1。
答案 0 :(得分:0)
您需要在您的服务器上保持计数持久性。您可以通过以下两种方式实现:1)使用文件来保持计数,或2)使用数据库。根据您安装LAMP的方式,您应该使用phpMyAdmin来帮助您创建数据库。
如果您没有phpMyAdmin,可以像下面这样安装:sudo apt-get install phpmyadmin
(details here)然后它就可以从localhost/phpmyadmin
创建表时,Mysql有一个叫做“自动增量”的东西,将第一列设置为整数,将其设置为主键,然后选中“AI”或“自动增量”框,这将处理你的递增ID。
您必须阅读PDO以获取PHP中数据库的计数。
这是一个非常广泛的问题,但我希望我能帮助一点。
修改强> 这里有一些应该让你入门的sudo代码。只需将此代码复制到服务器上的文件中并运行它,然后按照它给出的说明进行设置。
<?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>