在Cronjob(Crontab)中使用会话 [英] Use Session in Cronjob (Crontab)

查看:133
本文介绍了在Cronjob(Crontab)中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在cronjob中使用会话吗?



我使用的脚本是:

  session_start(); 

if(empty($ _ SESSION ['startwert'])){$ startwert = 0;}
else {$ startwert = $ _SESSION ['startwert'];}

if(empty($ _ SESSION ['zielwert'])){$ zielwert = 10000;}
else {$ zielwert = $ _SESSION ['zielwert'];}


....


$ _SESSION ['startwert'] = $ zielwert;
$ _SESSION ['zielwert'] = $ zielwert + 10000;
echoStartwert:。$ _ SESSION ['startwert']。< br>;
echoZielwert:。$ _ SESSION ['zielwert']。< br>;

但是Cron总是开始将startwert设置为10000,将zielwert设置为20000,




$ b pre> / usr / bin / wget -O - http://mydomain.com/script.php

$

解决方案

如果你通过 wget 从cron调用PHP脚本,使用 - save-cookies 选项;如果通过 curl ,请使用 - cookie-jar 。 (如果你通过 php -f [...] 或类似的方式调用PHP脚本,那么你首先需要通过改为



例如:

  wget --load-cookies / tmp / cron-session-cookies --save-cookies / tmp / cron-session-cookies --keep-session -cookies [...] 

  curl -b --cookie-jar / tmp / cron-session-cookies [...] 


wget 默认情况下不保存会话cookie,因此您需要它,因此 -keep-session-cookies 选项; curl 默认情况下保存它们,所以所有必要的是 -b 启用Cookie和 --cookie-jar 告诉 curl 在哪里可以找到它们。在任何情况下,用您已经传递给程序的任何选项和参数替换 [...] ,并调整cookie jar文件的位置。


Hi is it possible to use sessions in a cronjob?

The Script I use is:

session_start();

if(empty($_SESSION['startwert'])){$startwert = 0;}
else {$startwert = $_SESSION['startwert'];}

if(empty($_SESSION['zielwert'])){$zielwert = 10000;}
else {$zielwert = $_SESSION['zielwert'];}


....


$_SESSION['startwert'] = $zielwert;
$_SESSION['zielwert'] = $zielwert + 10000;
echo "Startwert: ".$_SESSION['startwert']."<br>";
echo "Zielwert: ".$_SESSION['zielwert']."<br>";

But the Cron allways start set "startwert" to 10000 and "zielwert" to 20000 and it does not increase the values.


Ok now I have tried this.

/usr/bin/wget -O - http://mydomain.com/script.php

But the cron starts allways with 10000 and 20000. Any ideas?

解决方案

If you're invoking the PHP script from cron via wget, use the --save-cookies option; if via curl, use --cookie-jar. (If you're invoking the PHP script via php -f [...] or similar, then you'll first need to invoke it via wget or curl instead.)

For example:

wget --load-cookies /tmp/cron-session-cookies --save-cookies /tmp/cron-session-cookies --keep-session-cookies [...]

or

curl -b --cookie-jar /tmp/cron-session-cookies [...]

wget by default doesn't save session cookies, which you want it to do, hence the --keep-session-cookies option; curl by default does save them, so all that's necessary is -b to enable cookies and --cookie-jar to tell curl where to find them. In either case, replace the [...] with whatever options and arguments you're already passing to the program, and adjust the location of the cookie jar file to taste.

这篇关于在Cronjob(Crontab)中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆