【问题标题】:how to store a value of an HTML input in a cookie in PHP如何在 PHP 的 cookie 中存储 HTML 输入的值
【发布时间】:2019-10-06 08:33:20
【问题描述】:

这里是html代码

<!

DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>



        <form action="test.php" method="POST">

            <input type="text" name="pseudo">

            <input type="submit">

        </form>

</body>
</html>

表单返回名为 test.php 的页面中的输入值和 coockie 中的股票,但它不起作用

这里是php代码

<?php

setcookie("pseudo", $_POST['pseudo'], time() + 365*24*3600);


echo "Your name is " . $_COOKIE['pseudo'];

【问题讨论】:

  • $_COOKIE['pseudo'] 无法立即使用,除非您执行刷新(即您的 php 读取 cookie)或在设置 cookie 标头的同时手动设置 $_COOKIE['pseudo']

标签: php html cookies


【解决方案1】:

您缺少设置 cookie 所需的 cookie 名称参数。 请看下面的实现

setcookie("Cookie_Name", $_POST['pseudo'], time() + 365*24*3600);

【讨论】:

  • 据我所知,OP 使用“伪”作为 cookie 名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 2017-03-27
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
相关资源
最近更新 更多