Hi I'm new to php I wanted to use session in php cli but I had no luck. but same code works when I run it with browser. am I doing anything wrong? I searched and I found out it is not possible to use session in php cli but I dont understand why and what is an alternative approach?
-
2Session is a web concept, to cope with the webs lack of state. PHP written for the CLI has state and therefore session is not available – RiggsFolly Sep 23 '16 at 20:33
-
_"am I doing anything wrong?"_ Maybe. How can we know if you don't show us the code? – ChrisGPT was on strike Sep 23 '16 at 20:35
-
now I got my answer from @mhndev. and it's like I shouldn't have asked this question and do a googling before asking :) – mjwaldner Sep 23 '16 at 20:43
-
1Possible duplicate of [Is it possible to read cookie/session value while executing PHP5 script through command prompt?](http://stackoverflow.com/questions/7578595/is-it-possible-to-read-cookie-session-value-while-executing-php5-script-through) – Jonathan Kuhn Sep 23 '16 at 20:57
3 Answers
You have to search and read online articles before asking question in stackoverflow. there are even same questions on stack.
yes thats true , PHP sessions are mainly created to be used with cgi SAPI, and as I know there is no use for php sessions with CLI SAPI. and the reason is simple , http is stateless and we use cookie and session to remember user on server and track his/her actions. but in console , why you need session at all???
consider that when running php in console there is no browser thhere is no http request, so there is no http header, so there is no http COOKIE header .
if you need a storage to be available to different php processes which are (CLI SAPI) you can use database for this purpose. but still if you insist on using php session for any reason, take a look at here :
Is it possible to read cookie/session value while executing PHP5 script through command prompt?

- 1
- 1

- 2,191
- 4
- 31
- 59
-
thanks, it seems I didn't know session real meaning and why use them. I have to read more about session. sorry if I ask silly question I never developed web programs – mjwaldner Sep 23 '16 at 20:36
You can't set session in cli mode. Session is part of web - you visit through internet resources. When you are using CLI, you are not calling internet.

- 4,318
- 4
- 26
- 43
As you may have found out by now, there are many reasons to use sessions in a CLI PHP script -- unit-testing your web-application being only one of the them. Another way to put this question: does PHP have a library like python's requests module, which has an elegant and intuitive Session construct? See python requests' Session object. I have been absent from perl for a while, buit there is no doubt perl would also have one.

- 21
- 2