Call function in the controller from a model - codeigniter - php

i have implement the php sdk for the facebook login in my project.. and all works with the login except the logout.
how can i write the code to call a function from the controller to destroy the session?
tried a lot of things but canĀ“t get understand how can i do this..
hope that somebody can help me with this.
my model with the logouturl is this:
'logoutUrl' => $this->facebook->getLogoutUrl()
and the function in the controller is this:
public function logout()
{
$this->CI->session->sess_destroy('logourUrl');
$this->load->view('home');
// do more thing you want to do such as redirect
}

Try adding the below in your controller
function logout()
{ $logout = $this->facebook->getLogoutUrl(array('next'=>'url to be redirected after logout'));
$this->facebook->destroySession();
$this->session->sess_destroy();
header("Location:$logout");
}
You don't need to load the view because facebook will automatically redirect to the given url after logout.

Related

How To Prevent Direct Url Access In Codeigniter

Let's said I have 2 pages "LowAdmin" and "Login"
I want to prevent user from accesing "LowAdmin" directly from url.
Instead, I want to redirect them to "Login" page.
I have a problem explaining my pages to my teacher, because he access LowAdmin, instead of Login page.
Anyone can help? I almost finish this final project.
By analysing your project code in GitHub you can redirect by changing your constructor function of LowAdmin Controller like this.
function __construct(){
parent::__construct();
$loginstatus = $this->session->userdata('status');
if($loginstatus!="login2"){
redirect(base_url("login"));
}
$this->load->model('project_model');
}
Please comment below if you encountered any problem.
By session you can
do it.
For example
public function index()
{
$this->load->library('session');
$admindata = $this->session->userdata('admin');
if($admindata){
$this->load->view('admin/dashboard');
}else{
redirect('admin/login');
}
}

Not able to redirect in codeigniter

I have created a user login and logout system. After login the user gets redirected to his dashboard. Currently with the help of correct url, the user can go directly to dashboard without login. to restrict this i used the following code so that user can not access any internal pages before login
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
if(!$this->session->userdata('logged_in'))
{
$this->session->set_flashdata('no_access', 'sorry you are not allowed');
redirect('user/noaccess');
}
}
}
Now when i try to access the dashboard with a correct url, but without login i am not able to view it but is is also not etting redirected to noaccess. i am getting the following error
the page not redirecting properly
Can anyone please tell how to do this properly
Sam consider doing this
public function logged_in()
{
$logged_in = $this->session->userdata('logged_in');
if (!isset($logged_in) || $logged_in != true)
{
redirect("Checkin"); //insert your login page
}
}
Then in your controller for page access you can call this method
public function admin()
{
$this->logged_in();
//rest of controller here
}
This will prevent anyone from accessing your admin pages without login. The "logged_in" method will redirect them to the login page. That way you are not redirecting to yet another page(user/noaccess) or using flashdata. Use the $this->logged_in(); on any method that requires login

codeigniter redirect when not login

I'm trying to make a login process in codeigniter because I'm new to it and want to familiarize myself with it. Then I want to add a security feature where if the dashboard is accessed using the URL, if the session of account_id is not set I will redirect them to login:
public function index() {
if ($this->session->userdata('account_id') === FALSE ) {
redirect('/');
}
return $this->_renderPage();
}
but then it gives me this result
As you can see it's still in the dashboard but its all blank page and it did not redirect.
How can I fix this?
Thanks in advance for the help.
Have you load the right helper ? Do it with $this->load->helper('url');.
After you can use redirect like this: redirect('/', 'refresh');
The CodeIgniter documentation about url helper
PS: The PHP way to do this is header('Location: http://www.website.com/');

Laravel Auth Filter and Logout

I am developing a website in laravel. I am facing a problem with the auth filter and the logout method.
In my home controller I want to apply the auth filter for some of the methods as follows.
public function __construct()
{
parent::__construct();
$this->filter('before',array('auth'))->except(array('index','view'));
}
I have action_logout as follows.
public function action_logout()
{
Auth::logout();
return Redirect::to("/home/index");
}
When I have logged in and trying to logout, i am not being able to access the logout action. I tried echoing something from the inside of the action but it didnt work.
changing to this..
$this->filter('before',array('auth'))->except(array('index','view','logout'));
works. Logically I should be able to logout only when i have logged in. But applying the filter above m not being able to access the logout action after loggin in. Help!.

cakePHP stuck on logging page with Auth

I have a problem here. I created a cakePHP application using the Bake feature of cakePHP. I baked my Model, my Controller and my views (with default index, add, edit, and view actions). I created a small table called users in my database which contains only three fields (id int auto_increment primary key, username varchar(15), password charr(40)). The problem that I'm having is that when I use the Auth component, I get stuck on the logging page forever (until I take it out). I have tried almost EVERYTHING in my functions login() and beforeFilter() with no success. Any idea please?
I use the Auth component like so in my Users Controller:
var $components = array('Auth');
I have tried this in my function beforeFilter(), but it does not work:
function beforeFilter() {
$this->Auth->autoRedirect = false;
parent::beforeFilter();
}
I have even tried to redirect right in my function login() like so:
function login() {
$this->redirect($this->Auth->redirect());
}
But when I do this I get error 310: TOO MANY REDIRECTS.
I cannot go to my index, or add, or view page. Help please?
In the code you provided, you don't seem to do anything to make the 'login' action reachable if you are not logged in yet.
function beforeFilter()
{
parent :: beforeFilter();
$this->Auth->allow('login');
}
If you don't do that, the login page is protected, making you redirected to... the login page, making you redirected to... the login page, making you redirected to... the login page, making you redirected to... the login page ;-)
This piece of code has you caught in an endless loop, because it keeps redirecting you back to the login page:
function login() {
$this->redirect($this->Auth->redirect());
}
Read up here on list of Auth component variables. Specifically the loginRedirect part. You need to put that in into your beforeFilter function:
function beforeFilter() {
parent::beforeFilter();
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
}

Categories