Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

php session doesn't work in cli

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...