Logout Link not working - codeigniter

So I am trying to click a logout link and end the session, and it doesnt seem to be working.
Link is located here:
application -> views-> modules -> header.php
like looks like this:
Logout
in the admin controller I have the function:
public function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('admin', 'refresh');
}
My routes files for admin look like this:
$route['admin/(:any)'] = 'admin/view/$1';
$route['admin'] = 'admin';
Admin views folder is located: views -> admin
Question: When I click on my logout link it does nothing. Like in my admin if there is no session it gets redirected to a login screen. That works but when I log out if it was redirecting to the admin page which is suppose to redirect to the login page if there is no session why is nothing happening?

Why are you redirecting to admin on logout? Just out of curiosity.
Try this, it sounds like your link itself is the issue. Using the base_url() means you don't have to worry about relative urls getting messed up depending on the depth you are in the site.
Logout

$route['admin/(:any)'] = 'admin/view/$1';
This code is redirecting admin/logout to admin/view/logout
So, like you said, adding $route['admin/logout'] = 'admin/logout'; near the top of your routes should take you to the correct page

Related

ask login page when user opens dashboard page directly through URL in CODEIGNITER

I have a dashboard page..if anyone opens dashboard link directly through URL system dosen't allow them to open that page..
when they open page directly through URL system should ask LOGIN..
please help me how to do this in codeigniter..
All of controller check session data available or not if session are not store then redirect to your login screen.
put if condition to your __construct function and check it.
function __construct()
{
parent::__construct();
if(! isset($this->session->userdata['user_info'])){
redirect($this->data['admin_url'].'authentication');
exit ;
}
}

how redirect back after login and logout - laravel 5

Now after login or logout I am forwarded to the homepage.
in this article have read that
I can define redirection path in Auth Controller
protected $redirectPath = '/';
I can also change this line in Middleware RedirectIfAuthenticated.php:
return new RedirectResponse(url('/'));
But my goal is to
either login by AJAX
or, after login or logout, redirect back to the page where I have pressed the button.
Both answers highly appreciated.
Thx.
To redirect to the previous page you can use back() like so:
return redirect()->back();
You can read more in the Laravel Docs.
How about using something like this ?
if(Session::get('redirect') == null) {
Session::flash('redirect', 'ok');
return redirect()->back();
}
For anyone who have to redirect user back to the page where they clicked the button for a protected page that redirected back to login. Here is my simple approach to it.
First on your nav, menu or whatever, your href value would be something like this:
Some Page
What this simply do is, if your user is not logged in, its uses the lgin page as url, which simply cut off the need to redirect and adds the current page they are in which would be the page they would redirected back after login as a get param to the login url. Unlike using sessions to retrieve http_referer which would be wrong if user were redirected back to login due to validation error, this would stick around on your url.
Example of login url may be http://localhost:2008/signin?redirect_to=protected-page
Then simply after login you would just check if get param redirected_to is available then redirect user back to that page or a default page if its not available as a get a param, probably your home or dashboard maybe, an example below might help.
$data = $request->all();
$referer = $data['redirect_to'] ? url($data['redirect_to']) : url('/');
if($allowAccess) return redirect($referer);
I hope this helps someone!

How to redirect the user to another page after they have logged in with Ion Auth CodeIgniter

I have been working on admin panel of my project. I use Ion_Auth. I have a problem about redirecting after I have logged in on the system.
If I change default_controller as "auth", everything is okey and after I logged in, I have been redirecting to the user lists. But I have already a default_controller as "Home" and I don't want to change it as "Auth". If I change it as "Auth", login screen is shown first. If I use home as default_controller, when I have logged in on the system, Home page is shown instead of the page of user lists.
So I guess I need two default controllers one of them for my home page and also the other one for ion auth.
Any idea about the solution?
In auth.php change the redirection route if user log in. That would be line 67 I guess. You need to play with ion_auth to set it fits your (application) needs.
I like to make parent controller in application/core that has checking function in constructor. All controllers that requires login part should extend that sort of controller. Google for phil sturgeon extending of MY_Controller.php to see what am I talking about.

Codeigniter redirect to variable url

I'm working on a project in codeigniter. What I have done until now is that if you type for instance
http://localhost/ci_project/dashboard
it will redirect to a page called: restricted.
However, what I want to do is that when you type dashboard on the url, codeigniter will redirect to login controller. If the login is successfull, go to the dashboard or whatever controller that the user added into the URL.
Basically the idea is to get a variable from the URL given by the user.
Thanks a bunch!
You can use the redirect function to send your users to any page you like:
redirect($URL, 'refresh');
You can read more about this function here (at the bottom of the page:)
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
Place an if statement in the target controller function (I'm assuming the index() function of the Dashboard class) to test if the user is logged in, then redirect as needed.

How to stay on same page after login in php?

I want that even after a user presses login button he stays on the same page. My login form is located at the header and is common for all. Like suppose there are two pages home and contact. User visits home page first. Later he visits contact page. Now even if the user presses the login button in the header, whatever he might have entered in the fields doesn't matter, validation occurs and the user either logs in and stays on contact page or an error message is displayed and the user stays on the contact page. I am using codeigniter framework. Is there anything that I can use in codeigniter?
After authenticating the user, do a redirect header to $_SERVER['HTTP_REFERER']
Hi Try to use $_SERVER['HTTP_REFERER'] for geeting the previous url.
$refering_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '' ;
put this code in your login view page for getting the previous page url
$this->session->set_userdata('url', $refering_url);
Store that url in session,so that it can be accessed in login controllers.Get the session
value from the conttroller and after checking login credentials ,redirect to this url
$url=$this->session->userdata('url');
For redirecting use redirect()
redirect($url, 'refresh');
Hope it helps
After authenticating the user you can redirect him to page of your choice
redirect(base_url(), 'refresh');
You can also sent error or any messages usign for example, you will need to load session library of course.
$this->session->set_flashdata('message', '<div class="message">' . $error . '</div>');
try this,
$this->agent->referrer();
before use this you have to load library
$this->load->library('user_agent'); in your parent class.
$this->load->library('user_agent'); //in your parent class.
redirect($_SERVER['HTTP_REFERER'], 'refresh'); //in function end redirection

Resources