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