dongzi0850 2014-01-14 01:03
浏览 46
已采纳

<PHP>会话不起作用(Cookie不发送?)[重复]

This question already has an answer here:

Edit 4: Why this question is not a duplicate to the linked question

While the underlying problem turned out to be the same as in the linked question, I did not receive the error message Headers not send, so the answer, which I read, was not useful to me at this time. Thus, this question, while maybe overly long, has merit on it's own.

Original Post

Sorry to bother you, but after two days of research, I broke down. I'll try to give the maximum amount of information.

What I'm doing:

I am currently writing a login system, just for fun. Registering, entering data into the database, loging in works perfectly fine. I host with 1and1, my database is there as well.

Now to keep going, I wanted to start with sessions. Initially, all seemed well and simple.

session_start();
//returns true when put into an if()

$_SESSION['id'] = //user ID read from databse
echo $_SESSION['id']; //prints out the ID just fine.

When I go to another page, I instantly call session_start(); again. In fact, ALL my pages start with

include 'functions.php';
$con = startup();

//statup(); is in functions.php, connects to a databse,
//calls session_start(); succesfully, then returns the connection. 

The problem should not be in the function, since it works fine, also I tried the connection/session starting thing to be in every file, rather than a shared function - didn't change anything.

But none of the session variables retain values.

Possible Problems

Cookies are not send

javascript:alert(document.cookie); in Firefox gives me

__qca=P0-1714752251-1385128732260

Chrome tells me no cookies are received, also none blocked.

Given that many people have the problem of cookies being unable to be send at the moment they call session_start(); because cookies have been send before, I assume I don't need to call anything regarding sending cookies in my code.

I don't know what to make of this.

My host doesn't allow sessions

Was another idea of mine. However, I can see my php_info(); and it tells me:

Session setting

Session Support enabled

session.cache_expire 180

session.cookie_domain no value

session.cookie_httponly Off

session.cookie_lifetime 0

session.cookie_path /

session.use_cookies On

session.use_only_cookies On

session.use_trans_sid 0

Those are just the lines which seem somewhat relevant to me.

So, what I read from that is that sessions should work just fine, with cookies and stuff. But I don't really understand this level either.

So, thats where I'm stuck. All the components of my code work fine, I'm quite sure the syntax is right, as far as I understand it the server supports sessions.

Oh, I've also tried changing the session path from /tmp to a directory right on my webspace - It didn't seem to do anything, no data was stored on my "part" of the server.

EDIT:

Using the new information about error_reporting(E_ALL); I now recieved the generic error message

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /homepages/23/d315777983/htdocs/mtm/index.php:14) in /homepages/23/d315777983/htdocs/mtm/functions.php on line 9

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /homepages/23/d315777983/htdocs/mtm/index.php:14) in /homepages/23/d315777983/htdocs/mtm/functions.php on line 9

I guess I can start working with older suggested solutions now.

EDIT 2:

As suggested, I added print_r($_SESSION); after the include.

It just prints "Array()" (without quotation marks)

I also moved session_start back into each individual file, called BEFORE functions.php is included.

It says cookies are send at line 14 - which in my code is literally just

<?php

EDIT 3: Well, that was some wasted time. Removed whitespace at the end of functions.php, called startup(); before any html output and now it works perfectly.

</div>
  • 写回答

3条回答 默认 最新

  • dth2331 2016-01-05 09:29
    关注

    I had a similar problem and I solved it removing blank spaces in source code.

    Specifically, the error occurs when an answer is send to the client prematurely. This can happen on accident when you have whitespace outside of the <?php ?>-tags. One way to mostly prevent this mistake is by simply not having a ?> at the end of the document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架