Homepage is looping in Codeigniter

Hi, please help me., I am creating a login form. after putting several codes my page is always redirecting to my homepage. I can’t figure out what’s the problem. Here’s the file:

CONTROLLER

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

class Pages extends CI_Controller {
 function __construct()
 {
   parent::__construct();
 }
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function view( $page = 'home' ){
		
		if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')){
			show_404();
		}
		
		if( $this->session->userdata('validated') ) {
			$this->load->helper(array('form'));
			$this->load->helper('url');
			$loggedIn = $this->session->userdata('validated');
			$loggedUser = $this->session->userdata('username');
			$data['loggedUser'] = $loggedUser;
			$data['loggedIn'] = $loggedIn;
			$data['title'] = $page;
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/nav', $data);
			$this->load->view('pages/'.$page, $data);
			$this->load->view('templates/footer');
        } else {
            $this->show_login(false);
        }
	}

public function process($page = 'home' ){
	// Load the model
	$this->load->library('form_validation');
	$this->load->model('login_model');
	// Validate the user can login
	$result = $this->login_model->validate();
	// Now we verify the result
	if(! $result){
		// If user did not validate, then show them login page again
		$this->show_login(true);
		
	}else{
		// If user did validate, 
		// Send them to members area
		redirect('http://ospar.netau.net/');
	}

	
}

function show_login( $show_error = false, $pages = 'home' ) {

	if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')){
		show_404();
	}
	
    $this->load->helper('form');
	$this->load->helper(array('form'));
	$this->load->helper('url');
	$data['title'] = $page;
	$loggedIn = $this->session->userdata('validated');
	$loggedUser = $this->session->userdata('username');
	$data['loggedUser'] = $loggedUser;
	$data['loggedIn'] = $loggedIn;
    $data['error'] = $show_error;
	$this->load->view('templates/header', $data);
	$this->load->view('templates/nav', $data);
	$this->load->view('pages/'.$page, $data);
	$this->load->view('templates/footer');
}

public function do_logout(){
	$this->load->helper('url');
	$this->session->sess_destroy();
	redirect('http://ospar.netau.net/');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

VIEWS:

	<div class="containerfull">
	
		<div id="slideContainer">
			<div id="slider">
				<img src="<?php base_url(); ?>images/slide1.jpg" title="Ospital ng Parañaque" alt=""/>
				<img src="<?php base_url(); ?>images/slide2.jpg" title="Ospital ng Parañaque" alt=""/>
				<img src="<?php base_url(); ?>images/slide3.jpg" title="Ospital ng Parañaque" alt=""/>
			</div>
			<div class="shadowslider">
			</div>
		</div>

</div>

<div class="container-fluid">
<div id="content">
	<div class="container">
		<div class="row">
			<div class="col-md-9">
				<div class="content">
					<h1>Welcome to Ospar!</h1>
					<hr />
				

	There are several accounts as to how the City of Parañaque got its name. One such story stated that long ago, at the mouth of what is now known as the Parañaque River, there stood a balete tree that, when viewed from afar, looked like a majestic ship. This earned it the name Palanyag, taken from the term ‘palayag’, which means “point of navigation”. 
Another version of the story was that, before the Spanish colonizers came to the Philippines, there were some natives who lived close to the Manila Bay whose main livelihood was fishing or  paglalayag. On the other hand, the neighboring place from the east calledMuntinglupa  had rice farmers who were referred to as taga-Palayan. One day, the fishermen and rice farmers had a feast and many got drunk from tuba or fermented coconut wine. One farmer suggested that they should name the whole place Palayan as a sign of cooperation and good will between them. However, a fisherman protested and stated that they should name it Palalayag instead. As a compromise, they agreed to merge these two words and came up with Palalanyag. However, another drunken guest shouted, “Mabuhay ang Palanyag at ang mga taga-Palanyag!” The rest liked this word better and ever since then, the place was called Palanyag.
					</div>
					<hr />
					<div class="row">
					
						<div class="col-md-6">
						<div class="homeSvc">
						<h3>In patient Services</h3>
						<img src="<?php base_url(); ?>images/in.jpg" alt="In patient Services" width="300" height="300"/>
						<p>In-patient services include medical, surgical, paediatric, obstetric and rehabilitation services. Many hospitals also provide mental health services and services for older people.</p>
						</div>
						</div>
						
						<div class="col-md-6">
						<div class="homeSvc">
						<h3>Out patient Services</h3>
						<img src="<?php base_url(); ?>images/out.jpg" alt="Out patient Services" width="300" height="300"/>
						<p>Most hospitals provide out-patient services including emergency services, day procedures, diagnostic and assessment services, and therapy services. Hospitals also support various community based programs to prevent or reduce the need for hospitalisation.</p>
						</div>
						</div>
						
					</div>
				</div>
				<div class="col-md-3">
				<div class="sidebar">
					
					<?php if($loggedIn): ?>
					<?php echo '<div class="widgetBar">;' ?>
					<?php echo '<h3>Welcome!' . $loggedUser .'</h3>'; ?>
	<?php echo '<a class="btn" href="http://ospar.netau.net/pages/do_logout"><i class="icon-share"></i> Logout</a>';?>
	<?php echo '</div>'; ?>
<?php endif; ?>
					
					
					<div class="widgetBar">
						<div class="employeeLog"><h3><a href="<?php echo site_url('authorized/employee_login');?>">Employee Login</a></h3></div>
					</div>
					
					<div class="widgetBar">
						<div type="button" class="btn btn-lg patientLog" data-toggle="modal" data-target="#myModal"><h3>Patients Login</h3></div>

						<!-- Modal -->
						<div class="modal fade" id="myModal" role="dialog">
							<div class="modal-dialog">
							
							  <!-- Modal content-->
							  <div class="modal-content">
								<div class="modal-header">
								  <button type="button" class="close" data-dismiss="modal">&times;</button>
								  <h4 class="modal-title">Please fill in the form to view your accout</h4>
								</div>
								<div class="modal-body">
									<?php if (isset($error) && $error): ?>
										  <div class="alert alert-error">
											<a class="close" data-dismiss="alert" href="#">×</a>Incorrect Username or Password!
										  </div>
									<?php endif; ?>
									<?php echo form_open('pages/process'); ?>
										<div class="form-group">
											<label for="Username">Username</label>
											<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Username" name="username">
										</div>
										<div class="form-group">
											<label for="Password">Password</label>
											<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password">
										</div>
										<div class="checkbox">
											<label>
											  <input type="checkbox"> Remember Me
											</label>
										</div>
										<button type="submit" class="btn btn-default" value="Login">Submit</button>
									<?php echo form_close(); ?>
								</div>
								<div class="modal-footer">
									<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
								</div>
							  </div>
							  
							</div>
						</div>
						
					</div>
					
					<!--<div class="widgetBar">
						<h2>Account Login</h2>
						<form method="POST">
							<div class="form-group">
								<label for="exampleInputEmail1">Username</label>
								<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Username">
							</div>
							<div class="form-group">
								<label for="exampleInputPassword1">Password</label>
								<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
							</div>
							<div class="checkbox">
								<label>
								  <input type="checkbox"> Remember Me
								</label>
							</div>
							<button type="submit" class="btn btn-default">Submit</button>
						</form>
					</div>-->
					
					<div class="widgetBar">
						<h2>Service Overview</h2>
						<div class="sideSvc">
						<ul>
							<li><a href=""><i class="fa fa-caret-right"></i> Obgyne.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Orthopedic.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Pediatrician Surgeon.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Dermatology.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Pulmonology.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Neurologist.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> ENT.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Psychiatrist.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Ophthalmology.</a></li>
							<li><a href=""><i class="fa fa-caret-right"></i> Gastroenteritis.</a></li>
						</ul>
						</div>
					</div>
				  				
				</div>
			</div>
		</div><!-- row -->
	</div><!-- container -->
</div><!-- content -->
</div>

MODEL:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
 * Description: Login model class
 */
class Login_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	
	public function validate(){
		// grab user input
		$username = $this->security->xss_clean($this->input->post('username'));
		$password = $this->security->xss_clean($this->input->post('password'));
		
		// Prep the query
		$this->db->where('username', $username);
		$this->db->where('password', $password);
		
		// Run the query
		$query = $this->db->get('users');
		// Let's check if there are any results
		if($query->num_rows() == 1)
		{
			// If there is a user, then create session data
			$row = $query->row();
			$data = array(
					'id' => $row->id,
					'username' => $row->username,
					'validated' => true
					);
			$this->session->set_userdata($data);
			return true;
		}
		// If the previous process did not validate
		// then return false.
		return false;
	}
}
?>

and ROUTES

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|	example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|	http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
|	$route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|	$route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/

$route['default_controller'] = 'pages/view';
$route['authorized/(:any)'] = 'elogin/view/$1';
$route['(:any)'] = 'pages/view/$1';


$route['pages/process'] = 'pages/process';
$route['pages/do_logout'] = 'pages/do_logout';

$route['404_override'] = '';



/* End of file routes.php */
/* Location: ./application/config/routes.php */

I assume that this function could be the problem which is in the controller:

function show_login( $show_error = false, $pages = 'home' ) {

		if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')){
			show_404();
		}
		
        $this->load->helper('form');
		$this->load->helper(array('form'));
		$this->load->helper('url');
		$data['title'] = $page;
		$loggedIn = $this->session->userdata('validated');
		$loggedUser = $this->session->userdata('username');
		$data['loggedUser'] = $loggedUser;
		$data['loggedIn'] = $loggedIn;
        $data['error'] = $show_error;
		$this->load->view('templates/header', $data);
		$this->load->view('templates/nav', $data);
		$this->load->view('pages/'.$page, $data);
		$this->load->view('templates/footer');
    }

Thank you. :smile:

Okay, I think my problem is solved. I just did this in my controller:

if( $this->session->userdata('validated') ) {
			$this->load->helper(array('form'));
			$this->load->helper('url');
			$loggedIn = $this->session->userdata('validated');
			$loggedUser = $this->session->userdata('username');
			$data['loggedUser'] = $loggedUser;
			$data['loggedIn'] = $loggedIn;
			$data['title'] = $page;
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/nav', $data);
			$this->load->view('pages/'.$page, $data);
			$this->load->view('templates/footer');
        } else {
			
			$this->load->helper(array('form'));
			$this->load->helper('url');
			$loggedIn = $this->session->userdata('validated');
			$loggedUser = $this->session->userdata('username');
			$data['loggedUser'] = $loggedUser;
			$data['loggedIn'] = $loggedIn;
			$data['title'] = $page;
			$this->load->view('templates/header', $data);
			$this->load->view('templates/nav', $data);
			$this->load->view('pages/'.$page, $data);
			$this->load->view('templates/footer');
            //$this->show_login(false);
			
    }

I have another question. suppose I have a link like this:

http://ospar.netau.net/restricted/patientdashboard

How can I set a separate controller for thaT and declare in routes? Please help me. :disappointed_relieved:

Hoping someone can help me?

I’m sure someone will respond to you, but I think we’re in one of those time zone lulls at the moment. Europe should be beginning to wake up, but it’ll be another 5 hours or so until we see the US spring to life.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.