Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

do not understand Cannot send session cookie - headers already sent

Question posted by: craigtomo (Newbie) on February 25th, 2008 06:00 PM
i have changed my login .php file to the following

this file is opened directly and is not called from any other file
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // dBase file
  4. $host=""; // Host name
  5. $username=""; // Mysql username
  6. $password=""; // Mysql password
  7. $db_name=""; // Database name
  8. $tbl_name=""; // Table name 
  9. // Connect to server and select databse.
  10. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  11. mysql_select_db("$db_name")or die("cannot select DB");
  12. session_start();
  13.  
  14. if ($_GET["op"] == "login")
  15.  {
  16.  if (!$_POST["username"] || !$_POST["password"])
  17.   {
  18.   die("You need to provide a username and password.");
  19.   }
  20.  
  21.  // Create query
  22.  $q = "SELECT * FROM `dbUsers` "
  23.   ."WHERE `username`='".$_POST["username"]."' "
  24.   ."AND `password`=PASSWORD('".$_POST["password"]."') "
  25.   ."LIMIT 1";
  26.  // Run query
  27.  $r = mysql_query($q);
  28.  
  29.  if ( $obj = @mysql_fetch_object($r) )
  30.   {
  31.   // Login good, create session variables
  32.   $_SESSION["valid_id"] = $obj->id;
  33.   $_SESSION["valid_user"] = $_POST["username"];
  34.   $_SESSION["valid_time"] = time();
  35.  
  36.   // Redirect to member page
  37.   Header("Location: members.php");
  38.   }
  39.  else
  40.   {
  41.   // Login not successful
  42.   die("Sorry, could not log you in. Wrong login information.");
  43.   }
  44.  }
  45. else
  46.  {
  47. //If all went right the Web form appears and users can log in
  48.  echo "<form action=\"?op=login\" method=\"POST\">";
  49.  echo "Username: <input name=\"username\" size=\"15\"><br />";
  50.  echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
  51.  echo "<input type=\"submit\" value=\"Login\">";
  52.  echo "</form>";
  53.  }
  54. ?>


this is the screen i am getting when i acces the page


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\virtualservers\182763\root\test\login\lo\login. php:1) in D:\virtualservers\182763\root\test\login\lo\login. php on line 12

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\virtualservers\182763\root\test\login\lo\login. php:1) in D:\virtualservers\182763\root\test\login\lo\login. php on line 12


i would like some help please in locateing the sourtce of the errors

i know the errors are from the <?php in line one but therfe are NO spaces after this line only a return

i get the error when i enter a correct or a false user name

i know the database info i put in is correct as i can add data to the table fine

thanks
craigtomo's Avatar
craigtomo
Newbie
8 Posts
February 25th, 2008
06:03 PM
#2

Re: do not understand Cannot send session cookie - headers already sent
i was also wondering if it could be a setup fault on my server as i have been reciveing the same sort of errors useing differant code

all i am trying to do is create a database login

Reply
Markus's Avatar
Markus
Moderator
2,217 Posts
February 25th, 2008
06:13 PM
#3

Re: do not understand Cannot send session cookie - headers already sent
session_start() needs to be at the very top of your page! ALL THE TIME!
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. // rest of code
  5.  

Reply
craigtomo's Avatar
craigtomo
Newbie
8 Posts
February 25th, 2008
09:07 PM
#4

Re: do not understand Cannot send session cookie - headers already sent
i have put the session start in as you suggested and this is now the error i am reciveng


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\virtualservers\182763\root\test\login\lo\login. php:1) in D:\virtualservers\182763\root\test\login\lo\login. php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\virtualservers\182763\root\test\login\lo\login. php:1) in D:\virtualservers\182763\root\test\login\lo\login. php on line 2

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
February 25th, 2008
10:17 PM
#5

Re: do not understand Cannot send session cookie - headers already sent
Are you sure that there is no blank following the <?php. If so take it out.

Ronald

Reply
craigtomo's Avatar
craigtomo
Newbie
8 Posts
February 27th, 2008
10:34 PM
#6

Re: do not understand Cannot send session cookie - headers already sent
there is deffinetly no blanks after or before the <?php command

could it be somethingto do with the setup on my server as i have been getting similar problems with lots of different types of script i have been trying?

all i want is a simple login process

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
February 27th, 2008
10:43 PM
#7

Re: do not understand Cannot send session cookie - headers already sent
Something must have been outputted before the session_start() was executed. You sure that was all the code?

Ronald

Reply
craigtomo's Avatar
craigtomo
Newbie
8 Posts
February 28th, 2008
04:28 PM
#8

Re: do not understand Cannot send session cookie - headers already sent
Quote:
Something must have been outputted before the session_start() was executed. You sure that was all the code?

Ronald

there is no other code


that is why i think it might be on my server that is the problem

the file is located at http://equifor.soon2b.net/test/login/lo/login.php

please can you try and run it and see if you are getting the same errors i am

it is really frustrateing me

thanks

Reply
hsriat's Avatar
hsriat
Expert
1,471 Posts
February 29th, 2008
05:27 AM
#9

Re: do not understand Cannot send session cookie - headers already sent
Quote:
there is deffinetly no blanks after or before the <?php command

could it be somethingto do with the setup on my server as i have been getting similar problems with lots of different types of script i have been trying?

all i want is a simple login process


No line break too?

This error is only shown if you have sent something before the header is set.

Reply
craigtomo's Avatar
craigtomo
Newbie
8 Posts
February 29th, 2008
07:05 PM
#10

Re: do not understand Cannot send session cookie - headers already sent
there is nothing else in the file apart from what is shown

as i have said i have had similar problems with various files all with the same responce

Reply
cyberforester's Avatar
cyberforester
Newbie
1 Posts
March 29th, 2008
01:28 AM
#11

Re: do not understand Cannot send session cookie - headers already sent
Same thing is happening to me and it has me tearing my hair out.

I have downloaded and customised a script that allows users to enter a password in order to move to a part of the site I want protected from the general public.

I debugged the script and had it working on my development machine (fedora 7, apache and php 5.x). I thought i was doing great, and then I uploaded it to the production machine which is Windows 2k3, IIS 6 and Php 5.x. That's when the wheels came off and i start getting this error.

What is going on? I always seem to add extra to my development time by having to work around the Windows insanity.

Reply
hsriat's Avatar
hsriat
Expert
1,471 Posts
March 29th, 2008
06:09 AM
#12

Re: do not understand Cannot send session cookie - headers already sent
Quote:
Same thing is happening to me and it has me tearing my hair out.

I have downloaded and customised a script that allows users to enter a password in order to move to a part of the site I want protected from the general public.

I debugged the script and had it working on my development machine (fedora 7, apache and php 5.x). I thought i was doing great, and then I uploaded it to the production machine which is Windows 2k3, IIS 6 and Php 5.x. That's when the wheels came off and i start getting this error.

What is going on? I always seem to add extra to my development time by having to work around the Windows insanity.

If you are using Notepad, then open the file in another editor and remove the stuff before sending header.

Regards

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,891 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top PHP Forum Contributors