doutao4938 2018-07-03 02:39
浏览 51

在服务器端脚本之间传递PHP SESSION变量

Is it possible to have two scripts that both run on say a cron jobs, and pass a SESSION variable from Script1.php to Script2.php? I understand the most common usage of SESSION variables is to create the typical unique ID cookie for users accessing a site via the browser and carrying information about that user from page to page. What would happen if I set a SESSION variable in a script that was executed server side and then run another to attempt and pick up the particular variable? Would the server generate a unique ID for itself that would expire after the likely default of 1440 seconds?

My thoughts are that, I build a lot of PHP scripts for things like consuming APIs where there is multiple files that often need to run in some sort of sequence. Instead of passing information from one script to next via MySQL database updates and selects, I'd like to use the more lightweight and seemingly more convenient SESSION variable method.

  • 写回答

1条回答 默认 最新

  • dqaxw44567 2018-07-03 03:42
    关注

    Session variables are not for passing data between scripts. They are stored by the server, so the cli, which executes php scripts run by cron, would not have a session at all. Sessions are not something that makes the least bit of sense to use for passing data/state/whatever it is you are talking about between two processes. If you want IPC then you need to either use threading or another language. PHP has traditionally used a database of some sorts for sharing data between processes. There is a reason for this.

    The short answer to your original question "What would happen if I set a SESSION variable in a script that was executed server side and then run another to attempt and pick up the particular variable?" Is that if the script was executed by the CLI you would get an error trying to access something that wasn't there. If you were accessing it through a script executed by Apache, for instance, you would put the data into the session belonging to the client that browsed to the URI. The next script would not be able to access that unless it was the same client.

    You could set and get environment variables for cli scripts to pass data I suppose.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?