2014-10-29 68 views
0

我想有一种方法可以查看PHP脚本已被使用的次数?我想要一个文件,里面只有一个数字,然后我希望能够查看它。因此,如果有6人通过我的PHP脚本发送电子邮件,那么每次使用时都会将1加1,然后将其保存到文件中?如何将计数添加到php脚本中?

我该怎么做?谢谢。

+0

听起来像一个典型的使用情况的数据库。如果这不是一个选项,你仍然想将文本文件作为“数据库”处理,那么你究竟在哪里遇到问题?读文件?增加价值? – Lix 2014-10-29 14:34:22

+0

在您的脚本中,打开文件,读取数字,添加一个数字,将数字写入文件。 – 2014-10-29 14:34:29

+0

是信不信,我不知道PHP我只知道一些不链接的位和bob,我不知道打开文件并将数据保存到他们的任何东西? – 2014-10-29 17:12:02

回答

2

你可以使用PHP的file_get_contents();file_put_contents();功能:

$file = 'count.txt'; 

if(file_exists($file)){ 
    $current = file_get_contents($file); 
    file_put_contents($file, ($current + 1)); 
}else 
    file_put_contents($file, 1); 
+0

如果没有文件,什么会给你'未能打开流:没有这样的文件或目录'。 – vaso123 2014-10-29 14:39:56

+0

@lolka_bolka不再:) – 2014-10-29 14:42:30

+0

thx编辑:) – vaso123 2014-10-29 14:44:57

2
//Define the name of the file 
define('FILE_NAME', 'counter.txt'); 

//Check is there this file 
if (file_exists(FILE_NAME)) { 
    //If yes, read out the content of it 
    $content = file(FILE_NAME); 
    //Put the incrased value 
    file_put_contents(FILE_NAME, $content[0] + 1); 
} else { 
    //Create the file with 1 visitor 
    file_put_contents(FILE_NAME, 1); 
} 
+1

我永远不会明白,为什么有人downvote测试和工作的解决方案。 – vaso123 2014-10-29 14:40:52

相关问题