Redirect in PHP Codeigniter

11,977

Using the redirect() method redirects to a URL. You therefore need to pass it a full URL (as it uses the header() function which according to the RFC for HTTP1.1 requires a full URL.

This means that you can use

redirect(site_url('home_invite'));

Which will redirect your user to http://www.yoursite.com/home_invite

This means that you must have a controller called home_invite available as you can't load a view from the URL. Equally you could create a method in your existing controller and use the routes.php file to masquerade /your_controller/home_invite as /home_invite

The site_url() function is also part of the URL helper you've already included to use redirect().

If you don't want to use site_url(), you could just as well hard code the URL in like

redirect('http://www.yoursite.com/home_invite');
Share:
11,977
Alexandre Bolduc
Author by

Alexandre Bolduc

I am computer science engineering student from Canada. I have a lot of interest in the QA Sector of the IT industry, but I do enjoy and practice PHP,ASP.NET,HTML,CSS,C/C++,C#,FLASH,BBCODE,JAVA,ASSEMBLY CODE. I have a great passion in Game Design as well as Philosophy.

Updated on June 04, 2022

Comments

  • Alexandre Bolduc
    Alexandre Bolduc almost 2 years

    I have some problems using Code Igniter and I feel there is something I don't understand because I can't get my redirects and my headers to work. Here is the situation :

    When site is entered, the default "home" controller is called.

        public function initialize()
    {
        printf("CONSTRUCTION OF HOME CONTROLLER - \n");
         //   print_r($_SESSION);
            //TODO : CONSIDER CREATING A LIBRARY TO AVOID WRITING THIS OFTEN. NOT
    
        // SESSION TROLLING DETECTION
            if( isset($_SESSION['banana']))
            {
                echo "SPLITTING THE TRUTH";
            }   
    
       // GETTING AS SERIOUS AS GREG     
            if( !isset($_SESSION['username']))
            {
                printf("USERNAME IS NOT SET. SETTING UP THE LOGIN PAGE. \n");
                redirect('home_invite');
            }
            else
            {
                $this->load->view('welcome_message');
            }
    }
    
    public function index()
    {
        //INITIALIZING THE PATH USED FOR THIS NAVIGATION
        printf("TROLLING THE BEGINNING OF THIS CONTROLLER HOME - ");
         $this->initialize();
         printf("TROLLING THE END OF THIS CONTROLLER  HOME - ");
       //TODO : CONSIDER CREATING A LIBRARY TO AVOID WRITING THIS OFTEN
    }
    

    Index calls initialize who verify if the user has already a session variable with username in it. If that's the case, we would proceed to check his level of privileges, etc, and load corresponding view. Thats not the problem.

    If the session is not started, I want to load the "login" view, called here "home_invite". And I want to redirect him to that page. But if I use this code, the page will show a 404 error.

    If I use $this->load->view('home_invite'), it works, but I don't understand and I feel it isn't what I want it to do.

    Why is redirect not working in this context?

  • Alexandre Bolduc
    Alexandre Bolduc over 12 years
    Thanks, the tutorials I have been following did not use the site_url() function.