0

I have several domains pointed to the same php script. Upon visit I want to redirect the user do a random domain of those, but only one time. To achieve this, I set a session variable, redirect and check for that variable.

My code is this:


    session_start();
    if($_SESSION['seen'] != 1) {
    $_SESSION['seen'] = 1;
    header("Location: ".$randomurl);
}

So it should only redirect 1 time if the user has never seen the page because afterwards the session variable would contain 1 and it would not redirect.

However this is redirecting me about 5 times till it stops on a page and I can't explain why.

Does anybody have a clue?

Matthias Dunnowa
  • 119
  • 1
  • 1
  • 12
  • You can probably use a cookie for this. The user could just remove their session cookie if they wanted to see the random page again and it's just wasting a lookup on your server. – Bailey Parker Jun 16 '12 at 22:32
  • so you have this code on each domain or just on 1 entrypoint? If on each domain then it's pretty logical since every new domain wil not be able to read the session cookie placed for the calling domain and it will only stop when you redirect to a url on a domain you visited already. (since that'll have a current and valid session cookie) – Harald Brinkhof Jun 16 '12 at 22:44
  • 1
    Like Harald said, im pretty sure when setting a session it is per domain, unless it is sub-domains in which case you can set the value as ".domain". Maybe this will help http://stackoverflow.com/questions/1064243/php-sessions-across-sub-domains/1064278 or this .. http://stackoverflow.com/questions/1339984/cross-domain-php-sessions – fl3x7 Jun 17 '12 at 00:01

2 Answers2

2

try:

if(isset($_SESSION['seen']) && $_SESSION['seen']!= 1) {
....
}
mgraph
  • 15,238
  • 4
  • 41
  • 75
0
<?php

session_start();


if ( !isset( $_SESSION["valid_user"]) )

  { header("location:domain");
}
Asuquo12
  • 827
  • 17
  • 26
  • Where are you getting `$_SESSION["valid_user"]`? OP does not mention it. Also, you should always provide some sort of explanation/reasoning for your answer. Code-only answers are strongly discouraged. – Patrick Q Sep 09 '16 at 13:22