i am new to cron jobs and i have done much searching on this topic but i couldn't understand it fully. can cron jobs access cookies or session variables?
thanks!
i am new to cron jobs and i have done much searching on this topic but i couldn't understand it fully. can cron jobs access cookies or session variables?
thanks!
A cron job won't be able to access cookies, since by definition it is not being invoked from a web browser request. Only the web browser stores a cookie, which contains the session id. No web browser, no cookie, no session.
Having said that, if you do know the session id somehow, you should be able to start the session manually by feeding the id into session_id()
before using session_start()
.
Another answer is that one can use session-cookie files with command-line web clients. example:
C=~/tmp/x
WGET="wget --keep-session-cookies --load-cookies=$C --save-cookies=$C"
$WGET -O index.html "http://mail.yahoo.com"
HIDDENVARS=`cat index.html | tr '\r\n\t' ' ' | tr -s ' ' | sed "s|> *<|>~<|g" | tr '~' '\n' | \
grep -i "<input .*hidden" | sed "s|.*name=\"\([^\"]*\)\".*value=\"\([^\"]*\)\".*$|\1=\2|g" | tr '\n' '&'`
FORMVARS=".persistent=y&login=USERNAME&passwd=SECRET"
$WGET -O login.html --post-data="${HIDDENVARS}&${FORMVARS}" "https://login.yahoo.com/config/login?"
There is a project called pseudo cron that i suppose would be able to access session and/or cookies. But it would make no sense to me.
A time-based, server-side "cookie" to run within a cron job:
Use file() to read the server-side "cookie" - a text file.
Use fopen,fwrite,fclose to write the server-side "cookie".
Use implode if storing more than one datavalue in your "cookie":
Main code:
<?php
$username = 'whatever';
$usercookie = 'http://yourdomainname.com/cookiebank/'.$username.'cookie.txt';
$oldtime = file($usercookie);
if(is_array($oldtime)) {
$cookievalue = implode(" ",$oldtime);
}
else {
$cookievalue = $oldtime;
}
// $cookievalue can then be used as you wish...
$newtime = date("M j G:i:s");
$newtime = strtotime($newtime)*1000; // current time in msec
$myfile = fopen($usercookie, "w") or die("Unable to open file!");
fwrite($myfile, $newtime);
fclose($myfile);
echo 'done';
?>
An approach straight from the mind of Heath Robinson - not very elegant but it works if you want to apply a cookie-like behaviour to a cron job.