Jump to content

error- cannot modify header informaton


jacko310592

Recommended Posts

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

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?

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

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

Archived

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

×
×
  • Create New...

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.