dqrb4228 2013-06-16 12:00
浏览 55
已采纳

如何在cookie中存储名称并回显它?

Alright so what I am doing is I have a Text box and submit button that posts to the current page. The user can put their name in it. It creates a cookie and that cookie will echo out their name on the page after they pressed submit.

But I can't seem to do this in one page refresh.

Here is what I have so far:

<?php 
error_reporting(E_ALL ^ E_NOTICE);
?>

<form action="#" method="post">
Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php 
$post = $_POST["fname"]; 

$expire=time()+60*60*24*30;
setcookie("user", $post, $expire);

echo $_COOKIE["user"];
?>

Thanks!

  • 写回答

3条回答 默认 最新

  • dongxiao3694 2013-06-16 12:30
    关注

    Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS array:

    <?php
       if (isset($_POST['fname'])) {
           $post = $_POST["fname"]; 
    
           $expire=time()+60*60*24*30;
           setcookie("user", $post, $expire);
          header("Location: index.php"); //notice the redirect?
        }
     ?>
    
    <form action="" method="post">
       Name: <input type="text" name="fname">
       <input type="submit">
    </form>
    
    <?php if (isset($_COOKIE['user'])) {
       echo $_COOKIE["user"];
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料