PHP会话和路径设置 [英] php session and path setting

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

问题描述

我想将源文件夹分成两个部分:包含您在地址栏中键入代码的文件夹,以及构成页面部分(平铺)和其他代码(类等)的文件夹.因此,在我添加的每个php文件的开头:

I would like to separate my source folders into two: The folders that contain the code that you type into the address bar and those that make up parts of the page (tiles) and other code (classes, etc). So at the start of every php file I added:

<?php
// index.php
include("config.php");
include("session.php");
?>

到目前为止,Config仅包含此内容,但是如果我需要其他目录(日志等),可以允许我扩展

Config contains just this so far, but allows me to expand if I need other directories (logs, etc.)

<?php
// config.php
$_PATHS["base"]      = dirname(dirname(__FILE__)) . "\\";
$_PATHS["includes"]  = $_PATHS["base"] . "includes\\";
ini_set("include_path", "$_PATHS[includes]");
?>

此外,会话在构造函数中还有对session_start的调用.它还需要其他类包含在其他位置-需要在会话包含之前列出配置.但是我得到了错误

And session has amongst other things, in the constructor, a call to session_start. It also requires other classes which are included elsewhere - which necessitates the config being listed before the session inclusion. However I get the error

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started ...

如果我切换了包含,则该特定错误消失了,但是我需要开始手动调整到头文件的链接.无论如何,是否需要先设置目录并仍然可以使用会话,还是必须session_start是文件包含的第一件事?

If I switch the includes around that particular error goes away but I need to start manually munging the links to the header files. Is there anyway of setting the directories first and still being able to use sessions or must session_start be the very first thing the a file includes?

推荐答案

我不知道您是否已经尝试过此方法,但是作为一种测试方法,请删除config.php include并将其粘贴在其中.

I don't know if you have already tried this, but as a means of testing remove the config.php include and paste the config code in there instead.

所以这个:

<?php
// index.php
include("config.php");
include("session.php");
?>

成为这个:

<?php
// config
$_PATHS["base"]      = dirname(dirname(__FILE__)) . "\\";
$_PATHS["includes"]  = $_PATHS["base"] . "includes\\";
ini_set("include_path", "$_PATHS[includes]");
//index
include("session.php");
?>

如果有效,则说明您的config.php文件有问题* [请参见下文],如果没有,错误是否仍指向ini_set行? [根据您上面的评论,这是当前错误的出处]

If it works then you have a problem with your config.php file* [see below], if it doesn't, does the error still point to the ini_set line? [assuming from your above comment that's where the current error points]

*我记得[不久前我承认]阅读过一次,文件为UTF-8可能会破坏会话.试图找到一个链接

*I remember reading once [a while ago I admit] that a file being UTF-8 might screw up sessions. Trying to find a link

好吧,我发现有人提交了有关UTF-8和session_start的错误报告.显然这不是一个错误-我没有研究原因-但无论哪种方式,它都不是完全相同的问题.一种UTF-8编码确实会导致会话错误,而不会导致您得到的Cookie错误. 如果有兴趣,请参见此处-UTF-8错误

Ok I found someone who submitted a bug report concerning UTF-8 and session_start. Apparantly it isn't a bug - I didn't look into why - but either way it isn't quite the same issue. A type of UTF-8 encoding does cause session errors, just not the cookie error you are getting. See here if you are interested - UTF-8 Error

这篇关于PHP会话和路径设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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