如何基于cookie重定向用户,我面临许多使用php重定向的问题
我正在使用cookie。我的客户希望当访问者访问他的站点时,会自动检查他以前是否访问过此站点。如果他以前访问过此站点,他将自动重定向到他以前访问过的站点。如果他以前没有访问过此网站,cookie将被保存,如果他将来访问此网站,他将被重定向到上次访问的页面。例如,一个站点有许多类别或主题,如食物、布料等。如果访问者以布料主题或类别访问该站点,cookie将被保存。下次他访问这个网站时,他会自动重定向布类页面,因为在过去,他访问了那个页面。但这一次,页脚中的一个选项将再次保存cookie,若他单击接受cookie,cookie将被保存并更新。 现在我尝试在localhost中执行此操作并共享它的文件。请检查有什么问题或在哪里。在这里,我使用header.php中的重定向选项来解决这个问题。如果在index.php中使用重定向选项,则会发生错误(请参见屏幕截图)。如果我在header.php中使用,每个页面都会发生错误。请参见屏幕截图: 您可以访问此网站以获取示例: 我的首页如何基于cookie重定向用户,我面临许多使用php重定向的问题,php,redirect,cookies,header,Php,Redirect,Cookies,Header,我正在使用cookie。我的客户希望当访问者访问他的站点时,会自动检查他以前是否访问过此站点。如果他以前访问过此站点,他将自动重定向到他以前访问过的站点。如果他以前没有访问过此网站,cookie将被保存,如果他将来访问此网站,他将被重定向到上次访问的页面。例如,一个站点有许多类别或主题,如食物、布料等。如果访问者以布料主题或类别访问该站点,cookie将被保存。下次他访问这个网站时,他会自动重定向布类页面,因为在过去,他访问了那个页面。但这一次,页脚中的一个选项将再次保存cookie,若他单击接
<?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
// 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;
}
如果您在标题转发到的页面上,则标题无法匹配,因此它会继续重定向。你必须在标题中有一个if
,它与你所在的位置相匹配,如果你不在你应该在的位置,那么它会重定向你,如果你在正确的位置,它不会重定向。另一个页面代码是pastebin.com/yjFmt3eg home page pastebin.com/k7rh0jqu谢谢你的回答。我在我的本地主机上使用了这段代码,然后它就工作了,但当我在网站上使用这段代码时,就不在网站上工作了。Cookie不保存,我是在主体内容中回显Cookie,但不要回显它。我怎样才能解决它?你能帮助我吗?另一个页面代码主页好的,我添加了一个带有一些人类可读方法的类。希望这能让你的cookies设置更容易规划。我有一个新的帖子,需要与此相关的帮助,请你看看,你能帮我吗?您好,此cookie在子目录中不工作,我可以使用子目录吗?它在子域中可能有问题(制作cookie时您应该能够指定子域),但我不认为子目录在访问cookie时会出现问题
<a href="clearcooke.php">Logout</a>
<?php
include("header.php");
print_r($_COOKIE);
echo "Ex php page";
include("footer.php");
<?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");
<?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;
}
<?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();