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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度