0

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 \ reading 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!

Eran Levi
  • 363
  • 7
  • 22
  • Cookies are saved on the client. When running PHP from the command line, there is no client. All the `setcookie()` does is issue a header in the response to the client. The header contains the cookie name and value. No headers are issued/printed from the command line. – Jonathan M Jul 23 '14 at 15:51
  • Hi Jonathen and thanks for your reply!, so, is there any "work around" to save\read cookies, after running php script from the terminal? – Eran Levi Jul 23 '14 at 15:52
  • Where would you like to read them *from* and write them *to*? There's no client. – Jonathan M Jul 23 '14 at 15:54
  • What are you actually trying to do by running the script in CLI? – Jonathan M Jul 23 '14 at 15:55
  • right, understood, I just thought, if there is external service or similar, that will open a client, and will run it from there, It sound like a basic problem... Thanks! – Eran Levi Jul 23 '14 at 15:57
  • I have a script that run every 2 seconds (shell script), then Im checking if I sent a value in the last 30 seconds, If i did, nothing will happen , otherwise, I'm sending this value. – Eran Levi Jul 23 '14 at 15:58
  • Duplicate, and better answers here: [Is it possible to read cookie/session value while executing PHP5 script through command prompt?](https://stackoverflow.com/questions/7578595/is-it-possible-to-read-cookie-session-value-while-executing-php5-script-through) – Déjà vu Jun 19 '20 at 06:31

2 Answers2

0

If you're willing to run some exec() functions, you can save cookies and things - see

How to get past the login page with Wget?

Community
  • 1
  • 1
Charlene Barina
  • 187
  • 1
  • 11
  • That's a solution to a different problem than this. (Also, it's not a very good solution; the `curl` functions can store cookies just as well.) –  Jul 23 '14 at 16:20
0

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));
  • Thank you for your answer, the thing is, that it have to be existed for 30 seconds and then to kill itself, just like a cookie, i can write another shell script that delete this file, but It sounds like an ugly solution, also, I really don't want to create and delete files every 2 seconds, that will kill my HD :) , don't you think? thanks anyway! – Eran Levi Jul 23 '14 at 16:46
  • If you need to keep track of when the script was last run, store the time in a file. Don't worry about the hard disk; it'll be fine. –  Jul 23 '14 at 17:11
  • Ok, I thought there is a better way, probably not, Thanks anyway!! – Eran Levi Jul 23 '14 at 18:06