0

I use a cookies values into a project to check the visitors entered, firstly, a submit post form exist to enter an email address, after submit, the cookies is set (for next navigation to the current page, should be enter direct to the home page not to the form page if cookies is set), everything goes right, the cookies is set properly , but after navigation to another page the data is lost and no cookies exist!!! is right that cookies should be valid in all pages navigation?? the below figure some codes...

if((!$_COOKIE["tickets"] && $_COOKIE["email_address"]) || (empty($_COOKIE['tickets']) && empty($_COOKIE['email_address']))){
$access_vcount=intval(getCurrentValueAccess())+1;
if(getCurrentValueAccess()==0){$query_vcounts=mysql_query("insert into visitors_counter(counter) VALUES('$access_vcount')");}
else{$query_vcounts=mysql_query("update `visitors_counter` set counter='".$access_vcount."'");}
setcookie("tickets", "accessible", time()+86400);
setcookie("email_address", $email_address, time()+86400);
}
header("Location:".BURL."/".getflink(2,'en'));  

The cookies are valid for a one day (should be that). When i try to print the values is cookies in another page , then no result.

Thank you for your suggestions.

4

2 回答 2

0

在重定向的页面上设置 cookie 时,必须在调用 header('Location: ....'); 之后设置 cookie;

如:

<?php
  header('Location: http://www.example.com/'); 
  setcookie('asite', $site, time()+60*60, '/', 'site.com');  ?>

从http://de3.php.net/manual/en/function.setcookie.php的评论中粘贴

于 2012-07-25T08:06:58.837 回答
0

在语法中

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

必须正确设置路径,因为 cookie 仅对其下的路径和目录有效。

文档中,

cookie 可用的服务器上的路径。如果设置为“/”,cookie 将在整个域中可用。如果设置为 '/foo/',则 cookie 将仅在 /foo/ 目录和域的 /foo/bar/ 等所有子目录中可用。默认值是设置 cookie 的当前目录。

于 2012-07-25T07:53:29.770 回答