headers already sent

  • Onderwerp starter Onderwerp starter vacla
  • Startdatum Startdatum
Status
Niet open voor verdere reacties.

vacla

Gebruiker
Lid geworden
27 feb 2013
Berichten
43
ik krijg steeds dit probleem: Warning: Cannot modify header information - headers already sent by (output started at /srv/disk6/1501985/www/wmhelp.co.nf/nl/login/index.php:169) in /srv/disk6/1501985/www/wmhelp.co.nf/nl/login/index.php on line 190
Dit is mijn script:
PHP:
<?php
session_start();
if (isset($_SESSION['ingelogd'])) {
    header('location: member.php');
} else {
    ?>
    
    <!doctype html>
    <html>
    <head>
    <style>
    *{margin:0;padding:0;}
*:focus{outline:none;}

h2 {
margin: 0;
 font-size: 3em; 
 line-height: 1em; 
 margin-bottom: 0.5em
 }
 
h3 {
margin: 0;
 font-size: 2.25em;
 line-height: 1.3333333333333333333333333333333em;
 margin-bottom: 0.6667em
 }
 
h4 {
margin: 0; 
font-size: 1.5em; 
line-height: 1em; 
margin-bottom: 1em
}

h5 {
margin: 0;
 font-size: 1.3125em; 
 line-height: 1.1428571428571428571428571428571em;
 margin-bottom: 1.1428571428571428571428571428571em
 }

body{
    background:#e4a090;
    font-size: 16px;
    font-family: Arial, Helvetica, sans-serif;
}

.tel {
    color:#000
}


a {
    text-decoration:none
}


img {
    border:none
}


#ssssnake {
    display: block;
    text-align: left;
    background: #ed9780
}


#card {
    margin:1 , 1;
    position:relative;
    display:block; 
    width:350px; 
    min-height:120px; 
    background:#f8f8f5;
    margin:50px auto 0px;
}
#card2 {
    margin:1 , 1;
    position:relative;
    display:block; 
    width:350px; 
    min-height:10px; 
    background:#f8f8f5;
    margin:50px auto 0px;
}


header {
    text-align:center;
    display:block; 
    position:relative; 
    width:400px; 
    height:280px; 
    padding:60px 100px 0px;
    margin:0px auto
}


header h1 {
    display:block; 
    margin:10px 0px 0px 0px; 
    border:none;
    color:#8e3633;
    font-size:38px
}


header h2 {
    color: #ffffff; 
    font-size:38px
}


header h2:hover {
    text-decoration:underline; 
    color:#998dc9; 
    cursor:pointer
}


header h3 {
    display:block;
    font-size:18px;
    margin-top:10px
}

footer {
    width: 100%;
height:100%;
background:#000;
display:block;
float:left;
}

p {
color:#000000;
text-align:center;
padding:30px 0px 0px 0px;
}

#container {
    width: 100%;
    overflow:hidden;
    min-width:960px;
}
#left {
    float: left;
    width: 50%;
    display: inline;
}
#right{
    float:right;
    width: 50%;
    display: inline;
}
bericht {
    display:block; 
    width:100px; 
    height:100px;
}
    </style>
    <title>WMHelp</title>
    </head>
    <body>
    
    <?php
    if (isset($_POST['hidden'])) {
        include "mysql_connect.php";
        
        //variable
        $gebruikersnaam = mysql_real_escape_string($_POST['gebruikersnaam']);
        $wachtwoord = mysql_real_escape_string($_POST['wachtwoord']);
        
        $sql = "SELECT * FROM login1 WHERE gebruikersnaam='".$gebruikersnaam."' AND wachtwoord='".$wachtwoord."'";
        $query = mysql_query($sql);
        $num = mysql_num_rows($query);
    
        if($num == 1) {
            //Ingelogd
            $sql2 = "SELECT * FROM login1 WHERE gebruikersnaam='".$gebruikersnaam."' AND wachtwoord='".$wachtwoord."'";
            $query2 = mysql_query($sql2);
            $fetch = mysql_fetch_assoc($query2);
            
            $_SESSION['gebruikersnaam '] = $gebruikersnaam;
            $_SESSION['id'] = $fetch['id'];
            $_SESSION['ingelogd'] = true;
            exit(header('location: member.php'));
        } else {
            echo "Gebruikersnaam of wachtwoord is verkeerd";
        }
    
    
    } else {
        ?>
                    
        <div id="card">        
        <form action="index.php" method="post">
            <div id="left">
            Gebruikersnaam:<br>
            <input type="text" name="gebruikersnaam" /><br>
            </div>
            <tr/>
            <div id="right">
            Wachtwoord:<br>
            <input type="password" name="wachtwoord" /><br>        
            </div>        
            <div id="left">    
            <input type="hidden" name="hidden">
            </div>
            <center><input type="submit" value=" Login " width="100">&nbsp;&nbsp; <a href="./register.php"><font color="red"> Registreer </font></a></center>
        </form>
        </div>
        <?php
    }
}
?>

</body>
</html>
Kunnen jullie mij verder helpen?
Groetjes vacla
 
Er staat precies wat er ms is. Je kunt de header niet aanpasen, nadat je al informatie aan de browser hebt gegeven. Op regel 190 probeer je de header aan te passen, terwijl je al HTML en CSS verstuurd hebt.
 
dit is de correcte code :
PHP:
<?php
session_start();
if (isset($_SESSION['ingelogd'])) {
	header('location: member.php');
} else {
	if (isset($_POST['hidden']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
		include "mysql_connect.php";
		
		//variable
		$gebruikersnaam = mysql_real_escape_string($_POST['gebruikersnaam']);
		$wachtwoord = mysql_real_escape_string($_POST['wachtwoord']);
		
		$sql = "SELECT * FROM login1 WHERE gebruikersnaam='".$gebruikersnaam."' AND wachtwoord='".$wachtwoord."'";
		$query = mysql_query($sql);
		$num = mysql_num_rows($query);
	
		if($num == 1) {
			//Ingelogd
			$sql2 = "SELECT * FROM login1 WHERE gebruikersnaam='".$gebruikersnaam."' AND wachtwoord='".$wachtwoord."'";
			$query2 = mysql_query($sql2);
			$fetch = mysql_fetch_assoc($query2);
			
			$_SESSION['gebruikersnaam '] = $gebruikersnaam;
			$_SESSION['id'] = $fetch['id'];
			$_SESSION['ingelogd'] = true;
			header('location: member.php');
			exit;
		} else {
			echo "Gebruikersnaam of wachtwoord is verkeerd";
		}
		
	
	
	} else {
		?>
        			
        <div id="card">		
		<form action="http://wmhelp.co.nf/nl/login/index.php" method="post">
			<div id="left">
			Gebruikersnaam:<br>
			<input type="text" name="gebruikersnaam" /><br>
			</div>
			<tr/>
			<div id="right">
			Wachtwoord:<br>
			<input type="password" name="wachtwoord" /><br>		
            </div>		
            <div id="left">	
			<input type="hidden" name="hidden">
			</div>
			<center><input type="submit" value=" Login " width="100">&nbsp;&nbsp; <a href="./register.php"><font color="red"> Registreer </font></a></center>
		</form>
		</div>
		<?php
	}
	?>
		<!doctype html>
	<html>
	<head>
	<style>
	*{margin:0;padding:0;}
*:focus{outline:none;}

h2 {
margin: 0;
 font-size: 3em; 
 line-height: 1em; 
 margin-bottom: 0.5em
 }
 
h3 {
margin: 0;
 font-size: 2.25em;
 line-height: 1.3333333333333333333333333333333em;
 margin-bottom: 0.6667em
 }
 
h4 {
margin: 0; 
font-size: 1.5em; 
line-height: 1em; 
margin-bottom: 1em
}

h5 {
margin: 0;
 font-size: 1.3125em; 
 line-height: 1.1428571428571428571428571428571em;
 margin-bottom: 1.1428571428571428571428571428571em
 }

body{
	background:#e4a090;
	font-size: 16px;
	font-family: Arial, Helvetica, sans-serif;
}

.tel {
	color:#000
}


a {
	text-decoration:none
}


img {
	border:none
}


#ssssnake {
	display: block;
	text-align: left;
	background: #ed9780
}


#card {
    margin:1 , 1;
    position:relative;
	display:block; 
	width:350px; 
	min-height:120px; 
	background:#f8f8f5;
	margin:50px auto 0px;
}
#card2 {
    margin:1 , 1;
    position:relative;
	display:block; 
	width:350px; 
	min-height:10px; 
	background:#f8f8f5;
	margin:50px auto 0px;
}


header {
	text-align:center;
	display:block; 
	position:relative; 
	width:400px; 
	height:280px; 
	padding:60px 100px 0px;
	margin:0px auto
}


header h1 {
	display:block; 
	margin:10px 0px 0px 0px; 
	border:none;
	color:#8e3633;
	font-size:38px
}


header h2 {
	color: #ffffff; 
	font-size:38px
}


header h2:hover {
	text-decoration:underline; 
	color:#998dc9; 
	cursor:pointer
}


header h3 {
	display:block;
	font-size:18px;
	margin-top:10px
}

footer {
	width: 100%;
height:100%;
background:#000;
display:block;
float:left;
}

p {
color:#000000;
text-align:center;
padding:30px 0px 0px 0px;
}

#container {
	width: 100%;
	overflow:hidden;
	min-width:960px;
}
#left {
	float: left;
	width: 50%;
	display: inline;
}
#right{
	float:right;
	width: 50%;
	display: inline;
}
bericht {
	display:block; 
	width:100px; 
	height:100px;
}
	</style>
	<title>WMHelp</title>
	</head>
<?php
}
?>


</html>
 
Nou, correct... :-)

Zodra de browser de header location heeft ontvangen zal hij de rest van de pagina negeren, maar jouw code produceert de rest van de pagina nog wel, en zou daarin dus ook dingen kunnen doen die helemaal niet moeten gebeuren omdat de bezoeker dat deel van de pagina sowieso nooit zou moeten zien (en dus ook niet ziet).

let ook op dat je nu niet kijjkt of iemand is ingelogd, maar of een bepaalde var in de sessie bestaat. Als in die var staat "nee, deze persoon is helemaal niet aangelogd" dan vindt jij hem toch aangelogd.
Het is een stuk veiliger om de variabele altijd te setten en hem op 0 te zetten voor niet ingelogd, en 1 voor wel ingelogd.
 
Je moet niets omdraaien, je moet je code zo maken dat hij alleen doet wat nodig is. Als een ingelogde bezoeker alleen naar een andere URL gestuurd moet worden dan kan het script dus stoppen na de header().
 
Nee, je php code print immers all de pagina voor de doctype & header gedeclareerd zijn. Ook is je eerste else niet nodig ( als de header immers al verandert.. Probeer maar eens alles behalve de 1e if (Inc de eerste else als if). In de body te plaatsen
 
best ik heb mijn bedenkingen en aanpassingen één op je script toegepast

PHP:
<?php
session_start();
// let op uw script is niet veilig en is snel te manipuleren  denkt toch dit OO te doen
if (isset($_SESSION['ingelogd'])) {
    header('location: member.php');
} else {
    if (isset($_POST['hidden']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
        include "mysql_connect.php";
        //variable
        $gebruikersnaam = mysql_real_escape_string($_POST['gebruikersnaam']);
        $wachtwoord = mysql_real_escape_string($_POST['wachtwoord']);
        $sql = "SELECT * FROM login1 WHERE gebruikersnaam='".$gebruikersnaam."' AND wachtwoord='".$wachtwoord."'";
        $query = mysql_query($sql);
        $num = mysql_num_rows($query);
        if($num == 1) {
            //Ingelogd
            $sql2 = "SELECT * FROM login1 WHERE gebruikersnaam='".$gebruikersnaam."' AND wachtwoord='".$wachtwoord."'";
            $query2 = mysql_query($sql2);
            $fetch = mysql_fetch_assoc($query2);
            $_SESSION['gebruikersnaam '] = $gebruikersnaam;
            $_SESSION['id'] = $fetch['id'];
            $_SESSION['ingelogd'] = true;
            header('location: member.php');
            exit;
        } 
        $erGingIetsFout = true;
    }
}
?>
<!doctype html>
    <html>
    <head>
    <style>
/*dat hoort hier niet thuis en zal je wel in een css plaatsen<link href="yourfile.css" rel="stylesheet" type="text/css" />*/
    </style>
    <title>WMHelp</title>
    </head>
            <div id="card">
            <?php if($erGingIetsFout){echo'<div id="error">Gebruikersnaam of wachtwoord is verkeerd</div>';}?>
        <form action="http://wmhelp.co.nf/nl/login/index.php" method="post">
            <div id="left">
            Gebruikersnaam:<br>
            <input type="text" name="gebruikersnaam" /><br>
            </div>
            <tr/>
            <div id="right">
            Wachtwoord:<br>
            <input type="password" name="wachtwoord" /><br>     
            </div>      
            <div id="left"> 
            <input type="hidden" name="hidden">
            </div>
            <center><input type="submit" value=" Login " width="100">&nbsp;&nbsp; <a href="./register.php"><font color="red"> Registreer </font></a></center>
        </form>
        </div>
</html>
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan