【问题标题】:How to set a cookie that counts the number of times a form has been sent per user?如何设置一个 cookie 来计算每个用户发送表单的次数?
【发布时间】:2020-12-03 20:09:40
【问题描述】:

我的 PHP 代码有问题,cookie 有效,但我想计算同一个用户 (cookie) 发送响应的次数。

这是我想要完成的:

  • 带有 HTML(用户名、电子邮件、消息)的反馈表
  • 使用 CSS 设置侧面样式
  • 使用 PHP 处理表单(表单提交后的确认输出)
  • 设置一个 cookie,用于计算每个用户发送表单的次数。
  • 将内容存储为 CSV 文件中的新行(格式:“用户名”、“电子邮件”、“消息”)
  • 如果用户发送了 3 次表单(参见 cookie)-> 没有存储和消息给用户

问题:

设置一个 cookie,用于计算每个用户发送表单的次数。如果用户发送了 3 次表单(请参阅 cookie),则它不会保存在 CSV 文件中

    <?php
        if (isset($_POST['submit'])) {
            $counter=0;
            setcookie("username",$_POST['username'],time()+60*60*24,"/");
            $conter = ++$_COOKIE['username']+1;
        }
     
         
     
        if (isset($_POST['submit'])) {
            if ($counter<3) {
            $fp = fopen("feedback.csv", "a");
            fwrite($fp, "\"". $_POST["username"] . "\",\"" . $_POST["email"] . "\",\"" . $_POST["message"] . "\"\n");
            fclose($fp);
         }
        }
        ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Feedback</title>
    <style type="text/css">
     
    body{
    display: flex;
     
    min-height:100vh;
    background-image: url('bg.png');
    background-size:1800px;
    font-family: Didot, serif;
    }
    input{
      width: 100%;
      padding: 12px 20px;
      margin: 8px 0;
      border: none;
      box-sizing: border-box;
      border-bottom: 2px solid #008080;
    }
     
    }
    div{
    width: 500px;
    height: 700px;
    border-radius:18px;
    padding:10px;
    border-width: 5px;
    border-color:gold;
    top:100px;
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #8ebf42;
    }
     
    }
    .header {
      padding: 60px;
      text-align: center;
      background: #1abc9c;
      color: white;
      font-size: 30px;
    }
     
    h1{
        text-align: center;
    }
    footer{
        bottom:0%;
        height:45px;
        position:absolute;
        color:white;
        background-color: black;
        width:100%;
        text-align: center;
    }
    .formp{
        color: green;
    }
     
    
    </style>
     
    
    </head>
     
    <body>
        <div class="header">
            <h1>Feedbackformular</h1>
        </div>
        <div>
        <form target="_blank" method="post">
            
            <br>
            <label for="name">Username:</label>
            <input type="name" id="username" name="username" required>
            <br>    
            <br>
            <label for="email">Enter your email:</label>
            <input type="email" id="email" name="email" required>
            <br>
            <br>
            <label for="text">Feedback:</label>
            <input type="text" id="message" name="message" required>
     
    </textarea>
    <br>
    <br>
           
               <input type="submit" value="Submit" name="submit">
     
    
        </form>
    </div>
    <?php
     if(isset( $_POST["submit"]))
     {
             echo "<p class='formp'>Es wurde gesendet!</p>";
    }
    ?>
     
    <footer>
      <p>&copy;</p></footer>
     </body>
    </html>

【问题讨论】:

  • 我没有看到 cookie 上设置了计数器值,只有计数器,第二个实例的计数器拼写错误。
  • 那我该怎么办?你能帮我吗?

标签: php html css csv


【解决方案1】:

如果我理解正确,您的问题与您的这部分代码有关。您想设置一个 cookie 来跟踪向服务器提交请求的次数。

 if (isset($_POST['submit'])) {
   $counter=0;
   setcookie("username",$_POST['username'],time()+60*60*24,"/");
   $conter = ++$_COOKIE['username']+1;
 }

1:变量名不一致。
您将$counter 设置为您的计数器变量。稍后你称它为$conter。它们应该具有相同的变量名。

 if (isset($_POST['submit'])) {
   $counter = 0;
   setcookie("username", $_POST['username'], time()+60*60*24,"/");
   $counter = ++$_COOKIE['username']+1;
 }

2:制作计数器全局变量。 如果您在每次用户提交表单时将计数器设置为 0,然后再对其添加 +1,则计数器将始终为最小值 0 或最大值为 1。相反,将计数器设置为等于 0在提交值之前。

 $counter = 0;
 if (isset($_POST['submit'])) {
   setcookie("username", $_POST['username'], time()+60*60*24,"/");
   $counter= ++$_COOKIE['username']+1;
 }

3:设置Cookie
setcookie(name, value, expire, path, ...)是PHPsetcookie签名。如果您想跟踪提交计数,我对为什么将 cookie 设置为用户名感到困惑。我找不到它在其他任何地方使用,所以我假设将它设置为计数 cookie 而不是用户名是安全的。将 +1 添加到 counter,然后插入计数作为值。该值将存储为字符串。

 $counter = '0';
 if (isset($_POST['submit'])) {
   $counter = $counter + 1;
   setcookie("counter", $counter, time()+60*60*24,"/");
 }

4:用cookie初始化计数器。
检查 cookie 是否存在,如果它的值为 0 或更大,则将计数器设置为等于 cookie 值。如果 cookie 不存在,请将计数器设置为 0。

 $counter = '0';
 if( !isset($_COOKIE['counter']) && $_COOKIE['counter'] >= 0 ) {
   $counter = $_COOKIE['counter'];
 }

 if (isset($_POST['submit'])) {
   $counter = $counter + 1;
   setcookie("counter", $counter, time()+60*60*24,"/");
 }

5:结果
以下代码会将 counter 变量设置为 0。如果存在 cookie 计数器,请将计数器设置为 cookie 值。这将允许它检索先前设置的计数器,即使在页面刷新或用户移动到不同页面后也是如此。

 // Set the counter to equal the current number of submissions.
 $counter = '0';
 if( !isset($_COOKIE['counter']) && $_COOKIE['counter'] >= 0 ) {
   $counter = $_COOKIE['counter'];
 }

此代码将在每次使用 POST 提交表单时增加计数器并设置 cookie。稍后可以使用$counter 或从计数器 cookie 中检索该值。

 // When the form is submitted, increase the counter and set the cookie.
 if (isset($_POST['submit'])) {
   $counter = $counter + 1;
   setcookie("counter", $counter, time()+60*60*24,"/");
 }

【讨论】:

  • 谢谢!我是 PHP 新手,我的知识很少。感谢您的改进
  • 如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案。没有义务这样做。
  • 我做到了。谢谢!
猜你喜欢
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2019-06-02
  • 2011-10-27
  • 2021-05-04
相关资源
最近更新 更多