sajeesh
sajeesh

Reputation: 21

Logout on Codeigniter

I am working on Codeigniter and issue is unable to logout. Below is my codes. Controller 'Login' has a function for logout.

function logout(){
//session destroy
}

View is login_view has a link with href

<a href="logout">Logout</a>

Added routes as below

$route['logout'] = 'Login/logout';

However when clicking on the link it redirects to the link http://localhost:8080/po/logout (po is my directory)

Please help, thanks in advance.

Upvotes: 0

Views: 1449

Answers (5)

Ravi Thanki
Ravi Thanki

Reputation: 289

Controller Function

function logout()
	{
		$this->session->sess_destroy();
		redirect('user');
	}

Menu Link
<li><a href="<?php echo base_url(); ?>user/logout">Logout</a></li>

Upvotes: 0

Ryuk Lee
Ryuk Lee

Reputation: 744

Please try this

<a href="<?= base_url()?>logout">Logout</a>

Add this to .htaccess to remove index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Upvotes: 0

Rishi
Rishi

Reputation: 483

Change your base_url as this

 $config['base_url'] = 'http://localhost/projectName/';

make a new file and paste the below code and save it as .htaccess in your root directory

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

This will remove the index.php comming in the url

change the anchor tag as following

<a href="<?= base_url('controller/function') ?>">Logout</a>

Upvotes: 2

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

There is wrong configuration. Fix it

$config['base_url'] should be

$config['base_url'] = 'http://localhost:8080/po/';

And link should be

<a href="<?php echo site_url('logout');?>">Logout</a>

site_url() will take your configuration better than base_url(). base_url only should use in asset link like CSS,JS etc.

Upvotes: 0

Del
Del

Reputation: 162

Make sure that in your base_url variable is defined in the config.php. Then try this code below.

<a href="<?php echo base_url . 'logout'; ?>">Logout</a>

Upvotes: 0

Related Questions