By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

Session_start() result in headers already sent

The1corrupted
100+
P: 134
Alright, I seriously do NOT understand sessions what so ever. Whenever I try to transfer data from page to page, the [PHP]session_start();[/PHP] is always killed because of some sort of cookie/cache limiter. I even tried the [PHP]session_register();
session_is_registered();[/PHP] script and it still blows up. I'm totally lost.
Feb 20 '07 #1
Share this Question
Share on Google+
14 Replies


ronverdonk
Expert 2.5K+
P: 4,258
What is the error returned by the server, what is blown up and how?

Ronald :cool:
Feb 20 '07 #2

The1corrupted
100+
P: 134
Oh, it just loves to blow up on my log in.

[PHP]//Compile it in one triple if-statement, comparing the username, password, and type
if ($array[2] == $pass) {
//Success for the first if statement
echo "Username is clear!<br>";
if ($array[1] == $user) {
//Success for the second if statement
echo "Password is clear!<br>";
if ($array[15] == 1) {
//If the user is type 1...
echo "Type 1 is clean!<br>
<META HTTP-EQUIV='refresh' content='30; url=more1.php'>";
} elseif ($array[15] == 2) {
//If the user is type 2...
echo "Type 2 is clean!<br>
mysql_close($tablesummon);
<META HTTP-EQUIV='refresh' content='5; url=more2.php'>";
} else {
//If it all fails and dies (bad user)...
echo "User type FAILED<br><br>User Type: ".$type."<br>
User Name: ".$user."<br>
User Password: ".$pass."<br>";
}
}
}
[/PHP]
Whenever I try to start a session in one of those if statements, it comes back with..

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Vardaes\user_conn.php:9) in C:\Vardaes\passcheck.php on line #
Feb 20 '07 #3

ronverdonk
Expert 2.5K+
P: 4,258
You get this message because you have already (somewhere earlier in your script) outputted something to the screen. The following extract article will explain how this can happen.

I will show the text of an article by wildteen88, june 2006, who can explain this a lot better then I can.

The reason why you are getting this message is because you have may have/be:
  • Whitespace before the opening php tag <?php
  • Outputting something to the browser before you use session_start, header, setcookie etc
session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\test.php:6) in C:\server\www\test.php on line 8
Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
So lets look at test.php:
Expand|Select|Wrap|Line Numbers
  1.  1 <html>
  2.  2 <head>
  3.  3 <title>Header test</title>
  4.  4 </head>
  5.  5 <body>
  6.  6 <?php
  7.  7
  8.  8 header("Location: http://www.google.com");
  9.  9
  10. 10 ?>
  11. 11 </body>
  12. 12 </html>
As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.

Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

In your case: why don't you just put the session_start() statement at the very beginning of your PHP script?

Ronald :cool:
Feb 20 '07 #4

The1corrupted
100+
P: 134
That killed the error message, now how do I use sessions to transfer data from one page to the next?
Feb 20 '07 #5

ronverdonk
Expert 2.5K+
P: 4,258
Just the standard way:

[php]
To store values
session_start();
$_SESSION['name'] = $whatever;

to read it out in another page:

session_start();
$var = $_SESSION['name'];
[/php]

Ronald :cool:
Feb 20 '07 #6

The1corrupted
100+
P: 134
Sweet! Thanks a ton!
Feb 20 '07 #7

ronverdonk
Expert 2.5K+
P: 4,258
You are welcome.

Ronald :cool:
Feb 20 '07 #8

The1corrupted
100+
P: 134
Great... um.. relavent question... How do I change data from page to page? For instance...
[PHP]
if ($dir == 1) {
++$x;
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
elseif ($dir == 3) {
--$x;
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
//Changing the Y coord
if ($dir == 2) {
++$y;
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
elseif ($dir == 4) {
--$y;
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
//Changing the Z coord
if ($dir == 5) {
++$z;
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
elseif ($dir == 6) {
--$z;
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
[/PHP]
The numbers, somehow, don't change even though those equal the session variables...
Feb 20 '07 #9

ronverdonk
Expert 2.5K+
P: 4,258
The numbers, somehow, don't change even though those equal the session variables...
You lost me. What is the relation of the $x and $y numbers to the data that is stored in the $_SESSION array?

Ronald :cool:
Feb 20 '07 #10

The1corrupted
100+
P: 134
You lost me. What is the relation of the $x and $y numbers to the data that is stored in the $_SESSION array?

Ronald :cool:
[PHP]$x=$_SESSION['xcoord'];
$y=$_SESSION['ycoord'];
$z=$_SESSION['zcoord'];[/PHP]

So do I have to input this
[PHP]
++$_SESSION['xcoord'];
[/PHP]

in order to simply add one to the $x or can I do it like so:

[PHP]
++$x[/PHP]

Oh, and these numbers have everything to do with everything working right.
Feb 20 '07 #11

ronverdonk
Expert 2.5K+
P: 4,258
You mix up the 2 completely different variables that are used.

$x is a local (local to your script) variable and can only be addressed within that script.
$_SESSION['xcoord'] is a variable in a global array and is accessable to every script that has access to that instance of the array (e.g. after doing a session_start()).

The statement [php]$x=$_SESSION['xcoord'];[/php] establishes no relation between the two. It only assigns the value of (independent) array variable $_SESSION['xcoord'] to the independent variable $x.
So when you increase variable $x, array variable $_SESSION['xcoord'] is not affected and vice versa!

When you want to keep 1 central x-coord. counter for all pages, you better use the ++$_SESSION['xcoord'].

Ronald :cool:
Feb 20 '07 #12

The1corrupted
100+
P: 134
It LIIVES!!

Thanks again.

Now.. new question on sessions... Can I use them to make a list of active users to be echoed on a new page (a who-is-online list)?
Feb 20 '07 #13

ronverdonk
Expert 2.5K+
P: 4,258
Of course you can. The $_SESSION array can be used for anything (variable wise) you like. It can hold a lot of data!

Ref. your question: ..... a more practical issue: how do you know that a user is gone? What if he does not log out but just closes his browser?

Ronald :cool:
Feb 20 '07 #14

The1corrupted
100+
P: 134
I'm going to have a session idle out jobby using time stamps when I get everything else set up.
Feb 20 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.