0
votes

browser showing to many redirection when connect 'base_url'.

Example, when I enter "www.xxx.com" , then codeigniter showing error!( to many redirection)

but I enter "www.xxx.com/login", showing login controller page! what happen to me..? I want to see default controller. but this doesn't working...

this is my codeigniter config source.

    $config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $config['base_url'] .= "://" . $_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);

end this is my codeigniter route source.

    $route['default_controller'] = 'login';
    $route['404_override'] = 'error404';
    $route['translate_uri_dashes'] = FALSE;
    $route['assets/(:any)'] = 'assets/$1';

end, this is my codeigniter Login controller source.

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Login extends CI_Controller
    {
        private $menu = "login";
        function __construct()
        {
            parent::__construct();
        }

        function index()
        {
            $data['menu'] = $this->menu;
            $this->load->view('include/header', $data);
            $this->load->view('login/login');
            $this->load->view('include/footer');
        }
        function register()
        {
            $data['menu'] = $this->menu;
            $this->load->view('include/header', $data);
            $this->load->view('login/register');
            $this->load->view('include/footer');
        }

    }

last this is my .htaccess source.

    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteCond $1 !^(index\.php|images|assets|captcha|data|include|uploads|robots\.txt)
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ /index.php/$1 [L]
    </IfModule>

I don't understand why default_controller doesn't working! What refers to I do?

1
I don't know what you think you're doing when you set the base_url, but simplifying that would be where I would start. As it is now, what is your base_url when you debug? - Brian Gottier
What exact error is? Paste here - Artier
Instead of using $_SERVER['HTTP_HOST'] to dynamically assign base_url() I strongly suggest using the approach documented HERE There are security concerns using $_SERVER['HTTP_HOST'] - DFriend
The easier way to build the URL is $protocol = (is_https() === TRUE) ? 'https://' : 'http://'; then $config['base_url'] = $protocol."example.com".'/'; Yes, hard code the domain. See my previous comment as to how/why to be ENVIRONMENT specific. - DFriend

1 Answers

0
votes

I'm really sorry. All problem start at 'hooks'! I recommended "Auth"hook. But, when I changing my domain, I'm not edit my hook. When edit domain, everything works well.! Thanks for your comment. It's really advise to me.

here is my 'Auth.php' < hook!

<?php
class Auth {
    function check_login() {
    $CI =& get_instance();
    isset($CI->session) OR $CI->load->library('session');

    $CI->session->has_userdata('user_name') OR
    $CI->session->set_userdata('user_name', 'guest');

    $username = $CI->session->userdata('user_name');

    if(!(strpos(current_url(), "authentication")) && 
       !(current_url() === "http://my_domain.com") && 
       !(current_url() === "http://my_domain.com/"))
    { 
       if($username==='guest' && 
          !(isset($CI->allowed_method) && 
           in_array($CI->router->method, $CI->allowed_method)))
       {
            redirect("/");
       }
    }
 }
}
?>