Jump to content

Strange cookie behaviour


bonacipher

Recommended Posts

I have a cookie called 'site' which is set during one of my pages: newPhotos.php. It is reset at the top of index.php using setcookie('site');

 

However if I refrest newPhotos.php, it seems to reset the cookie. I found that commenting out the reset on index.php resolves this behaviour.

 

Similarly, if I put an echo "HERE"; statement before setcookie('site'); on index.php, it does not reset the cookie when I refresh newPhotos.php (because the output means I cannot modify header information.) Fair enough, the only reason that this is of interest is that HERE is not displayed, nor is a warning that I "Cannot modify header information - headers already sent by ".... blah blah

 

So the big question is, how come this header information on index.php is being read, and yet index.php itself is not? I am not including index.php at any point. Very confused, any help much appreciated.

 

??? ??? ???

Link to comment
https://forums.phpfreaks.com/topic/42585-strange-cookie-behaviour/
Share on other sites

I don't think posting code will help, but here it is. I realise that setcookie needs to be before any html output, that is not the problem. The problem is that index.php header information appears to be being called from within an entirely different page, and yet index.php is not included in that page. Thanks in advance for your help.

 

index.php:

 

<?php

setcookie('site');

 

....etc.....

 

?>

 

newPhotos.php (where the cookie gets set):

 

$site = '';

if(isset($_GET['site'])){

$site = $_GET['site'];

}else if(isset($_COOKIE['site']) && $_COOKIE['site'] != ''){

$site = $_COOKIE['site'];

}

 

if($site == 'parkTour2006'){

setcookie('site','parkTour2006');

include_once('./rp/header.php');

}else{

include_once('./includes/header.php');

}

 

newPhotosView.php (where the cookie gets read)

 

<?

if(isset($_COOKIE['site']) && $_COOKIE['site'] != ''){

setcookie('site','parkTour2006');

$site = $_COOKIE['site'];

if($site == 'parkTour2006'){

include_once('./rp/header.php');

}

}else{

    include_once('./includes/header.php');

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.