How to logout code igniter session without form - codeigniter

I have a login and logout view but I do not want a form to logout. How can I use a button to logout my session?

You can use a link because without form you cannot use button otherwise you have to use ajax
Logout
function logout() {
$this->session->sess_destroy();
redirect('controller_class/login');
}

You can target the controller logout method through html a tag, and in your controller method you can destroy the session of codeigniter then redirect to home page or login page.
Logout
public static function logout()
{
$this->session->sess_destroy();
redirect('controller/login');
}

Related

Authentication redirection in codeigniter

I am new to codeigniter framework. I am trying to build a authentication system.
Authentication is working fine. My problem is that after successfully logging, when I click the back button in the browser it is directed to login page again. I want to redirect it to the home page itself. i want to reload the home page not the index page(index page is the login page, after successful login goes to home page)
How can I do it
In your login page check that login session is started or not, if it is started than redirect to home page :
if(isset($_SESSION['user']))
{
header('Location:http://localhost/projectname/home');
}
If you have any confusion please let me inform.
Thanks
on the home page controller check whether the session exists or not. if it exists then redirect to home page otherwise login page.
In your home_model.php do something like below:
<?php
class Home_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->check_session();
}
function check_session()
{
$session_login = $this->session->userdata("MYLOGIN");
if($session_login == '')
{
redirect('home');
}
if($session_login == 1 && $session_login == TRUE)
{
redirect('home/dashboard');
}
}
}
Load home_model in home controller.

laravel redirection error after session out

i am using laravel 4 in my admin section its working fine in normal condition but when session is timeout and i refresh the page than its not redirect properly my admin login path is
localhost/project_name/public/admin/login
and when i logout its redirect properly but when automatic session out than its not go to to admin/login its redirect to the followin path
localhost/admin/login
so can any body tell me the solution for this
#Deepak Goyal,
define a before filter to check that in routes.php, something like:
Route::group(["before" => "auth"], function ()
{
//rest of the authenticated routes goes here
}
And in the filters.php
Route::filter("auth", function()
{
if (Auth::guest()) return Redirect::guest("admin/login");
});

what is the Best way to logout from page in codeigniter?

I make a link for logout in my view page.When I clicked it goes to another page but when I click the back button of browser it comes back to last page I log outed from it.what should I do to prevent this action?
cotroller:
function logout(){
$this->session->sess_destroy();
redirect('acontrol/index');}
thanks for your help.
You can check before the function calls that whether a session is there are not.If the session is empty then you need to redirect to login page again.Better you can check at __construct function in your class.It will called prior to your function call so,for each page the page will be redirect to login whenever your session is empty.
function __construct() {
if($this->session->userdata('iUserId') == '') {
// Redirect to Login page
redirect('controllername/login');
}
}
you can check the session into controller construct method
public function __construct()
{
// Initialization of class
parent::__construct();
if(!$this->session->userdata('id')){
$this->session->set_flashdata('error', 'Please login first');
/* if user is not login then
redirect back to login page and show error
*/
redirect('admin/login/index');
}
}

Codeigniter Passing parameters from view to controller

view has this anchor:
echo anchor('login', 'Login or Register');
how do i send the current url to my controller login? and then use it on another function called login_validation?
all i want is, login and back to the last url, however nothing works. so i thought saving the current url when i click "Login or Register" and then after login, on function login_validation, i should redirect to that url saved...
controller index
public function index(){
$this->main_login();
}
main_login
public function main_login(){
$this->load->helper('url');
// on view i will call the next function login_validation
$this->load->view("view_login");
}
login_validation
public function login_validation(){
$this->load->library('form_validation');
(...)
if ($this->form_validation->run()){
// i should redirect the saved url here, instead of home
redirect('home');
}else{
(...)
}
}
i appreciate any help
You can do this simply by using $this->agent->referrer() in your controller class main_login().
Save the referrer url into your session, and once the user is validated (or if they are), then you pull that string from session:
in main_login():
// grabs url where you clicked login
$this->session->set_userdata('referrer_url', $this->agent->referrer());
in login_validation():
// pull url from session and redirect, make sure to account for not having url too
redirect( $referrer_url );
I apologize for the delay, i was really busy with the end of the semester,
So i will explain how i did to solve this problem, all i wanted was back to the current page after the login.. so, everytime i open my view login i will save the last page url, with this code:
$refering_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '' ;
and then i should save it in my session, so i can access from my controller and redirect the user to the last page, but look, i can’t simple save the url every time i access the view login, beucase every time i miss the password, i will be redirected to the login page, and so the last url, will the login and not the last one, and of course its wrong, So we have to save it on session, but only in the first time we access the view login, to do that, i compare the variable above ($refering_url) to the view login and view login validation, if its not the same, i can confirm that the previous page is the one i was before the login, and then i can save it to my session,
here the comparison, and the last url saved in my session:
if (($refering_url != ‘URL TO VIEW LOGIN‘) &&
($refering_url != ‘URL TO LOGIN VALIDATION){
$this->session->set_userdata('url', $refering_url);
}
after login is validated, on the controller, i should redirect the user , to the last page he was (the url saved on the session), to do that i used this code:
$this->session->set_userdata($data);
$url=$this->session->userdata('url');
redirect($url, 'refresh');

MVC3 Razor Engine execution/rendering order

I have a few links (login, logout, and register) in the _layout template, where the links are shown depending on whether the user is logged in. Like so:
if (User.Identity.IsAuthenticated)
{
<span class="username">#User.Identity.Name</span>
<span class="link">#Html.ActionLink("Logout", "Logout", "Account")</span>
}
else
{
<span class="link">#Html.ActionLink("Login", "Login", "Account")</span>
<span class="link">#Html.ActionLink("Register", "Register", "Account")</span>
}
Problem is that the logout link is still displayed the first time the user logs out of the system (I would expect that to be immediately replaced with the login, and register links) - that is until the page is refreshed, or the user moves to another page. Here is the logout action code:
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Abandon();
return View();
}
I have gone through this link - http://mvcdev.com/differences-between-asp-net-razor-and-web-forms-view-engines/ - which explains the execution order of the Razor engine, but in my case it seems to be executing differently. Ideally I would expect the FormsAuthentication.SignOut() to execute before the User.Identity.IsAuthenticated in the _layout.
What am I doing wrong? Thanks!
That's normal, you need to redirect after logging out:
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Abandon();
return RedirectToAction("Index");
}
The reason this happens is because when the client requested the Logout link he was still authenticated (he sent the authentication cookie along with the request). Then inside the controller action you are logging him out (FormsAuthentication.SignOut()) which does nothing more than mark the authentication cookie for removal on subsequent requests. Then you return a view, and inside this view of course the user is still authenticated as this view executes under the same request and the cookie is still present.

Resources