PHP会话在CLI中不起作用

编程入门 行业动态 更新时间:2024-10-08 22:19:51
本文介绍了PHP会话在CLI中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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中不起作用

本文发布于:2023-11-28 06:30:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   PHP   CLI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!