Jump to content

Recommended Posts

Hey ya, am having a wee problem setting a cookie. I'm using the exact same code as before with no joy, just getting an error saying cannot modify headers? Here is the code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cookie Setter</title>

<?php
$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=admin.php\">";
?> 


</head>

<body>
</body>
</html>

 

 

And the error message i get is:

 

 

Warning: Cannot modify header information - headers already sent by (output started at /home/cookieset.php:7) in /home/cookieset.php on line 10

 

 

Seems strange this is being denied when it worked before?

 

Thanks for any help!

 

Chris

 

Link to comment
https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/
Share on other sites

From the PHP Doc http://us2.php.net/setcookie

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Yeah i must admit i did read this earlier and thought it must be an old rule because i had this working on a previous prject, but you say it might have been set up on the server?

 

On then well i have a slight follow on question, if this has to be the first thing done in the script, how on earth do i GET a url variable so that i can put this into the cookie?

 

I'm just going to try adjusting the code so it's the first thing.

 

brb :)

so, the point of this script is to set the cookie then forward to admin.php immediately? no HTML is needed for this:

<?php
$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie('AboutVisit', date("F jS - g:i a"), $Month);
header('Location: admin.php');
exit;
?>

p.s. - note the quotes i put around AboutVisit...you were missing those

On then well i have a slight follow on question, if this has to be the first thing done in the script, how on earth do i GET a url variable so that i can put this into the cookie?

No, it has to be done before your script sends any output
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.