Jump to content

cannot send session cookie, headers already sent by.....


devvie

Recommended Posts

Im getting this message when i load my login page.  its brand new, so i hjave a million directions i can go.  I want a secure login with sessions so that i can remove the logins on other outside apps on the site.  [code]Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/c4sale/public_html/main/Connections/connection.php:12) in /home/c4sale/public_html/main/login.php on line 36

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/c4sale/public_html/main/Connections/connection.php:12) in /home/c4sale/public_html/main/login.php on line 36[/code]

heres the header of my page
[code]<?php require_once('Connections/connection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>

<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['textfield'])) {
  $loginUsername=$_POST['textfield'];
  $password=$_POST['textfield2'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "user/index.php";
  $MM_redirectLoginFailed = "login_deny.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_forsale, $forsale);
 
  $LoginRS__query=sprintf("SELECT Desired_Username, Password FROM login1_table WHERE Desired_Username=%s AND Password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
 
  $LoginRS = mysql_query($LoginRS__query, $forsale) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    $loginStrGroup = "";
   
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;      

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>[/code]
and heres the form
[code]<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
    <label>Username<br><input name="textfield" type="text" accesskey="l" tabindex="1" size="15"><br></label><label>Password<br><input name="textfield2" type="password" tabindex="2" size="15"><br></label><input type="submit" name="Submit" value="Submit" tabindex="3"></form>[/code]
Link to comment
Share on other sites

thats where the bugger comes in, there is no line 12, heres teh complete lines, thats it.  10 lines

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_forsale = "localhost";
$database_forsale = "db";
$username_forsale = "user";
$password_forsale = "pass";

?>
Link to comment
Share on other sites

you know i read soemthing about a cache limiter recently, and i never activated it(if necessary), or payed any attention it, and i notice that in the error message. could this be it?

BUT, it also says session headers already started, so might i need to do a session destroy or unset first?
Link to comment
Share on other sites

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
   
    header("Location: " . $MM_redirectLoginSuccess );
  }
}
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
Link to comment
Share on other sites

Ken, i wouldnt have understood it completely anyway, but now looking back on it, thanks for the link, becuase it starting to make more sense. I really dont understand  what headers are yet.  I read chapters of php everyday, so its coming more clear, but headers are still mystery for the most part.

redarrow, whats teh code you sent me, it looks like its a direct copy from my page.  can you give a comment with your post?

if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
Link to comment
Share on other sites

You did this code in dreamweaver?  I had the same problem and the code looks identical to the code that dreamweaver produces.  I played around with it for several hours before I got around it.  Wish I was sweet enough to tell you what I did to fix it.  Chances are though, if I were to do it again, I would run into the same problem!  Let me know how it works out so I know in the future.  Good luck!
Link to comment
Share on other sites

Akr, i use dreamweaver but i got this code from hotscripts i believe

about that businessman, you notice its NOT the first line of code, its like the 30th.....  look at my php code ont he top of this post.  would you consider the session start to be the first line of code in that example?  if so, then i am really confused.  Or, is that the problem, where the session start shoudl be on LINE1???
Link to comment
Share on other sites

No matter what if session_start();
is not LINE 1, ONLY, no other line, no spaces, no text, nothing at all before session_start()
then
error headers already sent, always pop's up at the top of the screen
always cop y and paste session_start() above all pages using sessions.
Preferably in it's own
<?php
session_start();
?> exactly like that before html or anything
if you have any includes, it's best to do them in another island, below the session start code island
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.