Hello Guys,
I have a question, what will i do to save the last domain a user was on either on a cookie or a session or anythin else that will work so that whenever they type the main domain they are redirected to the sub domain they were on last i have about 3 sub domains and the main domain. for example
main.com <== TLD
sub.main.com <== Sub Domain 1
sub2.main.com <== Sub Domain 2

user types in main.com and was browsing sub.main.com yesterday and instantly gets taken to sub.main.com

Comments

agentrickard’s picture

Category: feature » support

Make sure you set $cookie_domain = '.main.com' first. The rest is just JS or PHP code to set/read/react to the cookie value.

If you fail to set $cookie_domain, the sites cannot read a shared cookie for security reasons.

patoh’s picture

Thanks agentrickard .. i have the $cookie_domain setup in the settings.php would you happen to have some code or an example of to do that redirect to url using the $cookie_domain to point me in the right direction?? id really appreciate it.

agentrickard’s picture

This should come close. The trick to to avoid an infinite redirect loop. You may also need to adjust the $time setting.

This code goes in a custom.module file and may be buggy.


function custom_init() {
  $_domain = domain_get_domain();
  $cookie = custom_get_my_cookie();

  // This part needs tweaking, it assumes the visitor has not been to the site
  // in the last 12 hours, therefore issue a redirect.
  $time = REQUEST_TIME - (12 * 60 * 60); // NOW, minus twelve hours.

  // If the cookie timestamp is longer than we like, then redirect to last
  // known domain. Note that this may not be the homepage, but will be the
  // current page request on the other domain.
  if (isset($cookie['custom_redirect']['time']) && $cookie['custom_redirect']['time'] < $time) {
    if ($domain = domain_machine_name_load($cookie['custom_redirect']['domain'])) {
      setrawcookie('custom_redirect', array());
      domain_goto($domain);
    }
  }
  // Now set the new cookie.
  $cookie = array(
    'domain' => $_domain['machine_name'],
    'time' => REQUEST_TIME,
  );
  setrawcookie('custom_redirect', $cookie);
}

function custom_get_my_cookie() {
  $cookie = isset($_COOKIE['custom_redirect']) ? $_COOKIE['custom_redirect'] : array();
  return $cookie;
}

You could also change the $time handling to be expire-based, but that likely won't work in your use case.

patoh’s picture

Thanx ken .. I'll be sure to try it out and report the output.

patoh’s picture

Hi. I Modded it a little bit because it was throwing some tantrums :) but it still doesnt redirect to last domain :(
Here's what i have currently

function ubercookie_init() {
  $_domain = domain_get_domain();
  $cookie = ubercookie_get_my_cookie();

  $time = REQUEST_TIME + (12 * 60 * 60); //minus 12 hrs frm now <>><> why?? because

//tuma me to the last damn domain i was freaking on..
  if (isset($cookie['ubercookie_redirect']['time']) && $cookie['ubercookie_redirect']['time'] < $time) {
    if ($domain = domain_machine_name_load($cookie['ubercookie_redirect']['domain'])) {
      setrawcookie('ubercookie_redirect', array());
      domain_goto($domain);
    }
  }
  // Now set me the new cookie. gimmie the cookie
  $cookie = array(
    'domain' => $_domain['machine_name'],
    'time' => REQUEST_TIME,
  );
  foreach($cookie as $key=>$value)
	{
	//setcookie($key, $value, null);
	setrawcookie('ubercookie_redirect', $value);
	}
//echo '<pre>'; 
//print_r($_COOKIE); 
//echo '</pre>';
}  

function ubercookie_get_my_cookie() {
  $cookie = isset($_COOKIE['ubercookie_redirect']) ? $_COOKIE['ubercookie_redirect'] : array();
  return $cookie;
}
agentrickard’s picture

What _does_ it do? What, if anything is set in $_COOKIE?

agentrickard’s picture

I think this part is wrong:

// Now set me the new cookie. gimmie the cookie
  $cookie = array(
    'domain' => $_domain['machine_name'],
    'time' => REQUEST_TIME,
  );
  foreach($cookie as $key=>$value)
    {
    //setcookie($key, $value, null);
    setrawcookie('ubercookie_redirect', $value);
    }

You are trying to access an array above this. If the cookie cannot be an array, then each cookie needs a unique name.

What does this set:


// Now set me the new cookie. gimmie the cookie
  $cookie = array(
    'domain' => $_domain['machine_name'],
    'time' => REQUEST_TIME,
  );

    setrawcookie('ubercookie_redirect', $cookie);

patoh’s picture

This is the exception it throws without the for each bit
Warning: setrawcookie() expects parameter 2 to be string, array given in ubercookie_init() (line 20 of /var/www/hivisasa/sites/all/modules/cookie/ubercookie.module).

This what the $_COOKIE Holds:

Array
(
[ubercookie_redirect] => 1342440988
[nomobi] => yes
[UUID] => 9f2cef0c-7d3f-1b64-8dff-e6cf4acc2800
[__utma] => 78302283.228827486.1342044306.1342053442.1342070418.3
[__utmc] => 78302283
[__utmz] => 78302283.1342044306.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
[SESSae96f6b4b32c957e33681ae8c7d5a4eb] => 4XqpXDoFVusowuZztNQngEv6U1iCOpm_BSsa9apTxc4
[has_js] => 1
[Drupal_toolbar_collapsed] => 0
)

agentrickard’s picture

Then we just have a string/array mismatch because I'm used to dealing with $_SESSION, which allows arrays.

<?php
function ubercookie_init() {
  $_domain = domain_get_domain();
  $cookie = ubercookie_get_my_cookie();

  $time = REQUEST_TIME + (12 * 60 * 60); //minus 12 hrs frm now <>><> why?? because

//tuma me to the last damn domain i was freaking on..
  if (!empty($cookie['ubercookie_redirect_time']) && $cookie['ubercookie_redirect_time'] < $time) {
    if ($domain = domain_machine_name_load($cookie['ubercookie_redirect_domain'])) {
      setrawcookie('ubercookie_redirect_time', '');
      domain_goto($domain);
    }
  }
  // Now set me the new cookie. gimmie the cookie
  $cookie = array(
    'domain' => $_domain['machine_name'],
    'time' => REQUEST_TIME,
  );
  foreach($cookie as $key=>$value)
    {
    //setcookie($key, $value, null);
    setrawcookie('ubercookie_redirect_' . $key, $value);
    }
  }
} 

/**
 * It may be easier to just read $_COOKIE[KEY] directly....
 */
function ubercookie_get_my_cookie() {
  $cookie = array(
    'ubercookie_redirect_time' => isset($_COOKIE['ubercookie_redirect_time']) ? $_COOKIE['ubercookie_redirect_time'] : '',
     'ubercookie_redirect_domain' => isset($_COOKIE['ubercookie_redirect_domain']) ? $_COOKIE['ubercookie_redirect_domain'] : '',
  );
  return $cookie;
}
patoh’s picture

I coded the changes and it now has this two extra lines at the end of the cookie
[ubercookie_redirect_domain] => kazijob_com
[ubercookie_redirect_time] => 1342460940
which means that the cookie is now storing the domain machine name which is uber awesome :)

Now to test it out on a test server!! cross ur fingers. I hope it takes me to my last domain.

agentrickard’s picture

It should work now. I just forgot that $COOKIE['this_cookie'] must be a string.

patoh’s picture

Huh! What do yu mean? the machine name wont work?? or i replace the machine name with the domain?

agentrickard’s picture

No. I mean that we tried to set $_COOKIE['ubercart_redirect'] = array('this', 'that');. You just can't do that, so we need two cookies, not one.

patoh’s picture

Still doesnt redirect. The cookie is now storing the information but its not redirecting to previous domain when i type the TLD. is there something am missing? Been lookin at the code am gettng nothin..

agentrickard’s picture

The redirect will only fire if your cookie is more than 12 hours old and your math is backwards ;-).

$time = REQUEST_TIME - (12 * 60 * 60); //minus 12 hrs frm now <>><> why?? because

You changed the code between comment 3 and comment 5.

patoh’s picture

so if i use - (12 * 60 * 60) that will mean if the cookie is less than 12 hrs old. do the redirect.. changing and testing now.

patoh’s picture

take a look at the url http://kazijob.com/ look at the array. still not working as expected. nakuru.kazijob.com stores the cookie but thats it. :(

agentrickard’s picture

NO minus means MORE than 12 hours old. $time here is a Unix timestamp, in seconds since 1970. Smaller values are in the past.

You want to force it to run, set it to 10 seconds and wait for an infinite loop:

$time = REQUEST_TIME - 10; // NOW minus 10 seconds.
patoh’s picture

Also.. i have noticed that the cookies are expiring at the end of the session hence a new one is created on page load and killed on browser close.. am thinking if we can give the cookie an expiry timestamp, it will be able to redirect. what do yu think?

agentrickard’s picture

Makes sense. But that's a programming issue outside my scope for support.

Feel free to post back when you finish working code.

patoh’s picture

i have goin round the code over the weekend and among the issue for the timestamp not working i also suspect the function ubercookie_get_my_cookie() is not being called to action. i tried to echo something from the function and it doesnt show anything. any insight on this? Thanx

agentrickard’s picture

Status: Active » Closed (works as designed)
mas0h’s picture

Any update on this? or a working code?