0

I'm trying to learn about using sessions.

saw the following code example on a tutorial:

<?php
    session_start();

    $_SESSION['username'] = "hello";

    echo $_SESSION['username'];
?>

I do get hello when I execute the page, however, according to that tutorial, I should keep getting hello from now on (until closing the browser), with this code:

<?php
    session_start();

    echo $_SESSION['username'];
?>

But I don't. what's wrong?

EDIT:

According to your comments, I've updated my code to look like this:

<?php
error_reporting(-1);
session_start();
session_name("untitled");
$_SESSION['username'] = "hello";
echo $_SESSION['username'];
echo session_id();
?>

I'm getting two no headers sent error:

Warning: session_start(): Cannot send session cookie - headers already sent by..... on line 3

Warning: session_start(): Cannot send session cache limiter - headers already sent by..... on line 3

I've looked at the general thread that deals with this error, but in my case I can't figure what's wrong with these simple lines of code.

EDIT2

If I switch between line 2 and 3 as follows, I no longer get the headers sent error on one of my servers. on another domain and server, I keep getting it. anyway, the session_id keeps on changing on every refresh on both servers and codes.

<?php
session_start();
error_reporting(-1);
session_name("untitled");
$_SESSION['username'] = "hello";
echo $_SESSION['username'];
echo session_id();
?>
rockyraw
  • 1,125
  • 2
  • 15
  • 36
  • by doing what, changing the code of your same file, or in another file? Use a conditional statement to check if the session is set/not empty. use error reporting also http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Nov 19 '15 at 21:13
  • You could also check if sessions are enabled on the webserver with the phpinfo() function: http://it2.php.net/manual/en/function.phpinfo.php – Mat Nov 19 '15 at 21:16
  • @Mat OP: *"I do get `hello` when I execute the page*. That shows that sessions are enabled. – Funk Forty Niner Nov 19 '15 at 21:17
  • my first comment is 4 minutes old. My span of attention these days is 5 on first comments. @ me if you need me when I close this tab in 1 minute. *Time is a precious commodity*. – Funk Forty Niner Nov 19 '15 at 21:18
  • check `session_id()` in both scripts. if that changes, then something is causing your sessions to get "lost". bad cookie settings being the usual culprit. – Marc B Nov 19 '15 at 21:20
  • @Fred-ii- I tried both changing same file into the second code, as well as using the second code in a new file. both failed. shouldn't both methods yield same result though? does it matter? – rockyraw Nov 19 '15 at 21:21
  • `if(isset($_SESSION['username']) && !empty($_SESSION['username'])){ echo $_SESSION['username']; } else { echo "Session died"; }` in all files using sessions. Again; error reporting. – Funk Forty Niner Nov 19 '15 at 21:22
  • Is there any output before your session_start() - even a whitespace corrupts your session. Check if error reporting is on (hard way: "include('non_existant.php');" ) – clemens321 Nov 19 '15 at 21:24
  • if it's a whitespace issue, or bom etc. then error reporting will throw a notice. – Funk Forty Niner Nov 19 '15 at 21:24
  • @MarcB you're onto it I guess. My session ID changes each time I refresh the page. why is it so? – rockyraw Nov 19 '15 at 21:25
  • *"My session ID changes each time I refresh the page."* this tells me you're not showing us the whole picture. – Funk Forty Niner Nov 19 '15 at 21:26
  • yeah, but only if error reporting (and display_errors - who checks log files) is on. @rockyraw Check if there is a "PHPSESSID" cookie in your browser – clemens321 Nov 19 '15 at 21:26
  • @rockyraw: you could try to set the session name: `session_name("WebsiteID");` - ref: http://www.php.net/manual/en/function.session-name.php – Mat Nov 19 '15 at 21:30
  • @clemens321 what's the easiest way to check for that cookie? I don't think I see it. – rockyraw Nov 19 '15 at 21:34
  • @Mat adding that line to the code doesn't make a difference, session ID still keep changing... – rockyraw Nov 19 '15 at 21:36
  • Depends on your browser. In firefox it is menu settings, privacy, show cookies (or using firebug or another developer extension if you installed it). For other browsers please google, should be easy to find – clemens321 Nov 19 '15 at 21:36
  • @rockyraw: session_name is before session_start? About the cookie... you could also try with different browsers to see if it depends on some unusual setting – Mat Nov 19 '15 at 21:41
  • @Mat there's no difference if before or after. error occuring on 3 different browsers. I've tested the same code on another server, and over there I'm getting a "headers already sent" error along with the output. any clue yet? – rockyraw Nov 19 '15 at 21:55
  • 2
    Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Federkun Nov 19 '15 at 21:57
  • [First comment...](http://stackoverflow.com/questions/33814449/session-example-doesnt-work#comment55394451_33814449) told you what to do and to have told us about that "notice" from the get-go. [Then this 2nd comment...](http://stackoverflow.com/questions/33814449/session-example-doesnt-work#comment55394823_33814449) said about a notice. But you didn't share that *"headers already sent"*. You've had us all going from the start. I'd of answered this a mere "minute" after you posting your question. Do you think I put links in comments "just cuz"? – Funk Forty Niner Nov 19 '15 at 22:16
  • @Fred-ii- The "headers sent" error appears only on another server I tested it on. I discovered it only after those comments. I have no clue what does it indicate if on the first server this error doesn't show up. – rockyraw Nov 19 '15 at 22:51
  • consult the link that was included above http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Funk Forty Niner Nov 19 '15 at 22:58
  • @Fred-ii- I've edited my thread. looked in that link, but in my specific code I can't figure what's wrong exactly. – rockyraw Nov 19 '15 at 23:17
  • Edited again, I don't think the headers sent is really the issue here, I overcome it on my of my servers. – rockyraw Nov 19 '15 at 23:30
  • @clemens321 I didn't find such cookie in any of my browsers. but actually, a cookie with that name was not found for none of the domains I have visited during the past months. – rockyraw Nov 20 '15 at 00:25
  • I opened a new updated [thread here](http://stackoverflow.com/questions/33825348/session-id-changing-on-refresh-session-cookie-doesnt-show-up-in-browser) – rockyraw Nov 20 '15 at 11:56

0 Answers0