我是php的新手,我想在php cli中使用会话,但是我没有运气.但是当我使用浏览器运行它时,相同的代码也可以工作.我做错什么了吗?我搜索了一下,发现无法在php 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?
推荐答案您必须先搜索和阅读在线文章,然后才能在stackoverflow中提问.甚至还有相同的问题.
You have to search and read online articles before asking question in stackoverflow. there are even same questions on stack.
是的,PHP会话主要是为了与cgi SAPI一起使用而创建的,据我所知,使用CLI SAPI的php会话是没有用的.原因很简单,http是无状态的,我们使用cookie和会话来记住服务器上的用户并跟踪他/她的行为.但是在控制台中,为什么根本需要会话?
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???
请考虑在控制台中运行php时,没有浏览器,没有http请求,因此没有http标头,因此也没有http COOKIE标头.
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 .
如果您需要存储空间以供不同的php进程使用(CLI SAPI)您可以为此目的使用数据库.但是,如果您出于任何原因坚持使用php会话,在这里看看:
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 :
是否有可能通过命令提示符执行PHP5脚本时读取cookie/会话值?
更多推荐
PHP会话在CLI中不起作用
发布评论