【发布时间】: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>©</p></footer>
</body>
</html>
【问题讨论】:
-
我没有看到 cookie 上设置了计数器值,只有计数器,第二个实例的计数器拼写错误。
-
那我该怎么办?你能帮我吗?