Jump to content

Headers already sent - please help with workaround


tommyda

Recommended Posts

I have read the read me before posting thread and I still cant get my head around this. Im sorry if this is an annoying question.

 

Please help me!!

 

 

Heres the error:

Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/phptesting/mobile-social-networking/config.php:2) in /home/website/public_html/phptesting/mobile-social-networking/web/login.php on line 10
Cant set cookie!

 

Heres the code:

Login.php

<?
include("header.php");

$u = $_POST['u'];
$p = $_POST['p'];

$check = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='$u' AND `password`='$p'"));

if($check==1) {
	if(setcookie("BoardW",$u,time()+3600)) {
	 	Header("Location: index.php");
	}
	else {
	 	print "Cant set cookie!";
	}
}
else {
	print "User/Pass mismatch!";
}

include("footer.php");
?>

 

Config.php

<?
include './paths.php';
ob_start();
mysql_pconnect(localhost,w***m,password);
mysql_select_db(w***m);
$password = 'password';
?>

Link to comment
Share on other sites

Something in your header.php file is outputting data to the browser. This is causing your problem.

 

Side note: you shouldn't use short tags (<?), you should always use long tags (<?php). Short tags don't work on some servers, so if you want to move your code it won't work. And the server you are on now may suddenly change that option some day. It's sloppy programming.

Link to comment
Share on other sites

Ok thanks for the tip I will use <?php from now on.

There is no output in paths.php just variables

 

the header however contains a lot of output.

 

<?
include("../config.php");
?>

<html>
<head>
<title><? include'../title.php';?></title>
<style>
* {
	font-family: arial;
	font-size: 10pt;
	line-height: 18px;
	color: #FFF;
}

body {
	background-color: #000;
}

a {
	text-decoration: underline;
	font-weight: bold;
	color: #CCFFFF;
}
a:hover {
	text-decoration: none;
	font-weight: bold;
	color: #fff;
}

input,select,textarea {
	font-family: tahoma;
	font-size: 10pt;
	color: #CCFFFF;
	border: 2px solid #888;
	background-color: #000;
	padding: 2px;
}
</style>
</head>

<body>
<center>
<table border=0 cellpadding=3 cellspacing=2 bgcolor=#FFFFFF width=600>
<tr>
  <td colspan=2 bgcolor="#0d0d0d"><a href="index.php"><img src="../<? include'../logo.php';?>" alt="logo" /></a></td>
</tr>
<Tr>
  <td width="150" valign="top" bgcolor="#444444"><? include("nav.php"); ?></td>
  <td width="450" valign="top" bgcolor="#222222">

Link to comment
Share on other sites

Did you really read the headers already sent sticky? You cannot send any out put to the browser prior to calling the header function. html is output.

 

I understand that completely I am asking for help on working around this because I have no idea how i'm going to do it.

Link to comment
Share on other sites

try this

 

<?php
include_once("../config.php");
$u = $_POST['u'];
$p = $_POST['p'];

$check = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='$u' AND `password`='$p'"));

if($check==1) {
    if(setcookie("BoardW",$u,time()+3600)) {
        Header("Location: index.php");
    }
    else {
        $msg ="Cant set cookie!";
    }
}
else {
    $msg ="User/Pass mismatch!";
}
include("header.php");
echo $msg;
include("footer.php");
?>

Link to comment
Share on other sites

It looks like login.php includes header.php which immediately includes config.php, which calls ob_start() after including paths.php.  That leaves 2 options

 

1.  Either login.php or config.php or header.php has a blank line at the top, or some kind of hidden character before the php tag

2.  paths.php does produce output

 

Try moving the ob_start() above the include of paths.php.  If that doesn't work, move it to the top of login.php.  If THAT doesn't work, then something is at the top of login.php.

Link to comment
Share on other sites

<?php
ob_start();
$cookieCheck = setcookie("BoardTest","cookie",time()+3600);

include("header.php");

$u = $_POST['u'];
$p = $_POST['p'];

$check = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='$u' AND `password`='$p'"));

if($check==1) {
    if($cookieCheck) {
        ob_end_clean();
        setcookie("BoardW",$u,time()+3600);
        Header("Location: index.php");
    }
    else {
        print "Cant set cookie!";
    }
}
else {
    print "User/Pass mismatch!";
}

include("footer.php");
?>

Link to comment
Share on other sites

I think we have established that header.php is causing the problem.

 

But not why it causes the problem.  If you establish why (by moving the ob_start() around), you can fix it.  In theory, ob_start() should fix the problem if done early enough.  Since it's being called but isn't fixing the problem, it must not be run early enough.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.