cookie在根目录下工作正常,但在子目录下工作不正常 [英] cookie is working fine in root directory but not working sub directory

查看:131
本文介绍了cookie在根目录下工作正常,但在子目录下工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望访问者访问我的网站时,它会检查此站点cookie之前是否已保存在他的浏览器中。如果之前没有保存,则将保存新的Cookie。然后,如果他将访问主页,则将重定向到保存在浏览器cookie中的页面。为此,我可以在任何页面中使用以下代码:

  $ exlink = http:// {$ _SERVER ['HTTP_HOST ']} {$ _ SERVER ['REQUEST_URI']}; 

if(!isset($ _ COOKIE ['saveexperice'])){
setcookie('saveexperice',$ exlink,time()+ 60 * 60 * 24 * 30,'/ ');
}

并在首页中使用以下代码:

  if($ _ COOKIE ['saveexperice']){
$ exlinkr = $ _COOKIE ['saveexperice'];
标头( Location:。$ exlinkr);
}

工作正常。但是问题在于它只能在根目录下使用。无法使用任何子目录,例如domain.com/subfolder。
访客访问时将在此处创建cookie,但是如果访问者访问主页,它将不会重定向子文件夹。如果我访问根目录的任何页面(例如domian.com/about.php),cookie都将保存并返回我的主页,它将我重定向到我保存的cookie页面,即domain.com/about.php。
我希望它需要在子文件夹页面中工作。
我该怎么做?

解决方案

每个人,我都有针对该问题的解决方案,如果需要,您可以查看github链接


设置cookie并使用cookie重定向上次访问链接


您可以在访问者/客户端浏览器上设置cookie,然后在访问者离线时再次设置回到您的网站,然后访问者/客户尝试访问者主页时会自动重定向上次访问链接。他/她无法访问首页自动重定向最新链接。清除/删除cookie后,他/她可以再次访问主页。


设置cookie
首先,您需要在浏览器上将链接设置为cookie。只是使用下面的代码。

  $ exlink ='http:// {$ _SERVER ['HTTP_HOST']} {$ _SERVER ['REQUEST_URI' ]}';; 
setcookie('saveexperice',$ exlink,time()+(60 * 60 * 24 * 30),‘/’); //将Cookie设置为30天

注意:必须使用php文件。像.php一样,如果您的文件是.html,则.css表示php无法正常工作(例如,对于新用户而言)。并且必须将该代码用于所有代码的标头中。示例,请参见屏幕快照


现在,我们将浏览器上的上次访问链接设置为cookie


现在,我们

  if(isset($ _ COOKIE ['last_visited_url'])){
$ redirect_url = $ _COOKIE ['last_visited_url'];
}

现在我们有了该URL,您可以将header()重定向到该URL

  header('Location:'。$ redirect_url); 

完整代码,例如

  if( isset($ _ COOKIE ['last_visited_url'])){
$ redirect_url = $ _COOKIE ['last_visited_url']; //从Cookie
标头中获取链接( Location:。$ redirect_url); //重定向cookie链接
}

注意:必须使用php文件。像.php一样,如果您的文件是.html,则.css表示php无法正常工作(例如,对于新用户而言)。并且必须将该代码用于所有代码的标头中。示例,请参见屏幕快照


只需为另一个页面设置cookie并放置重定向代码可以通过访问重定向哪个链接?像主页一样。


清除或删除Cookie的
现在,您可以使用该代码删除/清除Cookie

  $ cookieurl = $ _SERVER ['REQUEST_URI']; 

setcookie('last_visited_url',$ current_location,time()-60 * 60 * 24 * 30,‘/’); //删除Cookie时间

标头(位置:https://link.com); // inpur链接,该链接在清除cookie


更改Cookie链接

  $ current_location = $ _SERVER ['REQUEST_URI']; 

if(isset($ _ COOKIE [‘last_visited_url’])){
$ cookie_location = $ _COOKIE [’last_visited_url’];


if($ current_location!= $ cookie_location){

setcookie('last_visited_url',$ current_location,time()+ 60 * 60 * 24 * 30 ,'/');
}
} else {
setcookie(‘last_visited_url’,$ current_location,time()+ 60 * 60 * 24 * 30,‘/’);
}

在github上查看此处


I want when a visitor visit my site, then it will check that this site cookie is saved in his browser before. If doesn't it saved before, then new cookie will be saved. Then if he will visit home page, he will be redirected the page which was saved in his browser cookie. To do this, i use this code in any page:

$exlink =  "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

if(!isset($_COOKIE['saveexperice'])){
    setcookie('saveexperice', $exlink, time()+60*60*24*30, '/');
}

and use this code in home page:

if($_COOKIE['saveexperice']){
    $exlinkr = $_COOKIE['saveexperice'];
     header('Location: ' . $exlinkr);   
}

It's working fine. But problem is that it's only working on root directory. not working any sub directory such as domain.com/subfolder. Here cookie will be made when a visitor visit, But if he visit home page, It won't redirected the subfolder. If i visit root directory any page such as domian.com/about.php, cookie will be saved and return my home page, it's redirected me to my saved cookie page as domain.com/about.php. I want it's need to work in subfolder page. How can i do this?

解决方案

Everyone, I got solution for that issue, If you need you can check github link

Set cookie and redirect previous visit link using cookie

You can set cookie on visitor/client browser then when a visitor goes offline and again come back into your website then the visitor/client auto redirect last visit link when he tries visitor home page. He/she can't visit home page auto redirect latest link. He/she can again visit the home page after clear/delete the cookie.

Set Cookie First you need to set link on browse as a cookie. just using below code.

$exlink =  "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
setcookie('saveexperice', $exlink, time()+(60*60*24*30), '/'); // set cookie for 30 days

Note: Must you need php file. like .php, if your file is .html, .css that php is not work (exmple for new user). and must using that code into header of above all code. example see screenshot http://prntscr.com/i7y64e

Now we've set last visited link on browser as a cookie

Now we'll get that url from cookie.

if(isset($_COOKIE['last_visited_url'])){
    $redirect_url = $_COOKIE['last_visited_url'];       
}

Now we've got that url, and you can redirect header() to that url

header('Location: '. $redirect_url);

full code like

if(isset($_COOKIE['last_visited_url'])){
    $redirect_url = $_COOKIE['last_visited_url']; // get link from cookie
    header('Location: '. $redirect_url); // redirect cookie link
}

Note: Must you need php file. like .php, if your file is .html, .css that php is not work (exmple for new user). and must using that code into header of above all code. example see screenshot http://prntscr.com/i7y8km

just set cookie for another page and place redirect code Which link will be redirected by visiting? like home page.

Cookie clear/delete Now you can remove/clear your cookie using that code

$cookieurl =  $_SERVER['REQUEST_URI'];

setcookie('last_visited_url', $current_location, time()-60*60*24*30, '/'); // remove cookie time

header('location: https://link.com'); // inpur link which link redirect after clear cookie

Change Cookie link

$current_location = $_SERVER['REQUEST_URI'];

if(isset($_COOKIE['last_visited_url'])){
 $cookie_location = $_COOKIE['last_visited_url']; 


 if($current_location != $cookie_location){ 

    setcookie('last_visited_url', $current_location, time()+60*60*24*30, '/'); 
 } 
} else {
 setcookie('last_visited_url', $current_location, time()+60*60*24*30, '/'); 
} 

Check Here on github

这篇关于cookie在根目录下工作正常,但在子目录下工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆