且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在php中包含页面中使用session

更新时间:2023-12-03 23:36:34

你可以调用session_start您喜欢的时间,如果会话已经打开,它将不会以任何形式更改它。不过,我建议将它放在一个实用程序文件中,在包含任何其他内容之前包含它,首先,在响应主体中写入任何内容之前(除非您使用输出缓冲)。我通常创建一个config.php文件,其中包含所有必要的配置设置和初始化过程,包括启动会话。

注意:如果是无cookie会话,您可能还需要考虑其他因素。 / BLOCKQUOTE>

hi all

i start a programming of simple cms

i created a index , contact , faq , ... page that all of these pages use from 2 files header.php and footer.php

now i have a problem with some code like usage session

if i use session_start() function in header , this code repeat in all page that use from header.php ... and this action maybe do conflict over than other page

what is a best solution for use code (like session or cookie) in a shared page (like header.php) ?

please help me

thanks a lot

You can call session_start as many times you like, if a session is already opened it won't change it in any form. Still, I suggest to put it in an utility file, that you include before including anything else, and first of all, before writing anything in the response body (except if you use output buffering). I am usually creating a config.php file, that contains all necessary configuration settings and initialization procedures, including starting session.
A note: in case of cookieless session you might need to have other considerations too.