jacer17 Posted June 17, 2006 Share Posted June 17, 2006 The orginal script[code]<? $uname = $_POST['uname']; $password = $_POST['password']; if( (!$uname) or (!$password) ) { header("Location:login.php"); exit(); } $conn = mysql_connect('localhost', 'username', 'password') or die('wrong connection info'); mysql_select_db('fcp_fcodes') or die('No database named that'); $sql="select * from fcdatabase where uname='$uname' and password = password('$password')"; $rs = mysql_query($sql) or die("could not execute query"); $row = mysql_fetch_object($rs); $id = $row->id; $firstname = $row->fname; $num = mysql_numrows($rs); if($num !=0 ) { setcookie("id", $id, time()+36000); setcookie("firstname", $firstname, time()+36000); header( "Location:settings.php?id=$id"); exit(); } else { header( "Location:login.php" ); exit(); }?><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title></head><body><form action="auth.php" method="post"><table><tr><td><b>Username:</b></td><td><input type="text" name="uname" size="25" /></td></tr><tr><td><b>Password:</b></td><td><input type="password" name="password" size="25" /></td></tr><tr><td><input type="submit" name="submit" value="Log In" /></td></tr></table></form></body></html>[/code]The error[code]Warning: Cannot modify header information - headers already sent by (output started at /home/fcp/public_html/friendcode/login.php:2) in /home/fcp/public_html/friendcode/login.php on line 8[/code]I would like to know why it is doing this and what are some ways to correct the script. Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/ Share on other sites More sharing options...
homchz Posted June 17, 2006 Share Posted June 17, 2006 Which one is line 8?? Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/#findComment-46816 Share on other sites More sharing options...
Fyorl Posted June 17, 2006 Share Posted June 17, 2006 Looks like the script above the second Location header is outputting something (maybe an error message you've set up?) and that will cause the redir to fail. Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/#findComment-46818 Share on other sites More sharing options...
jacer17 Posted June 19, 2006 Author Share Posted June 19, 2006 I tryed modifying the script.[code]<?if(!$_POST['submit']){ $uname = $_POST['uname']; $password = $_POST['password']; if( (!$uname) or (!$password) ) { header(" Location: login.php "); exit(); } $conn = mysql_connect('localhost', 'connect', 'connect') or die('wrong connection info'); mysql_select_db('fcp_fcodes') or die('No database named that'); $sql="select * from fcdatabase where uname='$uname' and password = password('$password')"; $results = mysql_query($sql) or die("could not execute query"); $row = mysql_fetch_object($results); $id = $row->id; $firstname = $row->fname; $num = mysql_numrows($results); if($num !=0 ) { setcookie("id", $id, time()+36000); setcookie("firstname", $firstname, time()+36000); header( " Location: settings.php?id=$id "); exit(); } else { header( " Location: login.php " ); exit(); }}else{?><html><head></head><body><form action="<?=$_SERVER['PHP_SELF']?>" method="post"><table><tr><td><b>Username:</b></td><td><input type="text" name="uname" size="25" /></td></tr><tr><td><b>Password:</b></td><td><input type="password" name="password" size="25" /></td></tr><tr><td><input type="submit" name="submit" value="Log In" /></td></tr></table></form></body></html><?}?>[/code]This time I get only a blank page Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/#findComment-47488 Share on other sites More sharing options...
mainewoods Posted June 20, 2006 Share Posted June 20, 2006 The header function will fail if there is so much as a single invisible space before your first php start tag.If you are going to use the header() or cookies or session variables, put the php start tag on the first line of the source code without so much as a singe space before it.[code]<?php //must be on the very first line, no spaces before it[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/#findComment-47530 Share on other sites More sharing options...
jacer17 Posted June 21, 2006 Author Share Posted June 21, 2006 The php tag is the first line, except the pasted code.There is no spaces before it and it is top line. Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/#findComment-47919 Share on other sites More sharing options...
wildteen88 Posted June 21, 2006 Share Posted June 21, 2006 This:[code]<?if(!$_POST['submit']){[/code]should be:[code]<?if(isset($_POST['submit'])){[/code]ALso I highly suggest you chnage this:[code]$uname = $_POST['uname'];$password = $_POST['password'];[/code]to the following:[code]$uname = mysql_real_escape_string(trim($_POST['uname']));$password = mysql_real_escape_string(trim($_POST['password']));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12269-error-with-headings-and-cookies/#findComment-47970 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.