Unable to set cookie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Unable to set cookie

Post by kendall »

Hey,

Sorry about this question but for some reason i think i need someone outside of the box to fill me in on this

i am trying to set a cookie using

Code: Select all

<?php
	$cookie = setcookie('ratings','set the cookie',time()+60*60*24*30,$PHP_SELF,$_SERVER['HTTP_HOST']);
	header("Location: administration/index.php");
	exit;
?>
and cont the index page i check to see if this cookie was set using

Code: Select all

if($_COOKIE['ratings'] || $HTTP_COOKIE_VARS['ratings'] || $_REQUEST['ratings'])
	print "cookie set";
else print "Could not set cookie";
echo $ratings;
but i get "could not set cookie" statement

I only have the above script setting the cookie so there are no output statements or blank lines
theres no pre-script process
im using an administrative profile on a windows XP
im using mozilla but it also doesnt work in IE
my register globals are on for a PHP 4.3.4

am i missing the point here?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$host = explode('.',$_SERVER['HTTP_HOST']);
if(count($host) > 2) {
  while(count($host) > 2) {
    array_shift($host);
  }
  $host = '.' . implode('.',$host);
} else {
  $host = implode('.',$host);
}
$cookie = setcookie('ratings','set the cookie',time() 60*60*24*30,'/',$host);
also, be aware that using a header redirection while setting a cookie may not set the cookie.
Last edited by feyd on Fri Feb 10, 2006 6:04 pm, edited 1 time in total.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Unable to set cookie SOLVED

Post by kendall »

oohhh :oops:
Post Reply