# Cookies not being recieved

**URL:** <https://forum.kirupa.com/t/cookies-not-being-recieved/56798>\
**Category:** programming\
**Created:** [July 7, 2004, 8:45pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798 "2004-07-07T20:45:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [July 7, 2004, 8:45pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/1 "2004-07-07T20:45:22Z")

</div>

Is there any way to open up cookies for usage, like session\_start but for cookies?

I’m being driven insane becaue my stupid code won’t see that their’s a cookie and treats it as empty, look at this code:

```php

if(isset($_COOKIE['Style'])) {
$style = $_COOKIE['Style'];
} elseif (isset($row_styles['style'])) {
$style = $row_styles['style'];
} else {
$style = "dark";
}
if($style=='bright') {
	echo "<link href=\"/nokrev/testing/master_bright.css\" rel=\"stylesheet\" title=\"Bright\" type=\"text/css\">";
	echo "<link href=\"/nokrev/testing/master.css\" rel=\"alternate stylesheet\" title=\"Dark\" type=\"text/css\">";
	echo $_COOKIE['Style'];
}
	else {
	echo "<link href=\"/nokrev/testing/master.css\" rel=\"stylesheet\" title=\"Dark\" type=\"text/css\">";
	echo "<link href=\"/nokrev/testing/master_bright.css\" rel=\"alternate stylesheet\" title=\"Bright\" type=\"text/css\">";
	echo $_COOKIE['Style'];
}

```

It should check to see if there’s a cookie, and if not use the recordset or the one defined, and it should also echo the value of the cookie in the header, which it doesn’t do. You can see the code at [nokrev.com](http://nokrev.com)

edit: and I do know I have the cookie, I checked and even checked the value, it’s correct

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 9:21pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/2 "2004-07-07T21:21:02Z")

</div>

anybody? I’ve searched php’s website and can’t find anything

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:31pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/3 "2004-07-07T22:31:43Z")

</div>

does anybody know what the problem is? I searched php’s site and couldn’t find anything there either…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:41pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/4 "2004-07-07T22:41:33Z")

</div>

i think u have to put session\_start() at the top of the page in order to access ur cookie

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:42pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/5 "2004-07-07T22:42:46Z")

</div>

I have session start at the beginning, it just wasn’t included in that part of the script, have any other ideas?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:45pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/6 "2004-07-07T22:45:46Z")

</div>

are you outputting anything prior to accessing the cookie? If not, paste the whole code.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:47pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/7 "2004-07-07T22:47:05Z")

</div>

```php
<?php 
session_start();
$hostname_users = "localhost";
$database_users = "users";
$username_users = "nokrev";
$password_users = "";
$users = mysql_pconnect($hostname_users, $username_users, $password_users) or die(mysql_error());

$username = "";
if (isset($HTTP_SESSION_VARS['username'])) {
  $username = $HTTP_SESSION_VARS['username'];
}
mysql_select_db($database_users, $users);
$query_styles = "SELECT style FROM user_auth WHERE userName = '$username'";
$styles = mysql_query($query_styles, $users) or die(mysql_error());
$row_styles = mysql_fetch_assoc($styles);
$totalRows_styles = mysql_num_rows($styles);

if(isset($_COOKIE['Style'])) {
$style = $_COOKIE['Style'];
} elseif (isset($row_styles['style'])) {
$style = $row_styles['style'];
} else {
$style = "dark";
}
if($style=='bright') {
	echo "<link href=\"/nokrev/testing/master_bright.css\" rel=\"stylesheet\" title=\"Bright\" type=\"text/css\">";
	echo "<link href=\"/nokrev/testing/master.css\" rel=\"alternate stylesheet\" title=\"Dark\" type=\"text/css\">";
	echo $_COOKIE['Style'];
}
	else {
	echo "<link href=\"/nokrev/testing/master.css\" rel=\"stylesheet\" title=\"Dark\" type=\"text/css\">";
	echo "<link href=\"/nokrev/testing/master_bright.css\" rel=\"alternate stylesheet\" title=\"Bright\" type=\"text/css\">";
	echo $_COOKIE['Style'];
}

mysql_free_result($styles);
?>

```

It’s being an include in every page on my site… but this is driving me insane, please help :puzzled:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:50pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/8 "2004-07-07T22:50:03Z")

</div>

the name of the variable is Style or style?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:53pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/9 "2004-07-07T22:53:21Z")

</div>

the cookie is Style but the recordset is style and the variable defined in the page is $style

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:55pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/10 "2004-07-07T22:55:05Z")

</div>

try echoing $style instead of $\_COOKIE[‘Style’]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 10:58pm UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/11 "2004-07-07T22:58:12Z")

</div>

now I get dark, but the cookie has “bright” in it, because the if statement is giong to the else, where it defines $style as dark

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 3:21am UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/12 "2004-07-08T03:21:20Z")

</div>

```php

#whats the output of this when you put it on top of the page
print_r($_COOKIE);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 3:22am UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/13 "2004-07-08T03:22:35Z")

</div>

it’s fixed sorry, forgot to post, it was that it was in the wrong directory

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 3:25am UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/14 "2004-07-08T03:25:12Z")

</div>

Thats c00l, normally if you want that $\_COOKIE to be global.

do it like this

```php

 setcookie("name", "value", time()+43600, "/");
 #that always works. 
 

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 3:29am UTC](https://forum.kirupa.com/t/cookies-not-being-recieved/56798/15 "2004-07-08T03:29:03Z")

</div>

yep, that’s what I did
