jacko310592 Posted January 23, 2010 Share Posted January 23, 2010 hey guys, the following code is for an admin login screen im creating, but currently, when i try to login i get... Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\admin.php:11) in E:\xampp\htdocs\admin.php on line 24 Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\admin.php:11) in E:\xampp\htdocs\admin.php on line 25 line 11 = <?php if ($_GET["logout"] == "true"){ line 24 = setcookie('MyLoginPage', md5($randomString1.$password.$randomString2)); line 25 = header("Location: ./"); here is the full code.... <?php $thisPage='Admin Login';?> <?php $userName = $_POST['name']; $password = $_POST['pass']; $userInfo = array('user1'=>'pass1', 'user2'=>'pass2'); $randomString1 = "xu5rac9Erub7KAPh"; $randomString2 = "P327efA5ujadRAfa"; ?> <?php if ($_GET["logout"] == "true"){ setcookie ("MyLoginPage", "", time() - 3600); unset($_COOKIE['MyLoginPage']); header("Location: ./admin.php"); exit; } ?> <?php if (isset($_POST['submit'])){ if ($password != $userInfo[$userName] || empty($password)) { echo '<div id="dataPass">The Username and/or Password you entered was incorrect</div>'; } elseif ($password == $userInfo[$userName]) { setcookie('MyLoginPage', md5($randomString1.$password.$randomString2)); header("Location: ./"); exit; } else { echo "<p>Sorry, you could not be logged in at this time. Refresh the page and try again.</p>"; } } ?> <?php if (isset($_COOKIE['MyLoginPage'])) {echo 'You are logged in';}?> <form action="#" method="post"><fieldset style="text-align:center;"> <legend>Admin Login</legend> <table style="margin:0 auto"><tr><td><label>Username:</label></td> <td><input type="text" name="name" id="name" /></td></tr> <tr><td><label>Password:</label></td> <td><input type="password" name="pass" id="pass" /></td></tr> <tr><td></td><td><input type="submit" name="submit" id="submit" value="Login" /></td></tr></table> </fieldset></form> can anyone suggest why this is happening? thanks Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/ Share on other sites More sharing options...
jacko310592 Posted January 23, 2010 Author Share Posted January 23, 2010 oh, seem'd to fix this problem by changing this: <?php $userName = $_POST['name']; $password = $_POST['pass']; $userInfo = array('user1'=>'pass1', 'user2'=>'pass2'); $randomString1 = "xu5rac9Erub7KAPh"; $randomString2 = "P327efA5ujadRAfa"; ?> <?php if ($_GET["logout"] == "true"){ setcookie ("MyLoginPage", "", time() - 3600); unset($_COOKIE['MyLoginPage']); header("Location: ./admin.php"); exit; } ?> to this: <?php $userName = $_POST['name']; $password = $_POST['pass']; $userInfo = array('user1'=>'pass1', 'user2'=>'pass2'); $randomString1 = "xu5rac9Erub7KAPh"; $randomString2 = "P327efA5ujadRAfa"; if ($_GET["logout"] == "true"){ setcookie ("MyLoginPage", "", time() - 3600); unset($_COOKIE['MyLoginPage']); header("Location: ./admin.php"); exit; } ?> can anyone explain to me why it didnt like me ending then starting a new php statment? Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000386 Share on other sites More sharing options...
jacko310592 Posted January 23, 2010 Author Share Posted January 23, 2010 -- correction: it still doesntwork properly, it seem'd to work once i changed the above, but its just doing the same as before now Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000390 Share on other sites More sharing options...
JAY6390 Posted January 23, 2010 Share Posted January 23, 2010 The problem is that you have whitespace which is classed as output by php. Your second code should work assuming you've actually submitted data. The errors wont be the same now. Can you post them. More than likely you're getting notice undefined index errors too now Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000392 Share on other sites More sharing options...
jacko310592 Posted January 23, 2010 Author Share Posted January 23, 2010 thanks JAY6390, i removed all the whitespace and it worked. but i needed to then add it to a more of a styled page (so i needed to add <html><head><body> tags etc) which is the following code, which then made the code not work again <?php $thisPage='Admin Login';?> <?php $userName = $_POST['name']; $password = $_POST['pass']; $userInfo = array('user1'=>'pass1', 'user2'=>'pass2'); $randomString1 = "xu5rac9Erub7KAPh"; $randomString2 = "P327efA5ujadRAfa"; if (isset($_COOKIE['MyLoginPage'])){ if ($_GET["logout"] == "true"){ setcookie ("MyLoginPage", "", time() - 3600); unset($_COOKIE['MyLoginPage']); header("Location: ./admin.php"); exit; }} ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Admin</title> <meta name="robots" content="noindex,nofollow"> <link rel="shortcut icon" href="/favicon.ico"> <link rel="stylesheet" href="/css/css_style.css" type="text/css" charset="utf-8" /> </head> <body> <div style="height:100%; width:800px; margin:0 auto;"> <div style="height:25%;"></div> <div id="toplogo" style="margin-bottom:20px;"></div> <div id="head"> <?php echo "$thisPage"; ?> </div> <div id="info"> <?php if (isset($_POST['submit'])){ if ($password != $userInfo[$userName] || empty($password)) { echo '<div id="dataPass">The Username and/or Password you entered was incorrect</div>'; } elseif ($password == $userInfo[$userName]) { setcookie('MyLoginPage', md5($randomString1.$password.$randomString2)); header("Location: ./"); exit; } else { echo "<p>Sorry, you could not be logged in at this time. Refresh the page and try again.</p>"; } } ?> <?php if (isset($_COOKIE['MyLoginPage'])) {echo 'You are logged in';}?> <form action="#" method="post"><fieldset style="text-align:center;"> <legend>Admin Login</legend> <table style="margin:0 auto"><tr><td><label>Username:</label></td> <td><input type="text" name="name" id="name" /></td></tr> <tr><td><label>Password:</label></td> <td><input type="password" name="pass" id="pass" /></td></tr> <tr><td></td><td><input type="submit" name="submit" id="submit" value="Login" /></td></tr></table> </fieldset></form></div> </div> </body> </html> error: Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\admin.php:37) in E:\xampp\htdocs\admin.php on line 47 Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\admin.php:37) in E:\xampp\htdocs\admin.php on line 48 so im asuming ive arranged the code wrongly, can you suggest where? thanks Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000398 Share on other sites More sharing options...
JAY6390 Posted January 23, 2010 Share Posted January 23, 2010 Basically anything to do with headers you need to put before any output. You can put the tags after the closing ?> so long as there are no more header() values Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000399 Share on other sites More sharing options...
jacko310592 Posted January 23, 2010 Author Share Posted January 23, 2010 thanks a lot (: its works fine now Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000400 Share on other sites More sharing options...
JAY6390 Posted January 23, 2010 Share Posted January 23, 2010 No problem Link to comment https://forums.phpfreaks.com/topic/189527-error-cannot-modify-header-informaton/#findComment-1000406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.