here, home.php is one controller file and i want to redirect into another controller file login.php but this code is not working. if you have any idea then plz help me...

home.php

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

class Home extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->check_isvalidated();
    }

    public function index(){

        echo 'Congratulations, you are logged in.';

        echo "<br /><a href=''.base_url().'home/do_logout'>Logout</a>";
    }

    private function check_isvalidated(){
        if(! $this->session->userdata('validated')){
            redirect('login');
        }
    }

    public function do_logout(){
        $this->session->sess_destroy();
        redirect('login');
    }
}
?>

Dani AI

Generated

the logic is fine; the usual culprit is that the URL helper (which provides redirect() and site_url()) or the Session library is not loaded. Autoload them so redirects work everywhere and you can read userdata() in the constructor:

// application/config/autoload.php
$autoload['helper']    = array('url');      // redirect(), site_url(), anchor()
$autoload['libraries'] = array('session');  // $this->session

Your logout link is also being concatenated inside a quoted string, so base_url() never runs. Build controller/method links with site_url() or, simpler, the anchor() helper:

// inside Home::index()
echo 'Congratulations, you are logged in.';
echo '<br />' . anchor('home/do_logout', 'Logout'); // generates the correct URL

A few gotchas to check if it still does not redirect:

  • Do not prefix with a leading slash unless you intend an absolute path from the domain root. redirect('login') uses site_url('login') and respects index_page, subfolders, and URL rewriting. Using redirect('/login') can break when the app is in a subdirectory.
  • Make sure no output (even a space/BOM) is sent before headers. Omit the closing ?> in PHP files and remove any whitespace before <?php. Header warnings in logs indicate this issue.
  • Avoid redirect loops: if your Login controller also redirects when already authenticated, guard it so Login does not call the same check as Home.

Good call by on enabling logs; set $config['log_threshold'] = 4 and inspect application/logs/ for header or routing errors. And +1 to : explicitly loading the URL helper in the controller also works, but autoloading keeps things consistent across requests.

Recommended Answers

All 2 Replies

Hi,

is the URL helper automatically loaded? By enabling the log in CI you should be able to get more information about the error, just check into application/logs/. Try also to prefix the redirect link with a slash, for example:

redirect('/login');

Before redirecting, try $this->load->helper('url');. Also, try to do a redirect like so redirect('login','refresh');

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.