• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

PHP and OPEN_SESSION problem

I am learning PHP and I have create the following login page.  I have two different Linux servers I wanted to play with this on.   On the first one it works just fine.  It validates my login and passes the information to reqs.php.  On the second server I get the following error message when I submit the following page.

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /srv/www/htdocs/login2.php:7) in /srv/www/htdocs/login2.php on line 87

I looked up this message and it indicated that SESSION_START() was in the wrong location so I moved it to the top of the page.  When I do that I get the following message.  

rning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/login2.php:9) in /srv/www/htdocs/login2.php on line 92

I would appreciate any suggestions as to what I have done wrong or how to better code the page.      Thanks.


<?php
SESSION_START();    (NEW LOCATION)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#Layer1 {
      position:absolute;
      width:744px;
      height:100px;
      z-index:1;
      background-color: #0033FF;
}
#Layer2 {
      position:absolute;
      width:524px;
      height:358px;
      z-index:2;
      left: 11px;
      top: 175px;
      background-color: #66FFFF;
}
#Layer3 {
      position:absolute;
      width:218px;
      height:359px;
      z-index:3;
      left: 525px;
      top: 0px;
      background-color: #66FF00;
}
#Layer4 {
      position:absolute;
      width:743px;
      height:141px;
      z-index:3;
      left: 11px;
      top: 400px;
      background-color: #CCCC66;
}
-->
</style>
</head>

<body>

<div id="Layer1">
  <p> &nbsp;&nbsp;&nbsp;<u>Login<br />
  </u>login here </p>
<?php


$u2=$_POST['User'];
$p2=$_POST['Pwd'];

print "<br> user = $u2";
print "<br> password = $p2";

if ($u2 != "" and $p2 !="")
      {
      print "<br>Empty using dbl quotes <br>";

      $linkID = mysql_connect("localhost","DB1","PWD01");
      if ($linkID != FALSE)
            {
            print "The connection to the server was successful.<br>";
            }
      else
            {
            print "The connection to the server failed<br>";
            }

      mysql_select_db("DB1",$linkID);
      print "<br> select * from user where uname=\"$u2\" and pwd=\"$p2\";<br>";
      $resultID = mysql_query("select * from user where uname=\"$u2\" and pwd=\"$p2\";",$linkID);
      print "reslultID = $resultID<br>";
      $num= mysql_numrows($resultID);
      if ($num == 0)
            {
            print "<br>Login failed<br>";
            }
      if ($num > 0)
            {
            SESSION_START();    (ORIGINAL LOCATION)
            list($myID,$myuname,$myfname,$mylname,$myadmin,$mypwd) = mysql_fetch_row($resultID);
            $_SESSION['LI']="Y";
            $_SESSION['UID']= $myID;
            $_SESSION['admin'] = $myadmin;
            header('Location:reqs.php');
            }

      mysql_close($linkID);
      }


?>

</div>
<div id="Layer4">
  <form id="form1" name="form1" method="post" action="login2.php">
   
    <p>
      <label for="textfield">User</label>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="text" name="User" id="User" />
      <br />
      <label for="label">Password</label>
      <input type="password" name="Pwd" id="Pwd" />
    </p>
    <p>
      <label for="Submit"></label>
      <label for="Submit"></label>
      <input type="submit" name="Submit" value="Submit" id="Submit" />
      <label for="Submit"></label>
    </p>
  </form>
</div>
</body>
</html>
0
qvfps
Asked:
qvfps
  • 4
  • 2
1 Solution
 
qvfpsAuthor Commented:
I am running this on SUSE Linux 10.1 sever.  One of the servers is the 64 bit version and the one I am having the problwm with is the 32 bit version.  

The 64 bit version is running the version of Apache distribued with SUSE and the 32 bit is running the latest versions of both.

Thanks for the assistance.
0
 
Raynard7Commented:
Hi,

It seems that you have the session trying to start twice in the page,

after if ($num > 0) and at the start

If you read the error message it does not have a problem with the session start at the beginning - but the one that is resident at line 91

Basically you can only start the session once or you will get into problems. So comment out the second and see if it works
0
 
qvfpsAuthor Commented:
Thanks for the quick response.  However i only have SESSION_START on the once.  I labeled one new and one original to show the two locations I have tried it.  I get the first error messge when I try and start the session on line 91.  (THis works on the other server) and I get the second error message when I move it to the top of the page.   It is only on the page once in the version I am testing.  
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
qvfpsAuthor Commented:
If I place the following code at the top of the page I transfer to the reqs page and pass the assigned values.

<?
          SESSION_START();  
          $_SESSION['LI']="Y";
          $_SESSION['UID']= 10;
          $_SESSION['admin'] = y;
          header('Location:reqs.php');
?>

How do I validate the login then pass the information as a session variable?  
0
 
nowaydown1Commented:
The reason you recieve an error message is that you're outputting HTML content before calling the header() function.   The header function sends an HTTP header.  All HTTP headers must be transmitted before your HTML, which is why you recieve this error.  It probably works on your one servers because you have output buffering enabled by default on one of them in your php.ini.  

I imagine the reason you're using the header function right in the middle of your HTML content is to retain your branding for error messages.  In this case, you should use the output buffering functions to have our headers/content come out in the right order.

Take a look at the PHP manual for ob_start().  
http://us2.php.net/ob_start

Hope this helps!  Have a good one!




0
 
Raynard7Commented:
I agree - you can not have any output (even a space) before you choose to use session_start unless you use ob_start()
0
 
qvfpsAuthor Commented:
Thaks.  OB_START(); took care of the problem.  Thanks for the assistance.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now