【问题标题】:File name as variable+.txt [closed]文件名作为变量+ .txt [关闭]
【发布时间】:2014-04-09 15:46:00
【问题描述】:
session_start();
$user = $_SESSION['username'];

if( isset($_POST['subm_btn']) ) { 
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents($user.".txt");
 }
 function incrementClickCount()
 { $count = getClickCount() + 1;
 file_put_contents($user.".txt", $count);
  }

用户在我的网站上注册,然后点击按钮 (name="subm_btn")。我想计算点击次数并在名为“username.txt”的文件中添加点击次数

【问题讨论】:

  • 这个脚本应该如何工作,什么不适合你?
  • 注意:您目前的书写方式可能会出现问题。如果我能以某种方式创建我的用户名,比如/var/www/secret_stuff,那就太糟糕了。清理这些字符串:)
  • @Dragony 我编辑帖子。

标签: php variables filenames


【解决方案1】:

我猜你正在寻找这样的东西:

$file=$user.'.txt';
incrementClickCount($file);

function incrementClickCount($file){ 
  $count = getClickCount($file) + 1;
  file_put_contents($file, $count);
}

function getClickCount($file) {
   return (int)file_get_contents($file);
}

如果您希望变量在函数中可用,您可以将其设为全局变量或将其作为参数传递(这样更好)。

【讨论】:

    【解决方案2】:

    您定义$user,然后访问$user1。这将是我对它为什么不起作用的猜测。此外,无论如何,使用$file 可能是一个更好的主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2016-08-15
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多