/    Sign up×
Community /Pin to ProfileBookmark

Cookie script returning errors

Hi all. On my site I use a cookie script (that’s optional) that lets a user fill out a form and then his name and some info will appear at the top of the page when he reloads it after submitting the form. When the user fills out the form I get the error:

[QUOTE]

Warning: Cannot modify header information – headers already sent by (output started at /home/freehost/t35.com/p/r/projep/a.php:1) in /home/freehost/t35.com/p/r/projep/top.php on line 5

[/QUOTE]

I use t35.com as my host right now and I don’t understand what i did wrong. the cookie code i use is:

[code=php]<?
$username = $_GET[“nme”];
if (! $username) {
} else {
setcookie(“username”, $username);
}
?>[/code]

Thanks in advance.

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@JonaMar 03.2004 — [font=arial]It means that you already sent HTML data to the browser. You have to set the cookie in the PHP code before you echo anything, and before you print out your HTML tag or anything.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 03.2004 — I have it before my doctype, but I also use includes statements to include the file that contains the cookie code. But when i go to the file with the php code (http://projep.t35.com/top.php) and do it, I get the same errors.
Copy linkTweet thisAlerts:
@JonaMar 03.2004 — [font=arial]Make sure you're not sending any header information to the browser in any files, even the includes files.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 04.2004 — this is the includes script:[code=php]<?
$a = $_GET['a'];
include 'top.php';
if(! $a) {
echo("No file requested.");
} else {
include ("articles/" . $a . ".html");
}
include 'bottom.php';
?>[/code]

i cant change the order. everything else works..
Copy linkTweet thisAlerts:
@JonaMar 04.2004 — [font=arial]First, it should be coded like this (to avoid further errors if you ever got a new host with a tighter PHP install).[/font]

[code=php]
<?
include 'top.php';

if(isset($_GET["a"]))
{
$a = $_GET['a'];
include ("articles/" . $a . ".html");
} else {
echo("No file requested.");
}
include 'bottom.php';
?>
[/code]


[font=arial]What is the code in top.php? Is that where you are setting your cookie?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 04.2004 — the variable MUST be before the top.php sorry. i need it for the action part to work right. and i wanna get a new host.. y is my code invalid or something?
Copy linkTweet thisAlerts:
@JonaMar 04.2004 — [font=arial]Technically, yes. If you moved to another host, and they had a tighter install of PHP, you would get an error. You should check to see if the $_GET variable is set before you set it to a variable, rather than the other way around.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 04.2004 — I don't understand what's wrong with it. I can see how your code would make sense, but why is mine wrong? It works fine and would only error if there was no get.... then it would just show the text. I'm lost.
Copy linkTweet thisAlerts:
@JonaMar 05.2004 — [i]Originally posted by omega [/i]

[B]I don't understand what's wrong with it. I can see how your code would make sense, but why is mine wrong? It works fine and would only error if there was no get.... then it would just show the text. I'm lost. [/B][/QUOTE]


[font=arial]Like I said, technically that'd throw an error even if it was present. I've worked with this kind of thing before. I have a tight install on my local webserver, and that script would most likely throw an error about that. I just thought I'd give you the heads up. No big deal, though.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 05.2004 — I already changed my script to yours, I was simply wondering the exact reason it'd error for future reference.
Copy linkTweet thisAlerts:
@JonaMar 05.2004 — [font=arial]Would you mind showing me the code in top.php, or is your problem solved?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@crh3675Mar 05.2004 — Use the ob_start() and ob_end_flush() functions to output data to the browser and avoid errors
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 05.2004 — It still won't work. And there's a ton of it, most of it irrelevant. How much do you want?
Copy linkTweet thisAlerts:
@JonaMar 05.2004 — [font=arial]The first ten or so lines, and the last ten or so lines should do...[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 05.2004 — K.

the beginning of top.php
<i>
</i>&lt;?
$username = $_GET["nme"];
if (! $username) {
} else {
setcookie("username", $username);
}
?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;

...

the large excessive php code for the cookie-using message
[code=php]

<div id="name">
<?
if (!$username) {
$defaultname = "YoUr NaMe";
} else {
$defaultname = $username;
}
if ($justset=="y") {
$setform = "<p>The cookie will be active once you refresh this page or go to another page on this site.</p>";
} else {
$setform = "<a href='a.php?a=wtfnme'>WTF is this? --&gt;</a><form action='" . $_SERVER['PHP_SELF'] . "'><fieldset><input type='hidden' name='justset' value='y' /><input type='hidden' name='a' value='" . $a . "' /><input type='hidden' name='s' value='" . $s . "' /><input type='hidden' name='css' value='" . $css . "' />
<input type='text' value='" . $defaultname . "' name='nme' maxlength='20' /><input type='submit' value='///' /><input type='reset' value='clear' /></fieldset></form>";
}
$time = date("g"); //Gives hour 01-12
$apm = date("a"); //Gives am or pm
if($time >= 08 && $apm=="pm") {
$blah = "Go to bed, "; //If its past 8pm then blah=go to bed
$punc = "!";
}
if($time < 12 && $time > 04 && $apm=="am") {
$blah = "Awake already, "; //If it's 8pm - 8am then blah=awake already
$punc = "?";
}
if($time >= 01 && $time <= 04 && $apm=="am") {
$blah = "You're an insomniac, "; //If it's midnight to 4am then blah=youre an insomniac
$punc = "!!!";
}
if ($time >= 01 && $time <= 04 && $apm=="pm") {
$blah = "'Sup, ";
$punc = "?";
}
if ($time >= 05 && $time <= 07 && $apm=="pm") {
$blah = "Welcome, "; //If it's between 8am and 8pm bal=welcome
$punc = "!";
}
$hour = date("g"); //Gives hour 1-12
$min = date("i"); //gives min 00-59
$month = date("n"); //Gives month 1-12
$day = date("d"); //Gives day 1-31
$year = date("y"); //Gives year in 2 digits
$message = $blah . $username . $punc . "<br />By the way it's " . $hour . ":" . $min . $apm . ", " . $month . "/" . $day . "/" . $year;
if (isset($_COOKIE["username"])) { //If cookie is set then message is echoed
echo $message;
} else {
$string = $_SERVER['PHP_SELF'];
$boom = explode("/", $string);
echo $setform;
}
?></div>
[/code]

and the end of top.php
<i>
</i>&lt;!-- My w.i.u.t.-ly (whenever i update this-ly) ramblings --&gt;
&lt;div id="ramblings"&gt;


i would like to note that the cookie script used to work, then t35 went down, and when it came back up, it returned errors.
Copy linkTweet thisAlerts:
@JonaMar 05.2004 — [font=arial]Do you think maybe they put in ads that send header information before your code? Also, make sure you don't have spaces or new lines before your PHP code begins (that might make a difference).[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 05.2004 — I don't know. I know they add an ad script aftert the </html> tag.
Copy linkTweet thisAlerts:
@JonaMar 05.2004 — [font=arial]That is compeletly weird... I don't know what the problem could be. That error has always meant the same thing in the past, for me...[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorMar 05.2004 — and it did work b4.... ah well. its probly server side, t35 has been VERY messed as of late. I hope flatface gives me subby.
×

Success!

Help @Ben_Rogers spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...