?
Solved

PHP and OPEN_SESSION problem

Posted on 2006-10-21
7
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:qvfps
ID: 17782332
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
ID: 17782341
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
ID: 17782353
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:qvfps
ID: 17782388
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 1000 total points
ID: 17782592
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
ID: 17782616
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
ID: 17784742
Thaks.  OB_START(); took care of the problem.  Thanks for the assistance.  
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

770 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