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:
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
edit: and I do know I have the cookie, I checked and even checked the value, it’s correct
anybody? I’ve searched php’s website and can’t find anything
system
July 7, 2004, 10:31pm
3
does anybody know what the problem is? I searched php’s site and couldn’t find anything there either…
system
July 7, 2004, 10:41pm
4
i think u have to put session_start() at the top of the page in order to access ur cookie
system
July 7, 2004, 10:42pm
5
I have session start at the beginning, it just wasn’t included in that part of the script, have any other ideas?
system
July 7, 2004, 10:45pm
6
are you outputting anything prior to accessing the cookie? If not, paste the whole code.
system
July 7, 2004, 10:47pm
7
<?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:
system
July 7, 2004, 10:50pm
8
the name of the variable is Style or style?
system
July 7, 2004, 10:53pm
9
the cookie is Style but the recordset is style and the variable defined in the page is $style
system
July 7, 2004, 10:55pm
10
try echoing $style instead of $_COOKIE[‘Style’]
system
July 7, 2004, 10:58pm
11
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
system
July 8, 2004, 3:21am
12
#whats the output of this when you put it on top of the page
print_r($_COOKIE);
system
July 8, 2004, 3:22am
13
it’s fixed sorry, forgot to post, it was that it was in the wrong directory
system
July 8, 2004, 3:25am
14
Thats c00l, normally if you want that $_COOKIE to be global.
do it like this
setcookie("name", "value", time()+43600, "/");
#that always works.