我正在使用cookie工作。我的客户希望当一个访问者访问他的网站,它会被自动检查,他访问过这个网站之前。如果他以前访问过这个网站,他将被自动重定向到他以前访问过的网站。如果他以前没有访问过这个网站,cookie将被保存,如果他将来访问这个网站,他将被重定向到他最近访问过的页面。例如,一个网站有许多类别或主题,如食物,衣服等。如果访问者以布料主题或类别访问本网站,cookie将被保存。下次他访问这个网站时,他将被自动重定向布料类别页面,因为在过去,他访问过那个页面。但这一次,在页脚有一个选项可以再次保存cookie,如果他点击接受cookie, cookie就会被保存更新。现在我正试图在localhost中做到这一点并共享它的文件。请检查出什么问题或在哪里。这里我使用重定向选项在header.php和面对的问题。如果我使用重定向选项在index.php,错误发生(见截图)。如果我在header.php中使用,每个页面都会出现错误。见截图:http://prntscr.com/cbkux6您可以访问以下站点获取示例:http://www.louisvuitton.com/
my header page
<?php
if(isset($_COOKIE['saveexperice'])){
$link = $_COOKIE['saveexperice'];
header("Location: $link");
exit;
}
else{
header('Location: http://localhost/cookie');
$exlink = $_SERVER['PHP_SELF'];
setcookie('saveexperice', $exlink, time()+60*60*24*30);
exit;
}
我的索引页
<?php
include("header.php");
//$page_link = $_SERVER['PHP_SELF'];
echo "Index page";
//echo $page_link;
print_r($_COOKIE);
include("footer.php");
页脚页
<a href="clearcooke.php">Logout</a>
另一个页面
<?php
include("header.php");
print_r($_COOKIE);
echo "Ex php page";
include("footer.php");
cookie clear page
<?php
$exlink = $_SERVER['PHP_SELF'];
setcookie('saveexperice', $exlink, time()-60*60*24*30);
header('location: index.php');
另一个页面
<?php
include("header.php");
print_r($_COOKIE);
echo "CK php page";
include("footer.php");
你可以下载完整的项目zip文件
http://www116.zippyshare.com/d/6Gz32nO0/2541183/Coockie.zip正如我在评论中提到的,以防我不清楚,您没有任何停止代码,所以无论哪种方式,您都将重定向。没有不重定向的条件:
<?php
// Move here
$exlink = $_SERVER['PHP_SELF'];
if(isset($_COOKIE['saveexperice'])){
$link = $_COOKIE['saveexperice'];
// If your cookie doesn't match where you are now
if($exlink != $link) {
// Redirect
// NOTE: You may want to check the timestamp and only redirect
// if the cookie is X-amount of minutes old otherwise you
// will probably be stuck in another loop, always pushing you
// to the same page.
// If it's within the timeframe to not push to another page,
// then you have to reset the cookie to the current page.
header("Location: {$link}");
exit;
}
}
else{
setcookie('saveexperice', $exlink, time()+60*60*24*30);
// I am not sure why you need to redirect here since you are on a page
// you supposedly want to be on
header('Location: http://localhost/cookie');
exit;
}
编辑:好吧,既然你不能得到我的编辑工作,我已经添加了另一层,以增加一些人类可读的方法,使代码更容易理解。IT是一个可以快速构建的类,但是所有的方法都是不言自明的。它是我使用的部分(一般来说):
<?php
# I am calling it Session, but that is because it would have both cookie and session methods
class Session
{
private $expireTime,
$cookieName;
/*
** @description This will set the time for the cookie to expire
*/
public function setTime($time)
{
$this->expireTime = $time;
return $this;
}
/*
** @description Returns the name of the last cookie used in the instance
*/
public function getName()
{
return $this->cookieName;
}
/*
** @description This will set the name of the cookie
*/
public function setName($name = false)
{
$this->cookieName = $name;
return $this;
}
/*
** @description This actually creates the cookie
*/
public function setCookie($val, $name = false)
{
if(!empty($name))
$this->setName($name);
if(empty($this->cookieName))
return false;
$this->expireTime = (!empty($this->expireTime))? $this->expireTime : (time()+60*60*24*30);
setcookie($this->cookieName,json_encode(array($this->expireTime,$val)),$this->expireTime);
}
/*
** @description Self-explanatory
*/
public function destroyCookie($name = false)
{
if(!empty($name))
$this->setName($name);
if($this->cookieExists($this->cookieName))
setcookie($this->cookieName,null,(time()-1000));
}
/*
** @description Self-explanatory
*/
public function cookieExists($name = false)
{
if(!empty($name))
$this->setName($name);
return (isset($_COOKIE[$this->cookieName]));
}
/*
** @description Self-explanatory
*/
public function getCookie($name = false)
{
$cookie = $this->getCookieData($name);
return (!empty($cookie[1]))? $cookie[1] : $cookie;
}
/*
** @description This will get an array of the value and expire time
*/
public function getCookieData($name = false)
{
if(!empty($name))
$this->setName($name);
return (!empty($_COOKIE[$this->cookieName]))? json_decode($_COOKIE[$this->cookieName],true) : false;
}
/*
** @description Checks if the cookie is expired
*/
public function isExpired($name = false)
{
$cookie = $this->getCookieData($name);
if(!empty($cookie[0]))
return false;
return true;
}
/*
** @description Gives an array for a countdown of sorts
*/
public function willExpire($name = false)
{
$cookie = $this->getCookieData($name);
$now = strtotime("now");
if(!empty($cookie[0])) {
$seconds = ($now - $cookie[0]);
return array(
'h'=>trim(number_format(($seconds/60/60),0),'-'),
'm'=>trim(number_format(($seconds/60),0),'-'),
's'=>trim($seconds,'-')
);
}
return true;
}
/*
** @description Resets the expire time on the cookie
*/
public function extendTime($time,$name=false)
{
$cookie = $this->getCookieData($name);
$this->setTime($time)->setCookie($cookie[1]);
}
}
使用:<?php
# Add the class
require_once(__DIR__.'/Session.php');
# Create instance
$cEngine = new Session();
# Check if the cookie exists already
if(!$cEngine->cookieExists('saveexperice')) {
# If it doesn't exist, create it by
# First setting an expire time
$cEngine->setTime(strtotime('now + 20 minutes'))
# Add the data
->setCookie($_SERVER['PHP_SELF'],'saveexperice');
}
# This would just echo the value
echo $cEngine->getCookie();
# This will tell you when it will expire (count down)
print_r($cEngine->willExpire());
# This will extend the expiration time on the cookie
$cEngine->extendTime(strtotime("now + 1 day"));
# This will destroy the cookie
# $cEngine->destroyCookie();