dongzi0850
dongzi0850
2014-01-14 01:03

<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 dth2331 5年前

    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.

    点赞 评论 复制链接分享
  • dqb14659 dqb14659 7年前

    when you go to next page after below line try printing all the sessions and see if you get it.

    include 'functions.php';
    print_r($_SESSION);
    

    one possibility i see that there could be some headers already sent in function.php before calling

    session_start();
    
    点赞 评论 复制链接分享
  • dougou1943 dougou1943 6年前

    Well, even being a long time after the solution, a found another, i had a problem with $_SESSION after cleaning browser data, my connection script wasn't receiving the data, so i did create another script thats receive the data by $_REQUEST and then transforms these data into a new session. Hope that helps anyone.

    点赞 评论 复制链接分享