Kjeld Schmidt Kjeld Schmidt - 5 months ago 15
PHP Question

*Solved*<PHP> Sessions don't work (Cookie not send?)

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.

I sincerely hope I didn't annoy you with an overly long, simple question because I made a small misstake somewhere. I genuinely tried to work this out by googling other peoples problems for two days now.

I thank you all for your time, patience and help.

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


Well, I'll read more answers on this for now. Thanks a lot!

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.

Thanks a lot to all of you!

Answer

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();
Comments