Click to See Complete Forum and Search --> : Cookie Headache


itbeings
02-03-2007, 08:31 PM
:confused: Am really confused about ggetting cookies info from the browser. It work on one page but it doesn't work on another payment. On page 1 I used
if(isset($_REQUEST['cookie_name']) && $_COOKIE['cookie_name']=="on"){
include "enabled.inc.php";
}else{
include "disabled.inc.php";
}

but on Page 2 I used
if(isset($_REQUEST['cookie_name']) && $_COOKIE['cookie_name']=="on"){
$_SESSION['yap']['link'] = $go;
header("Location: ./See");
}else{
//Redirect to URL link
header("Location: ".$go);
}

It does not seem to find the cookie and it just redirect, whereas the cookie_name is on. Can anyone tell me what could be the problem?

Thanks

NightShift58
02-03-2007, 08:44 PM
It's hard to tell from your code snips. The main problem is cookies always seems to be that cookies are designed to be sent to the visitor's browser at the end of the script, roughly speaking. As such, they only become available in the following page/script.

As I said, I can't tell from the snippets above how you've defined the cookies and where. If you initialized the cookie at the top of the page and then try to use it's value at the bottom of the same script, it will not work.

That would be one possibility.

The other, however remote, may have to do with the use of $_REQUEST. Here, too, I can't tell how or why you're using this form to test the existence of the cookie. Just use $_COOKIE... Otherwise, you are potentially testing for a different condition than you intended, depending on the GPC settings on your server.

itbeings
02-03-2007, 08:49 PM
This is how the cookie is been set
//Set cookies if required
if(!empty($_REQUEST['_a'])){
if($_REQUEST['_a'] == "_enable"){
$value = "on";
setcookie("int-url", $value, time()+360000000000000);
header("Refresh: 1; URL=./");
}elseif($_REQUEST['_a'] == "_disable"){
$value = "off";
setcookie("int-url", $value, time()-42000);
header("Refresh: 1; URL=./");
}
}

itbeings
02-03-2007, 08:50 PM
I mean
//Set cookies if required
if(!empty($_REQUEST['_a'])){
if($_REQUEST['_a'] == "_enable"){
$value = "on";
setcookie("cookie_name", $value, time()+360000000000000);
header("Refresh: 1; URL=./");
}elseif($_REQUEST['_a'] == "_disable"){
$value = "off";
setcookie("cookie_name", $value, time()-42000);
header("Refresh: 1; URL=./");
}
}

NightShift58
02-03-2007, 08:54 PM
And when is your next (failed) attempt to use the cookie? In the same script?

itbeings
02-03-2007, 09:00 PM
Not in the same script. It's on a different script on the home page.

NightShift58
02-03-2007, 09:06 PM
Could you do:<?php
print "<pre>";
print_r($_COOKIES);
print "</pre>";
exit;
?>... right before the spot where the cookie is not being recognized?

itbeings
02-03-2007, 09:19 PM
What I got was
Notice: Undefined variable: _COOKIES in C:\Inetpub\wwwroot ....

NightShift58
02-03-2007, 09:21 PM
I'm sorry... Should be $_COOKIE - no "S" at the end...

itbeings
02-03-2007, 09:23 PM
there was an error
with <?php
print "<pre>";
print_r($_COOKIE);
print "</pre>";
exit;
?>
I got
Array
(
[switchmenu] => sub2
[PHPSESSID] => n5n06p981e3q5bskkmqqpjb9g4
)

which mean the the cookie I set does not exit and this script below is working
if(isset($_REQUEST['cookie_name']) && $_COOKIE['cookie_name']=="on"){
include "enabled.inc.php";
}else{
include "disabled.inc.php";
}

itbeings
02-03-2007, 09:35 PM
On checking the cookie manually with firefox, it is present and set to on.

NightShift58
02-03-2007, 09:35 PM
Are you on a Unix or Windows server?

itbeings
02-03-2007, 09:36 PM
Windows server

NightShift58
02-03-2007, 09:37 PM
Read this: http://www.php.net/manual/en/function.setcookie.php#50957

itbeings
02-03-2007, 09:37 PM
Windows server 2003 SE

itbeings
02-03-2007, 09:39 PM
Is there anything different there to what I've done?

NightShift58
02-03-2007, 09:42 PM
The order is diefferent: header before set cookie. But the writer says that it doesn't work on IIS 5. Is that what you're using?

itbeings
02-03-2007, 09:43 PM
am using 6.0

NightShift58
02-03-2007, 09:46 PM
The the only thing I can think of is to put header() before setcookie() so that the headers sent out are in the right order. If that doesn't work, I'm in above my head.

Sessions could be a simpler alternative, if your probject isn't too far along...

itbeings
02-03-2007, 09:46 PM
Anyway I have another question which I think you could help me with at the moment. If a pagination returns 50 pages from my limitation, how can I make five number show in the for($i = 1; $i <= $num_pages; $i++){
if($pg == $i){
echo "[$i] ";
} else {
echo "<a href=\"".$qstring."&_pg=$i\">$i</a> ";
}
}
instead of all the 50 page numbers?

NightShift58
02-03-2007, 09:50 PM
Which five numbers? 10,20,30... or 1,2,3,4,5?

itbeings
02-03-2007, 09:53 PM
two before current page and two after it as in
if the current page is 13 I want
11 12 [13] 14 15

NightShift58
02-03-2007, 10:01 PM
Heres on way:$thisPAGE = $_GET['_pg'];
$prev1PAGE = $thisPAGE-1 > 0 ? $thisPAGE-1 : "";
$prev2PAGE = $thisPAGE-2 > 0 ? $thisPAGE-2 : "";
$next1PAGE = $thisPAGE+1 <=50 ? $thisPAGE+1 : "";
$next2PAGE = $thisPAGE+2 <=50 ? $thisPAGE+2 : "";

if ($prev2Page <> "") {
echo "<a href=\"".$qstring."&_pg=$prev2Page\">$prev2Page</a> ";
}
if ($prev1Page <> "") {
echo "<a href=\"".$qstring."&_pg=$prev1Page\">$prev1Page</a> ";
}
echo "[$thisPAGE]";
}
if ($next1Page <> "") {
echo "<a href=\"".$qstring."&_pg=$next1Page\">$next1Page</a> ";
}
if ($next2Page <> "") {
echo "<a href=\"".$qstring."&_pg=$next2Page\">$next2Page</a> ";
}

itbeings
02-03-2007, 10:11 PM
I had these errors
Notice: Undefined variable: prev2Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 226

Notice: Undefined variable: prev1Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 229
[2]
Notice: Undefined variable: next1Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 234

Notice: Undefined variable: next2Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 237

Notice: Undefined variable: prev2Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 226

Notice: Undefined variable: prev1Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 229
[2]
Notice: Undefined variable: next1Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 234

Notice: Undefined variable: next2Page in C:\Inetpub\wwwroot\int-url\Administrator\urls.view.inc.php on line 237

Why are they Undefined when they are properly declared

NightShift58
02-03-2007, 10:21 PM
Sorry... the effects of 1 typo with many Copy&Paste...

They should be called $prev1PAGE, $prev2PAGE, $next1PAGE and $next2PAGE...

PAGE all caps.

itbeings
02-04-2007, 03:39 AM
Ok thanks

itbeings
02-04-2007, 04:33 AM
I just solved the cookie headache by set a part for the cookie. I noticed from firefox that the cookie path is only for the path I am and it subdirectories, whereas I want to use it ina durectory prior the set path and it works.
Thanks for all the help.

itbeings
02-04-2007, 04:38 AM
//Set cookies if required
if(!empty($_REQUEST['_a'])){
if($_REQUEST['_a'] == "_enable"){
header("Refresh: 0; URL=./");
$value = 'on';
setcookie("cookie_name", $value, time()+360000000000000, "/");
}elseif($_REQUEST['_a'] == "_disable"){
header("Refresh: 0; URL=./");
$value = 'off';
setcookie("cookie_name", $value, time()-42000, "/");
}
}

NightShift58
02-04-2007, 07:07 AM
That I knew but there was no indication in your code that you were changing paths.

You may want to set the cookie to the domain name because I think that it offers better security, if that should be a concern.