Jump to content


Photo

Warning: Cannot modify header information - headers already sent by


  • Please log in to reply
3 replies to this topic

#1 rtp

rtp
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 August 2006 - 11:29 PM

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:

Error Message:

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

Code: (line by line)
<?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>


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 20 August 2006 - 11:34 PM

Avoid the warning by setting error_reporting level lower.

http://ca.php.net/ma...r-reporting.php

That should avoid the output to the browser that's causing the header error.
Legend has it that reading the manual never killed anyone.
My site

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 August 2006 - 09:20 AM

Chnage this:
setcookie( 'auth', 'ok' );
	setcookie( 'userGroup', 'user' );
	setcookie( 'name', $_POST['name'] );
to:
if(isset($_POST['name']))
{
    setcookie( 'auth', 'ok' );
    setcookie( 'userGroup', 'user' );
    setcookie( 'name', $_POST['name'] );
}
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.

#4 rtp

rtp
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 August 2006 - 10:59 AM

okay. thanks. i did try the error thing, but that only got rid of the session error.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users