Joomla 2.5主页重定向问题

I want to set joomla site to redirect visiting users to registration page if my cookie is not set. I've put below code in top of the (after imports) template index.php file.

if (empty($_COOKIE["abc"])){   
    $app = &JFactory::getApplication();    
    $link = "index.php?option=com_users&view=registration";
    $app->redirect($link);
   exit();
}

URL is redirect but it is going in loop. so page not loading correctly. This is fire bug screen shot enter image description here

How to omit this loop ? Thanks.

Your problem because of your code. You put your code on top of page... your redirection to registration page is correctly but when page goes to registration page.. at that time this code also check because it is on top of index file.. so again redirect on registration page.

So, loop continue redirecting...then i think issue create.

Put condition on your code with cookies like below,

if (empty($_COOKIE["abc"]) && empty($_COOKIE['is_redirected']) && (new condition)){ 
  • new condition: above replace new condition with check your page is not login or register what you want.

Please check if the page you are redirecting to runs the piece of code you have mentioned in your question.

If so, you can use a placeholder cookie.

Here is how:

if (empty($_COOKIE["abc"]) && empty($_COOKIE['is_redirected'])){   
    $app = &JFactory::getApplication();    
    $link = "index.php?option=com_users&view=registration";
    setcookie("is_redirected", "true", time()+3600); // this will make sure that only one redirection happens
    $app->redirect($link);
   exit();
}

cause template index.php running on all pages and also on registration page so you need to set cookie values before redirect. so it will be set on register page and you not redirect again

if (empty($_COOKIE["abc"])){   
        $app = &JFactory::getApplication();    
        $link = "index.php?option=com_users&view=registration";
        setcookie("abc", "true", time()+3600);
        $app->redirect($link);
       exit();
    }