dousha4804 2014-07-23 15:47
浏览 58
已采纳

来自命令提示符的PHP脚本 - 是否可以设置 ead cookies?

I have a simple php page, and I'm trying to run this php script is from the terminal console (ssh):

php /home/account/domains/domain.com/public_html/script.php

Now, I can run the script, the thing is, if I'm running the php script from the console, setting eading a cookie doesn't work, If I'm running the same page from the browser, everything work just fine (Read and write cookies..)

So my question is, can I run this script from the terminal (just like above), and make the cookies save somehow?

My php scrpit is simple:

setcookie("cookie", "cookie", time()+30);
if (!isset($_COOKIE["cookie"])){
echo "New Cookie is set!";
}else{
echo "The Cookie already set!";
}

Thank you very much for your help!

  • 写回答

2条回答 默认 最新

  • douyouming9180 2014-07-23 16:25
    关注

    Cookies only exist in a browser. There's no browser involved when you run a script from the command line, so there's nowhere for those cookies to be stored.

    If you need to store data across calls to a command-line script, you will need to read and write it to a file. One simple way of doing this might be:

    // At the start of the script...
    $PERSIST = unserialize(file_get_contents("myscript.cache")) ?: array();
    
    // Read data from there...
    print $PERSIST["blah"];
    // Write some
    $PERSIST["foo"] = "stuff";
    
    // At the end of the script, save it back
    file_put_contents("myscript.cache", serialize($PERSIST));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器