Solved

PHP and OPEN_SESSION problem

Posted on 2006-10-21
7
213 Views
Last Modified: 2012-05-05
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
Comment
Question by:qvfps
  • 4
  • 2
7 Comments
 

Author Comment

by:qvfps
Comment Utility
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
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
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
 

Author Comment

by:qvfps
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:qvfps
Comment Utility
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
 
LVL 2

Accepted Solution

by:
nowaydown1 earned 250 total points
Comment Utility
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
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
I agree - you can not have any output (even a space) before you choose to use session_start unless you use ob_start()
0
 

Author Comment

by:qvfps
Comment Utility
Thaks.  OB_START(); took care of the problem.  Thanks for the assistance.  
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now