473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Session_start() result in headers already sent

The1corrupted
134 100+
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
14 1868
ronverdonk
4,258 Expert 4TB
What is the error returned by the server, what is blown up and how?

Ronald :cool:
Feb 20 '07 #2
The1corrupted
134 100+
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
4,258 Expert 4TB
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
134 100+
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
4,258 Expert 4TB
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
134 100+
Sweet! Thanks a ton!
Feb 20 '07 #7
ronverdonk
4,258 Expert 4TB
You are welcome.

Ronald :cool:
Feb 20 '07 #8
The1corrupted
134 100+
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
4,258 Expert 4TB
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
134 100+
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
4,258 Expert 4TB
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
134 100+
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
4,258 Expert 4TB
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
134 100+
I'm going to have a session idle out jobby using time stamps when I get everything else set up.
Feb 20 '07 #15

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

Similar topics

2
by: mr_burns | last post by:
Hi, i am getting the following errors when running my php script: Warning: Cannot send session cookie - headers already sent by (output started at ...
3
by: Florence HENRY | last post by:
Hello, I've searched about my problem on google before posting, but I didn't find anything relevant... I have a problem with session_start. Here is my code : <html> <head> <?php...
4
by: Bob | last post by:
Seem to have a problem ending a session. I get the following message. Warning: session_start(): Cannot send session cookie - headers already sent by (output started at...
8
by: lkrubner | last post by:
I was trying to set a cookie before I called session_start() and it was giving me an error. But isn't sessions really just a cookie? Why would it matter if I sent a cookie before session_start? Can...
3
by: lawrence k | last post by:
I'm getting this warning: PHP Warning: session_start(): Cannot send session cache limiter - headers already sent in...
19
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache...
1
by: juergen | last post by:
Hi, there are many postings on this subject, I have read them put don't have an answer. I get these warnings: Cannot send session cookie - headers already sent in, but my php-scripts are okay. ...
2
by: IchBin | last post by:
I am getting the error message below when ever I try to start my script on two different servers. I am not getting any errors off of my PC. I have looked around and found the answer a simple one. I...
4
by: three-eight-hotel | last post by:
I'm somewhat of a newbie to PHP coding, but have developed a site using the technology, and have been pleasantly surprised by the capabilities offered. I am more comfortable in the ASP world,...
3
by: sudhakarsh | last post by:
Warning: session_start(): open(/tmp\sess_bd6e0da1ed76c6b86f102376e2efb508, O_RDWR) failed: No such file or directory (2) in C:\Program Files\Apache Group\Apache2\htdocs\Care2x\installer\Installer.php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.