rtp Posted August 20, 2006 Share Posted August 20, 2006 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 4Warning: 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 4Warning: 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] Quote Link to comment https://forums.phpfreaks.com/topic/18143-warning-cannot-modify-header-information-headers-already-sent-by/ Share on other sites More sharing options...
AndyB Posted August 20, 2006 Share Posted August 20, 2006 Avoid the warning by setting error_reporting level lower. http://ca.php.net/manual/en/function.error-reporting.phpThat should avoid the output to the browser that's causing the header error. Quote Link to comment https://forums.phpfreaks.com/topic/18143-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-77784 Share on other sites More sharing options...
wildteen88 Posted August 21, 2006 Share Posted August 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/18143-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-77955 Share on other sites More sharing options...
rtp Posted August 21, 2006 Author Share Posted August 21, 2006 okay. thanks. i did try the error thing, but that only got rid of the session error. Quote Link to comment https://forums.phpfreaks.com/topic/18143-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-77978 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.