简体   繁体   English

仅重定向文件一次

[英]Redirect file only once

Is is possible to redirect to a file 2 times then for a third times it redirect to somewhere else 可以重定向到文件两次,然后第三次重定向到其他地方

EX:/index.php redirect to /index2.php then /index2.php redirect to /index.php.....,,,, then for the second times we visit /index.php it will redirect us to other file? 例如:/index.php重定向到/index2.php,然后/index2.php重定向到/index.php ..... ,,然后第二次访问/index.php它将把我们重定向到其他文件?

can we do this with javascript or PHP? 我们可以用JavaScript或PHP做到这一点吗?

index.php redirets to index2.php index.php重新发行到index2.php

index2.php redirects to index.php ?r=1 index2.php重定向到index.php ?r=1

in index.php check: 在index.php中检查:

if($_GET['r']==1){
    //redirect to other page
}

UPDATE 更新

As I understood you have this redirects: 据我了解,您有此重定向:

1. Somewhere => index.php
2. index.php => site.com
3. site.com  => index.php // and in this step you need to redirect to other place

So just add check: 因此,只需添加检查:

<script type="text/javascript">
if(document.referrer == 'site.com'){
    location.href = 'OTHER PAGE ADDRESS';
}
else{
    location.href = 'site.com';
}
</script>

So if user came from other site he will redirect to site.com if he from site.com when to other place. 因此,如果用户来自其他站点,则当他从site.com转到其他地方时,他将重定向到site.com。

Yes, You can add a counter in a session variable to track the redirection. 是的,您可以在会话变量中添加计数器以跟踪重定向。 After each redirect, increment the counter. 每次重定向后,增加计数器。

When the counter reach its limit, perform your desired action. 当计数器达到其极限时,请执行所需的操作。

Yes you can. 是的你可以。 The question is how permanent you want it. 问题是您想要多久。

JAVASCRIPT: JAVASCRIPT:

If you want to do it with javascript then take a look at window.location and to remember whether user was on the first page just use JavaScript cookies. 如果要使用javascript,请查看window.location并记住用户是否在第一页上,只需使用JavaScript cookie。 Simple tutorial http://www.tutorialspoint.com/javascript/javascript_cookies.htm You can also set an expiration. 简单教程http://www.tutorialspoint.com/javascript/javascript_cookies.htm您也可以设置有效期限。

PHP: PHP:

If you want to make it "less permanent" for example just for one opened browser window or something like that then look at PHP session management http://php.net/manual/en/features.sessions.php 如果要使它“不太持久”(例如仅用于一个打开的浏览器窗口或类似的窗口),请查看PHP会话管理http://php.net/manual/en/features.sessions.php

i have written the code for index.php?re_id = 我已经编写了index.php?re_id =的代码

mention re_id on each page. 在每个页面上提及re_id。 n you can give more conditions n您可以提供更多条件

 if($_GET['re_id']){

     $re_id = $_GET['re_id'];   
      if($re_id=="1")
      {
      header("location:index3.php");
      }
      else
      {
         header("location:index2.php");
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM