php setcookie vs Zend_Http_Cookie

2021-12-29 00:00:00 php zend-framework

为什么这段代码不起作用,我怎样才能让它像

Why this code not working, and how can I make it works like

setcookie('cookie_name','cookie_value');

不创建cookie的代码:

$cookie=new Zend_Http_Cookie('cookie_name','cookie_value','.google.com');

或者有什么区别:

setcookie('cookie_name','cookie_value');

对比

$cookie=new Zend_Http_Cookie('cookie_name','cookie_value','.google.com');

谢谢

推荐答案

Zend_Http_Cookie 不是用来设置 cookie 的,它是 Zend_Http_Client 的配套类.假设您想从站点中截取一些内容,但该内容只有在您登录后才可用.您可以使用 Zend_Http_Client 将您的凭据发布到登录表单,然后服务器将发送回会话 cookie.然后,您可以在对要抓取的页面的后续请求中包含此会话 cookie,以模拟登录用户查看该页面.

Zend_Http_Cookie is not for setting cookies, it is a companion class for Zend_Http_Client. Let's say you wanted to screen scape some content off a site but that content is only available if you are logged in. You could use Zend_Http_Client to post your credentials to the login form, the server would then send back a session cookie. You could then include this session cookie in a subsequent request to the page you want to scrape in order to simulate a logged in user viewing that page.

要在 ZF 中设置 cookie,您可以只使用本机 PHP 函数,或者可能将数据存储在会话中.

To set cookies in ZF you can just use the native PHP function, or possibly store the data in the session instead.

相关文章