Skip to main content
Participant
September 2, 2009
Answered

User Authentification - cannot send session cookie header... etc.

  • September 2, 2009
  • 1 reply
  • 1341 views

I am quite new at creating dynamic sites especially using php.  After following many tutorials however, I have been able to connect to a database using Xampp and phpMyadmin with everthing works as it should.  I have now created a template and can still access the database, display tabled information in various way without a problem until I try to Log in User.  As soon as I do, I get the following error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent in C:\Program Files\xampp\htdocs\FM.........ry\TestLogin.php on line 0

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\FM..........ry\TestLogin.php:0) in C:\Program Files\xampp\htdocs\FMRPNewTry\TestLogin.php on line 0

I have no idea what to do. The procedure of seeting up a login page seems very straight forward and I actually had it working while following a tutorial.  I can't think of anything I've done differently in my own page/site other than using a template which is pretty basic.

Can anyone help?

This topic has been closed for replies.
Correct answer David_Powers

Thanks for your help but one thing I am curious about, I did not connect to the data base differently between the two sites so why did one have the code "require once" and the other has "virtual".


The difference is caused by selecting the option to use links relative to the site root in the site definition. With a PHP site, you should always use links relative to the document. Alternatively, you need to amend all instances of virtual(), which is an Apache-exclusive function, and which also causes a lot of problems, such as the one you stumbled across.

1 reply

David_Powers
Inspiring
September 2, 2009

You have encountered the "Headers already sent" error that confuses the hell out of most PHP novices. You'll find an explanation of the problem and its cure in the following tutorial: http://kb2.adobe.com/community/publishing/505/cpsid_50572.html. It deals principally with page redirects, but the cause and cure are the same.

Participant
September 2, 2009

Thank you very much for your reply.  I'm still muddling my way through the tutorial to see if my code or "white space" is the culprit but honestly, am for now, just not sure.

Prior to receiving your post, I took a look at the page I knew was working and found this difference"

The page that created the error had this code

<?php virtual('/FMRPNewTry/Connections/connCosmo.php'); ?>

<?php

and the page that worked had this:

<?php require_once('Connections/connCosmo.php'); ?>
<?php

When I replaced the code that worked to the page that wasn't, since it was the same database, it worked no problem.  I don't know what the long term effects will be however and if this is a bad thing.

Here is the entire code of the page that wouldn't work.

<?php virtual('/FMRPNewTry/Connections/connCosmo.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $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['U'])) {
  $loginUsername=$_POST['U'];
  $password=$_POST['P'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "/FMRPNewTry/index1.php";
  $MM_redirectLoginFailed = "/FMRPNewTry/Login.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_connCosmo, $connCosmo);
 
  $LoginRS__query=sprintf("SELECT userEmail, password FROM users WHERE userEmail=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  
  $LoginRS = mysql_query($LoginRS__query, $connCosmo) 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']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];   
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3.........................

Thanks for helping.

David_Powers
Inspiring
September 2, 2009

Replace virtual() with require_once(). Also change the path inside the parentheses of require_once() to a link relative to the document.