laravel redirection error after session out - laravel

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");
});

Related

How To check if a url in laravel was acceded through a view or through the browser?

I have a URL with a defined route which can be accessed through the browser by entering the url or through a view. I need to check if that URL was accessed using a browser(entering a url) or through a view triggered by an action of the user.
Is that a way to do that in Laravel ?
You can check for the existence of a previous url
Route::get('test', function () {
if (url()->previous() === config('app.url')) {
dd('accessed from browser');
} else {
dd('accessed from view');
}
});
Hope this helps

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.

How to logout code igniter session without form

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');
}

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');
}
}

Laravel 4 redirect logged in users to a specific page

Hello what I am trying to do is fairly simple I know it can be done with routes.php I am confused about how to do it, I would like all logged in users to not be able to see "/"
so when they try to access '/' they get send to '/name'
Route::get('/', function() {
// ...
return Redirect::to('/name');
});
this doesnt check if they are logged in
Use before filters:
Route::get('/', function() {
return View::make('your-home-view-comes-here');
})->before('auth');
Edit the filters.php file and change it to:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/name');
});
you'd also might want to group the routes to a filter as explained in
laravel's route groups
this will be easier to manage if you want that filter to run for multiple route item.

Resources