简体   繁体   English

PHP会话和路径设置

[英]php session and path setting

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文件的开头:

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

Config contains just this so far, but allows me to expand if I need other directories (logs, etc.) 到目前为止,Config包含此内容,但如果需要其他目录(日志等),则允许我扩展

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

And session has amongst other things, in the constructor, a call to session_start . 会话在构造函数中除其他外还有对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 ...

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? 无论如何,是否需要先设置目录并仍然可以使用会话,还是必须将session_start作为文件包含的第一件事?

The rest of that error is the exact bit that will tell you where the problem is! 该错误的其余部分将确切告诉您问题出在哪里! Chances are you have some trailing whitespace at the end of config.php. 您可能在config.php的末尾有一些空白。

(Either that, or session.php sends output before your call to session_start() , but I'm really just guessing now :) (或者,或者session.php在调用session_start()之前发送输出,但是我现在只是在猜测:)

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

So this: 所以这:

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

becomes this: 变成这个:

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

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? 如果工作正常,则说明您的config.php文件有问题* [请参见下文],如果没有,错误是否仍指向ini_set行? [assuming from your above comment that's where the current error points] [根据您的上述评论,这是当前错误的出处]

*I remember reading once [a while ago I admit] that a file being UTF-8 might screw up sessions. *我记得有一段时间(我承认)读过一次,文件为UTF-8可能会破坏会话。 Trying to find a link 试图找到一个链接

Ok I found someone who submitted a bug report concerning UTF-8 and session_start. 好的,我发现有人提交了有关UTF-8和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. 一种UTF-8编码确实会导致会话错误,而不会导致您得到的Cookie错误。 See here if you are interested - UTF-8 Error 如果您感兴趣,请参见此处-UTF-8错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM