Jump to content

Archived

This topic is now archived and is closed to further replies.

rtp

Warning: Cannot modify header information - headers already sent by

Recommended Posts

I've done everything within my grasp (I'm pretty new to php).  I'm trying to create a cookie using information from within a form.  Here's my code and the complete error message:

[b]Error Message:[/b]

Notice: Undefined index: name in C:\Program Files\Apache Group\Apache2\htdocs\Testing Site\test_login.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Testing Site\test_login.php:4) in C:\Program Files\Apache Group\Apache2\htdocs\Testing Site\test_login.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\Testing Site\test_login.php:4) in C:\Program Files\Apache Group\Apache2\htdocs\Testing Site\test_login.php on line 10

[b]Code:[/b] (line by line)
[code]
<?php
setcookie( 'auth', 'ok' );
setcookie( 'userGroup', 'user' );
setcookie( 'name', $_POST['name'] );
?>
-----inserted by Dreamweaver for login (doesn't count as line)-----
<?php require_once('Connections/conn_test.php'); ?>
<?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['name'])) {
  $loginUsername=$_POST['name'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "group";
  $MM_redirectLoginSuccess = "test_success.php";
  $MM_redirectLoginFailed = "test_login.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_conn_test, $conn_test);
 
  $LoginRS__query=sprintf( "SELECT `username` , `email` , `group` FROM `users` WHERE username = '%s' AND password = '%s'" ,
  get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
 
  $LoginRS = mysql_query($LoginRS__query, $conn_test) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
   
    $loginStrGroup  = mysql_result($LoginRS,0,'group');
   
    //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 );
  }
}
?>


<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>
</head>
<body>
<center>
<form ACTION="<?php echo $loginFormAction; ?>" method="POST" name="login">
<table border="1" cellpadding="3" cellspacing="0" bordercolor="#000000" bgcolor="#E0E0E0">
  <tr>
    <td>User Name</td>
    <td><input name="name" type="text" size="30" maxlength="255" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input name="password" type="password" size="30" maxlength="255" /></td>
  </tr>
  <tr>
    <td><input name="userGroup" type="hidden" value="user" /></td>
    <td align="right"><input name="login" type="submit" value="Log-in" /></td>
  </tr>
</table>
</form>
</center>
</body>
</html>
[/code]

Share this post


Link to post
Share on other sites
Avoid the warning by setting error_reporting level lower.

http://ca.php.net/manual/en/function.error-reporting.php

That should avoid the output to the browser that's causing the header error.

Share this post


Link to post
Share on other sites
Chnage this:
[code]setcookie( 'auth', 'ok' );
setcookie( 'userGroup', 'user' );
setcookie( 'name', $_POST['name'] );[/code]
to:
[code]if(isset($_POST['name']))
{
    setcookie( 'auth', 'ok' );
    setcookie( 'userGroup', 'user' );
    setcookie( 'name', $_POST['name'] );
}[/code]
Always check whether a POST var exists first before using it. Then you dont get the underfined index notice message.

Also Why are you setting a cookie and a session with the same information? You might want to move the code above down to the bit where the user has successfully logged in. Otherwise if the user wasnt successfully logged in the unauthorised user is going get a cookie set.

Share this post


Link to post
Share on other sites
okay. thanks. i did try the error thing, but that only got rid of the session error.

Share this post


Link to post
Share on other sites

×

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.