Jump to content

[SOLVED] error when dealing with session


sungpeng

Recommended Posts

hi check I keep having this error when dealing with session

 

Warning session_start()[function.session-start]: Cannot send session cookie - headers already sent by (output started at /home2/example/public_html/house/test2.php.

 

Can anyone please help explain?

Link to comment
Share on other sites

 

accesscontrol.php

<?php 
include ("".$_SERVER['DOCUMENT_ROOT']."/housing/includes/config.php");

session_start();

if (isset($_POST['uid'])) {
$uid = $_POST['uid'];
} else {
$uid = $_SESSION['uid'];
}
if (isset($_POST['pwd'])) {
$pwd = md5($_POST['pwd']);
} else {
$pwd = $_SESSION['pwd'];
}


if(!isset($uid) || !isset($pwd) )
{
  ?>
  <html>
  <head>
  <title> Please Log In for Access </title>
  </head>
<body>
  <table align=center width=300 border=0 cellspacing=0 cellpadding=0 bgcolor="#2f4f4f">
  <tr><td>
   <table border=0 width=100% cellspacing=1 cellpadding=1>
    <form action="<?=$_SERVER['PHP_SELF']?>" method=POST>
    <tr><td BGCOLOR="#2f4f4f"><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif" COLOR="#FFFFFF">
    <B>Please Log In For Access:</B>
    </td></tr>
    <tr><td BGCOLOR="#c7c7c7"><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif">
You must log in to access this area of the site.
     </td></tr>
    <tr>
     <td BGCOLOR="#fffff0">
      <table width=100% border=0 cellspacing=0 cellpadding=0>
    <tr>
     <td><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif">Email Address:</td>
     <td><input type=text name="uid" size="20" value=""></td>
    </tr>
        <tr>
     <td><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif">Password:</td>
     <td><input type=password name="pwd" size="20"></td>
    </tr>
    <tr>
     <td colspan=2 align=center>
      <input type=submit name="Login" value="Login">
     </td>
    </tr>
    </form>
      </table>
     </td>
    </tr>
   </table>
  </td></tr>
</table>
  </body>
  </html>
  <?php
  exit;
}
//Clean the input submitted to mysql
$uid=addslashes($uid);
$pwd=addslashes($pwd);

//this puts the variable into the session

$_SESSION['uid'] = $uid; 
$_SESSION['pwd'] = $pwd;

$sql = "SELECT * FROM users WHERE email = '$uid' AND passwd = '$pwd' ";

$result = mysql_query($sql);

if (!$result) {
echo "A database error occurred while checking your login details";
}
//if bad user/pass combo access denied
if (mysql_num_rows($result) == 0) {

  unset($_SESSION['uid']);
  unset($_SESSION['pwd']);
  ?>
  <html>
  <head>
  <title> Access Denied </title>
  </head>
  <body>
  <h1> Access Denied </h1>
  <p>There are several reasons this may be happening:<BR>
  <UL><LI>Your username or password is incorrect</LI>
  <LI>You have forgotten your login information. <a href="password_reset.php">Lost Password</a></LI></UL>
  To return to our login page, <a href="index.php">click here</a>.</p>
  </body>
  </html>
    <?php
  exit;
}

?>

 

 

 

test2.php




<?php 

if($_POST[action]=="Update")
{

echo "hello";

    
}

?>

<html>

<title>Upload an image to a database</title>


<body>


<h2>Update with new information</h2>
<form name="form" enctype="multipart/form-data" method="post" action="<?php echo"$PHP_SELF?rid=$_GET[rid]"; ?>">
<?php 
include ("".$_SERVER['DOCUMENT_ROOT']."/housing/accesscontrol.php");
?>
<input type=text name='images' class='bginput'>


<input type="submit" name="action" value="Update">
</form>

</body>
</html>

Link to comment
Share on other sites

The session_start() MUST be called before any output is sent to the browser. In your case make sure there is no white space (or blank lines) before the first "<?php" in test2.php and then put the session_start() right after the "<?php". Remove it from accesscontrol.php

 

Ken

Link to comment
Share on other sites

housing accesscontrol.php

<?php 
include ("".$_SERVER['DOCUMENT_ROOT']."/housing/includes/config.php");

session_start();

if (isset($_POST['uid'])) {
$uid = $_POST['uid'];
} else {
$uid = $_SESSION['uid'];
}
if (isset($_POST['pwd'])) {
$pwd = md5($_POST['pwd']);
} else {
$pwd = $_SESSION['pwd'];
}


if(!isset($uid) || !isset($pwd) )
{
  ?>
  <html>
  <head>
  <title> Please Log In for Access </title>
  </head>
<body>
  <table align=center width=300 border=0 cellspacing=0 cellpadding=0 bgcolor="#2f4f4f">
  <tr><td>
   <table border=0 width=100% cellspacing=1 cellpadding=1>
    <form action="<?=$_SERVER['PHP_SELF']?>" method=POST>
    <tr><td BGCOLOR="#2f4f4f"><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif" COLOR="#FFFFFF">
    <B>Please Log In For Access:</B>
    </td></tr>
    <tr><td BGCOLOR="#c7c7c7"><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif">
You must log in to access this area of the site.
     </td></tr>
    <tr>
     <td BGCOLOR="#fffff0">
      <table width=100% border=0 cellspacing=0 cellpadding=0>
    <tr>
     <td><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif">Email Address:</td>
     <td><input type=text name="uid" size="20" value=""></td>
    </tr>
        <tr>
     <td><FONT SIZE="-1" FACE="Verdana,Tahoma,Arial,Helvetica,sans-serif">Password:</td>
     <td><input type=password name="pwd" size="20"></td>
    </tr>
    <tr>
     <td colspan=2 align=center>
      <input type=submit name="Login" value="Login">
     </td>
    </tr>
    </form>
      </table>
     </td>
    </tr>
   </table>
  </td></tr>
</table>
  </body>
  </html>
  <?php
  exit;
}
//Clean the input submitted to mysql
$uid=addslashes($uid);
$pwd=addslashes($pwd);

//this puts the variable into the session

$_SESSION['uid'] = $uid; 
$_SESSION['pwd'] = $pwd;

$sql = "SELECT * FROM users WHERE email = '$uid' AND passwd = '$pwd' ";

$result = mysql_query($sql);

if (!$result) {
echo "A database error occurred while checking your login details";
}
//if bad user/pass combo access denied
if (mysql_num_rows($result) == 0) {

  unset($_SESSION['uid']);
  unset($_SESSION['pwd']);
  ?>
  <html>
  <head>
  <title> Access Denied </title>
  </head>
  <body>
  <h1> Access Denied </h1>
  <p>There are several reasons this may be happening:<BR>
  <UL><LI>Your username or password is incorrect</LI>
  <LI>You have forgotten your login information. <a href="password_reset.php">Lost Password</a></LI></UL>
  To return to our login page, <a href="index.php">click here</a>.</p>
  </body>
  </html>
    <?php
  exit;
}

?>

Link to comment
Share on other sites

You can't because you already  have "output". Once any html, plain text, or even white-space/line-breaks has been output, you cannot declare session_start(). I'd suggest removing it from accesscontrol.php and simply placing it at the top of test2.php . That should alleviate the problem, but remember that all pages that are using accesscontrol.php will need session_start() declared.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.