Connecting Tech Pros Worldwide Forums | Help | Site Map

php session error

Newbie
 
Join Date: Nov 2007
Posts: 23
#1: Nov 21 '07
i am facing problem in php session..
my code is giving error..
i am giving my code and error message below..


my code:-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['submit']))
  3. {   if(empty($password)){echo "No password specified";}
  4.  
  5.     $connection=mysql_connect("localhost","wwwuser_raja","kalika");
  6.       if(!$connection)
  7.         {echo"<b>connection not set...</b><br>";}
  8.  
  9.     $sel=mysql_select_db("wwwuser_raja");
  10.       if(!$sel)
  11.         {echo"<b>database not selected...</b><br>";}
  12.      $query="select password from users where username='".$username."'";
  13.      $result=mysql_query($query);
  14.      if($row=mysql_fetch_array($result))
  15.      {
  16.          if(!(md5($password)==$row["password"]))
  17.            {echo "Wrong Password !";} 
  18.  
  19.  
  20.      }
  21.      else
  22.      {echo "<b>User does not exists  !!</b>";}    
  23.      session_start();
  24.      session_register("username");
  25.      session_encode();
  26.      $url="Location:loggedin.php?PHPSESSID=".$PHPSESSID;
  27.      header($url); 
  28.  
  29.  }    
  30. ?>
  31.  
  32.  
  33. <html xmlns="http://www.w3.org/1999/xhtml">
  34.  
  35. <head>
  36. <meta http-equiv="Content-Language" content="en-us" />
  37. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  38. <title>Login</title>
  39. <style type="text/css">
  40. .style1 {
  41.     margin-left: 0px;
  42. }
  43. .style2 {
  44.     margin-left: 248px;
  45.     margin-top: 126px;
  46. }
  47. </style>
  48. </head>
  49.  
  50. <body>
  51.  
  52. <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" style="height: 202px; width: 321px" class="style2">
  53. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter&nbsp; your information&nbsp; in 
  54.     the form&nbsp; below&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  55. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
  56. &nbsp;<br />
  57. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  58. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  59. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  60. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  61.     <br />
  62. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Username
  63.     <input name="username" type="text" maxlength="20" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>" />&nbsp;&nbsp;&nbsp;&nbsp; <br />
  64. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
  65. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Password
  66.     <input name="password" type="password" maxlength="20" />&nbsp; <br />
  67.     <br />
  68. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  69. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  70. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  71. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  72. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  73. &nbsp;&nbsp;&nbsp;&nbsp;
  74.     <input name="submit" type="submit" value="Login" style="width: 40px; height: 20px" class="style1" /><br />
  75. </form>
  76. </body>
  77.  
  78. </html>
  79.  
Error msg:-when i am giving the correct username and password..

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 29

can anybody help me...
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#2: Nov 21 '07

re: php session error


Hi mishrarajesh44,
Welcome to The Scripts. You mistakenly placed your question in the PHP Articles, so I moved it to the PHP Forum where I am sure you will receive a better response.
Motoma
brettl's Avatar
Member
 
Join Date: Sep 2007
Posts: 38
#3: Nov 21 '07

re: php session error


Quote:
Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.
just add session_start() to the very first line of your page before anything is outputted to the browers.

php.net session_start - Manual
Member
 
Join Date: Sep 2007
Posts: 34
#4: Nov 21 '07

re: php session error


remove the lines with these code:

Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. session_register("username");
  3. session_encode();
  4. $url="Location:loggedin.php?PHPSESSID=".$PHPSESSID;
  5. header($url);
so you can see if your logic is actually working. maybe the code is echoing something even when you give the correct username and password.

That error message is caused if any output is given (whitespace not within the <php tags included) before the call to session_start() or header() function.

I once had this problem with the fckeditor code and couldn't find any extra whitespace in the entirety of the fckeditor library, and i have never found out what the problem was.

EDIT:
Quote:
Quote:
Originally Posted by
Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.


just add session_start() to the very first line of your page before anything is outputted to the browers.
Brettl i think that might have been the problem that i encountered.
Newbie
 
Join Date: Nov 2007
Posts: 23
#5: Nov 21 '07

re: php session error


i am verified my code by putting session_start() at the beginning , still getting error..


mycode:-

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  session_start();
  3.      //session_register("username");
  4.      //session_encode();
  5.     // $url="Location:loggedin.php?PHPSESSID=".$PHPSESSID;
  6.     // header($url);
  7.  if(isset($_POST['submit']))
  8. {   if(empty($password)){echo "No password specified";}
  9.  
  10.     $connection=mysql_connect("localhost","wwwuser_raja","kalika");
  11.       if(!$connection)
  12.         {echo"<b>connection not set...</b><br>";}
  13.  
  14.     $sel=mysql_select_db("wwwuser_raja");
  15.       if(!$sel)
  16.         {echo"<b>database not selected...</b><br>";}
  17.      $query="select password from users where username='".$username."'";
  18.      $result=mysql_query($query);
  19.      if($row=mysql_fetch_array($result))
  20.      {
  21.          if(!(md5($password)==$row["password"]))
  22.            {echo "Wrong Password !";} 
  23.  
  24.  
  25.      }
  26.      else
  27.      {echo "<b>User does not exists  !!</b>";}    
  28.     // session_start();
  29.     // session_register("username");
  30.     // session_encode();
  31.     // $url="Location:loggedin.php?PHPSESSID=".$PHPSESSID;
  32.     // header($url); 
  33.  
  34.  }    
  35.  
  36. ?>
  37.  
error:-
Expand|Select|Wrap|Line Numbers
  1. Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 2
  2.  
the line 2 is ..session_start();


i am not getting ..the username and password r correct, i have checked it by removing sesssion parts..
anyone can find the bogs..
Member
 
Join Date: Sep 2007
Posts: 34
#6: Nov 21 '07

re: php session error


why are you posting on a new thread??

i would recommend editing that file with a test editor that: strips all whitespace from the start and end of the file. i dont know any free software that does that. Zend studio does it.
Newbie
 
Join Date: Nov 2007
Posts: 23
#7: Nov 21 '07

re: php session error


Quote:

Originally Posted by andho

why are you posting on a new thread??

i would recommend editing that file with a test editor that: strips all whitespace from the start and end of the file. i dont know any free software that does that. Zend studio does it.


ok,sorry andho..

i am giving again my previous codes..
Expand|Select|Wrap|Line Numbers
  1.    1.
  2.       <?php
  3.    2.
  4.       if(isset($_POST['submit']))
  5.    3.
  6.       {   if(empty($password)){echo "No password specified";}
  7.    4.
  8.  
  9.    5.
  10.           $connection=mysql_connect("localhost","wwwuser_raja","kalika");
  11.    6.
  12.             if(!$connection)
  13.    7.
  14.               {echo"<b>connection not set...</b><br>";}
  15.    8.
  16.  
  17.    9.
  18.           $sel=mysql_select_db("wwwuser_raja");
  19.   10.
  20.             if(!$sel)
  21.   11.
  22.               {echo"<b>database not selected...</b><br>";}
  23.   12.
  24.            $query="select password from users where username='".$username."'";
  25.   13.
  26.            $result=mysql_query($query);
  27.   14.
  28.            if($row=mysql_fetch_array($result))
  29.   15.
  30.            {
  31.   16.
  32.                if(!(md5($password)==$row["password"]))
  33.   17.
  34.                  {echo "Wrong Password !";}
  35.   18.
  36.  
  37.   19.
  38.  
  39.   20.
  40.            }
  41.   21.
  42.            else
  43.   22.
  44.            {echo "<b>User does not exists  !!</b>";}   
  45.   23.
  46.            session_start();
  47.   24.
  48.            session_register("username");
  49.   25.
  50.            session_encode();
  51.   26.
  52.            $url="Location:loggedin.php?PHPSESSID=".$PHPSESSID;
  53.   27.
  54.            header($url);
  55.   28.
  56.  
  57.   29.
  58.        }   
  59.   30.
  60.       ?>
  61.   31.
  62.  
  63.   32.
  64.  
  65.   33.
  66.       <html xmlns="http://www.w3.org/1999/xhtml">
  67.   34.
  68.  
  69.   35.
  70.       <head>
  71.   36.
  72.       <meta http-equiv="Content-Language" content="en-us" />
  73.   37.
  74.       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  75.   38.
  76.       <title>Login</title>
  77.   39.
  78.       <style type="text/css">
  79.   40.
  80.       .style1 {
  81.   41.
  82.           margin-left: 0px;
  83.   42.
  84.       }
  85.   43.
  86.       .style2 {
  87.   44.
  88.           margin-left: 248px;
  89.   45.
  90.           margin-top: 126px;
  91.   46.
  92.       }
  93.   47.
  94.       </style>
  95.   48.
  96.       </head>
  97.   49.
  98.  
  99.   50.
  100.       <body>
  101.   51.
  102.  
  103.   52.
  104.       <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" style="height: 202px; width: 321px" class="style2">
  105.   53.
  106.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter&nbsp; your information&nbsp; in
  107.   54.
  108.           the form&nbsp; below&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  109. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
  110.   55.
  111.       &nbsp;<br />
  112.   56.
  113.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  114. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  115. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  116. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  117.   57.
  118.           <br />
  119.   58.
  120.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Username
  121.   59.
  122.           <input name="username" type="text" maxlength="20" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>" />&nbsp;&nbsp;&nbsp;&nbsp; <br />
  123.   60.
  124.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
  125.   61.
  126.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Password
  127.   62.
  128.           <input name="password" type="password" maxlength="20" />&nbsp; <br />
  129.   63.
  130.           <br />
  131.   64.
  132.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  133. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  134. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  135. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  136. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  137. &nbsp;&nbsp;&nbsp;&nbsp;
  138.   65.
  139.           <input name="submit" type="submit" value="Login" style="width: 40px; height: 20px" class="style1" /><br />
  140.   66.
  141.       </form>
  142.   67.
  143.       </body>
  144.   68.
  145.  
  146.   69.
  147.       </html>
  148.  

Error msg:-when i am giving the correct username and password..
Expand|Select|Wrap|Line Numbers
  1. Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 29
  2.  
waiting for your rply..
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#8: Nov 21 '07

re: php session error


It would be better if you use PHP code tags around your code. This will enhance your code's readability. These tags are available when you post the comment. e.g.
[PHP]<?php
echo "It is a good idea to use PHP tags to format PHP code";
?>[/PHP]
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#9: Nov 21 '07

re: php session error


Hi mishrarajesh44.

Please do not double post your questions, or create new threads to respond to a question in another thread. I have merged the two threads you created to avoid any confusion.

Also, as mwasif already pointed out, please use [code] tags when posting your code examples. (See How to ask a question)

[code=php]PHP code goes here [/code]

Thank you.
Newbie
 
Join Date: Nov 2007
Posts: 23
#10: Nov 22 '07

re: php session error


Quote:

Originally Posted by andho

why are you posting on a new thread??

i would recommend editing that file with a test editor that: strips all whitespace from the start and end of the file. i dont know any free software that does that. Zend studio does it.

hiii ..

i was just checking to find out error in code,

even the only session_start( ) gives error...

code:-

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. ?>
  4.  
ERROR MESSAGE:-

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php:1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php:1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php on line 2

it may help u to find out my problem..
waiting for reply..
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#11: Nov 22 '07

re: php session error


Is there a new-line character, a space, or anything else before you open the <?php tags?

The session_start() function needs to be called before anything is echoed, or added in any other way to the response. Once you start adding content to the response, the headers are locked and the session cookie can not be added, thus causing the session to fail.

If non of that is the problem, is it possible that your HTTP server is somehow set up to automatically include something on all pages, and that this page could be adding to the response?
Newbie
 
Join Date: Nov 2007
Posts: 23
#12: Nov 22 '07

re: php session error


Quote:

Originally Posted by Atli

Is there a new-line character, a space, or anything else before you open the <?php tags?

The session_start() function needs to be called before anything is echoed, or added in any other way to the response. Once you start adding content to the response, the headers are locked and the session cookie can not be added, thus causing the session to fail.

If non of that is the problem, is it possible that your HTTP server is somehow set up to automatically include something on all pages, and that this page could be adding to the response?


hii.

i got the problem...
it was the text editer problem..
previously i was doing in "microsoft expression web",
now the same code does not show error when typed in "edit plus"..
thanks all for your support..
Reply