Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SETCOOKIE not working - thanks for help in advance

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
0
0
CA
Hello All,

I am trying to set my cookie via a function. However, I am not getting any luck.

Here is my reqs:
1) At every page refresh,see if there is a function to set the cookie already in the code. if there is add to it and publis it as "Current request count is" increase old one by one.
2) At refresh, if there is no cookie set, set one and initialised at 0 and increase it by 1.

Below is mu code, however it is given error - Warning: Cannot modify header information - headers already sent by (output started at /home/tayo/public_html/pagereq.php:16) in /home/tayo/public_html/pagereq.php on line 19

Current request count is :
+++++++++++++++++++++++ Below is code+++++++++++++++++++
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Activity 8.1 </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="taxcalc.php" method="post">

<h1>  </h1>
</form>
<?php 

$count = 0;
setcookie(pagereq, $value, time()+30,',',0);

//Function set_pagereq
function set_pagereq($value){
if  (isset($pagereq)){
 return $count++;
}
else{
 return $count;
  }		
}

//Call Functions
$count=set_pagereq($value);

//print result
print "<p><div>
Current request count is :  <span class=\"text\">$count</span></p></div>"
?>
</body>
</html>
 
Several things wrong with that script:

1. There can be absolutely no output to screen/browser before attempting to use the setcookie function:

PHP Manual said:
Description

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
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.

2. Your Variables are all over the place, you should be getting undefined variable notices all around.

I strongly suggest you make sure your PHP installation is set to report these.

3. The first parameter of the setcookie function is a string so should be surrounded by quotes. Again this should be producing a notice about trying to use an undefined constant.

4. Once a cookie is set the method to retrieve the value of the cookie is using the $_COOKIE superglobal:

EXAMPLE from PHP MAnual said:
<?php
// Print an individual cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];

// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>

I strongly recommend looking at the manual at PHP.net, it has many many examples for all the functions you may need to use, and will help you become familiar with their usage.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Please I will appreciate a specific suggestion(s).

It is only giving me one error:
Cannot modify header information - headers already sent by (output started at /home/tayo/public_html/pagereq.php:16) in /home/tayo/public_html/pagereq.php on line 19
 
As I said and quoted there can be no output prior to the usage of setcookie. This includes any and all HTML and even white space.

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). [red]This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.[/red]

Which means your setcookie call must be placed before any output is sent to the browser. So nothing can come before the call. No text, no html, not even white space.

Code:
[red]<?PHP
setcookie(pagereq, $value, time()+30,',',0);

?>
[/red]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Activity 8.1 </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="taxcalc.php" method="post">

<h1>  </h1>
...
</html>
</form>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
After making the change as suggested
Code:
<?PHP 
$count = 0;
setcookie(pagereq, $value, time()+30,',',0);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Activity 8.1 </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="pagereq.php" method="post">

<h1>  </h1>

function set_pagereq($value){
if  (isset($pagereq)){
 return $count++;
}
else{
 return $count;
  }		
}


$count=set_pagereq($value);


print "<p><div>
Current request count is :  <span class=\"text\">$count</span></p></div>"
</body>
</form>
</html>

Still getting error
function set_pagereq($value){ if (isset($pagereq)){ return $count++; } else{ return $count; } } $count=set_pagereq($value); print "

Current request count is : $count
 
That's not an error, that just the rest of your code that's being directly output because its not surrounded by PHP tags:

Code:
<?PHP 
$count = 0;
setcookie(pagereq, $value, time()+30,',',0);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Activity 8.1 </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="pagereq.php" method="post">

<h1>  </h1>
[red]<?HP[/red]
[blue]
function set_pagereq($value){
if  (isset($pagereq)){
 return $count++;
}
else{
 return $count;
  }        
}


$count=set_pagereq($value);


print "<p><div>
Current request count is :  <span class=\"text\">$count</span></p></div>";
[red]?>[/red]
[/blue]
</body>
</form>
</html>

Just because you moved a part of your code above the html sections doesn't mean the rest of it doesn't still need to be surrounded by PHP tags.

This isn't complex PHP programming its the basic stuff. I'm starting to feel you are way out of your league here, and perhaps hiring someone to do this may be a better alternative.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I have solved my problem here thanks everyone for your input including vancunita.
<?php
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top