cton453
cton453

Reputation: 1

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?

can we do this with javascript or PHP?

Upvotes: 0

Views: 1105

Answers (4)

Narek
Narek

Reputation: 3823

index.php redirets to index2.php

index2.php redirects to index.php?r=1

in index.php check:

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.

Upvotes: 1

ajay
ajay

Reputation: 1570

i have written the code for index.php?re_id =

mention re_id on each page. n you can give more conditions

 if($_GET['re_id']){

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

Upvotes: 0

kuncajs
kuncajs

Reputation: 1098

Yes you can. The question is how permanent you want it.

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. Simple tutorial http://www.tutorialspoint.com/javascript/javascript_cookies.htm You can also set an expiration.

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

Upvotes: 0

Harpreet
Harpreet

Reputation: 719

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.

Upvotes: 0

Related Questions