php:基于cookie的会话 [英] php: cookie based sessions

查看:128
本文介绍了php:基于cookie的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何机构有关于如何集成基于cookie的会话系统的任何信息/链接?我使用的文件/ mysql,我目前使用memcached。我想玩apc会议,但我认为我会去饼干,只是我不知道很多。

does any body have any info/links as to how to integrate a cookie based session system? i've used file/mysql, and am currently using memcached. i wanted to play with apc sessions, but thought i'd give a go at cookies, only i don't know much about it.

我想我会有要写自己的会话处理程序类?

i imagine i'd have to write my own session handler class?

推荐答案

在PHP会话数据通常存储在一个文件中。存储在cookie中的唯一的事情是会话标识符。当启用会话并找到有效的会话cookie时,PHP会将用户会话数据从文件加载到超级全局,名为funnily enough SESSION。

In PHP session data is usually stored in a file. The only thing stored in the cookie is a session identifier. When sessions are enabled and a valid session cookie is found, PHP loads the users session data from the file into a super global called funnily enough SESSION.

基本会话 session_start(); 在任何文本发送到浏览器之前调用。那么使用简单的数组索引,将项目添加到会话对象或从会话对象中删除例如

Basic sessions are started using session_start(); called before any text is sent to the browser. then items are added to or removed from the session object using simple array indexing eg.

$_SESSION['favcolour'] = 'blue'; 

稍后...

$favcolour = $_SESSION['favcolour'];

只能通过调用

 set_cookie('favcolour','blue'[,other params]);

,然后从任何文本发送到浏览器,然后从超级全球的

before any text is sent to the browser, then retrieved from the cookie superglobal

$favcolour = $_COOKIE['favcolour'];

您不需要调用 session_start()如果只执行cookie的会话。

you don't need to call session_start() if doing cookie only sessions.

可选的[,其他params]更高级,可以在这里阅读 http://www.php.net/manual/en/function.setcookie.php

the optional [,other params] are more advanced and can be read about here http://www.php.net/manual/en/function.setcookie.php

会话可以成为一个非常复杂的讨论,我建议在他们做一些轻的工作,然后扩大你的知识。

Sessions can become a very complex discussion, I'd suggest doing some light work in them and then expand your knowledge.

DC

您想了解的有关PHP会话的所有信息

all you ever wanted to know about PHP sessions

http://www.php.net/manual/en/book.session.php

DC

要重用PHP的会话处理代码,您需要使用 session_set_save_handler 然后在该处理程序中完全没有。

To reuse PHP's session handling code you will need to add a write handler using session_set_save_handler and then do exactly nothing in that handler. That's because its called after the output to the browser is closed therefore you cannot send anything to the browser.

在将非标题数据写入浏览器之前,使用set_cookie函数和存储$ _SESSION数组的内容(序列化和加密后)转换为cookie。当应用程序启动时,您可以读取cookie unserialise它并将其放入$ _SESSION数组。

Before writing non header data to the browser use the set_cookie functions and store the contents of the $_SESSION array (after serialising and encrypting) into a cookie. when the applications start you can read the cookie unserialise it and put it into the $_SESSION array.

这是一个快速提示,我从来没有做过,我喜欢写我所有的自己的cookie代码。可能有一些gotcha的,但它不难的几个测试应该找到任何gotcha的。

That's a quick hint at what to do as I have never done it, I prefer to write all my own cookie code. There may be some gotcha's but its not hard a few tests should find any gotcha's.

DC

这篇关于php:基于cookie的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆