9

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!

kregg
  • 93
  • 1
  • 3

4 Answers4

10

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().

deceze
  • 510,633
  • 85
  • 743
  • 889
2

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"

1. get the login page

$WGET -O index.html "http://mail.yahoo.com"

2. fish out any hidden values

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' '&'`

3 manually add non-hidden vars

FORMVARS=".persistent=y&login=USERNAME&passwd=SECRET"

4. post form-data to the target

$WGET -O login.html --post-data="${HIDDENVARS}&${FORMVARS}" "https://login.yahoo.com/config/login?"

5. profit ;)

jmullee
  • 51
  • 3
0

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.

Peter Lindqvist
  • 10,122
  • 3
  • 41
  • 60
  • Not really. By running from an actual request, it "sees" the cookies from the person requesting the page at that exact moment, not the cookies from the person whose cron-job you're running. The same restriction @deceze cite would apply here. You'd need to know beforehand the *session_id* and use it to start a session. Which them makes pseudo-cron not a good option unless you can't use cron. – Carlos Lima Nov 09 '09 at 07:46
  • Yes, you're right. But I thought it was relevant since the question made no reference to which particular session to access. But given you want to access a session with a known session id the answer from deceze would be accurate. – Peter Lindqvist Nov 09 '09 at 08:27
0

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.