I have below to handle logout and display information in the header to show after logout. But sometimes, it doesn't work. I suspect whether it is good to write this code to handle. Do you have any ideas? Please help
public function logout(){
$this->nativesession->delete('user');
redirect(base_url()); }
In header section of the php page:
<?php $usr = $this->nativesession->get('user');
if(!isset($usr) || empty($usr) || $usr["username"]==null) {
// show login button
}
Logout Function:
public function logout()
{
// Removing session data
$this->session->sess_destroy();
redirect('users/home');
}
Add In Header befor html tag:
<?php
if ($this->session->userdata['admin']!='admin' ) {
redirect('login');
}
?>
Related
I want to prevent user to access login page when its already logged in. In header page I've checked session so that user can't access any admin page without logging in. What code do I have to write in header page and session page to prevent user access login page while already logged in?
Here's my session class
public static function init(){
session_start();
}
public static function set($key, $val){
$_SESSION[$key]=$val;
}
public static function get($key){
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
} else {
return false;
}
}
public static function checkSession(){
self::init();
if (self::get("login")==false) {
self::destroy();
header("Location:login.php");
}
}
public static function destroy(){
session_destroy();
Check the session variable value on login page if exist then redirect or dashboard or home page if not exist session variable value redirect on login page.
Write your login page like this:
<?php
session_start();
if(isset($_SESSION['key'])){
header('home.php');
} else {
//Paste login page code here
}
?>
And this code avoid the user back to the page after log out
if(!isset($_SESSION['login_user'])){
header("location:login.php");
location.reload();
}
This code is to avoid repeated login from the user
if(isset($_SESSION['login_account_id'])){
header('location: /index.php');
}
<?php
// Initialize the session
session_start();
// If user is already logged in, redirect to index
if($_SESSION["loggedin"] == true){
header("location: index.php");
}
?>
i have a master page where all my menus are in and m unable to check if the user has logged in or no this is what i have so far
functions.php
<?php
ob_start();
function loggedin()
{
if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id']))
{
return true;
}else
{
return false;
}
}
?>
Masterpage.php
if(!loggedin()){
echo "log out";
}else{
echo "log in";
}
but this doesn't work for some reason i am always shown with the Logout option
i have tried changing the if statement but no success.
you have to start a session with session_start() before checking th session, and to make sure that the session has been opened with the correct name and value.
Also you can optimize your function to:
function loggedin(){
return !empty($_SESSION['user_id']);
}
Got this question here (sorry for being stupid), Just started with Codeigniter recently.
I have a login-system working fine. I tried to go to homepage while logged in with code on index-header.php:
<?php
if( !isset($_SESSION) ){
echo 'Login';
} else {
echo 'Log Out';
}
?>
And on main_view.php (homepage controller)
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
if($this->session->userdata('is_logged_in')){
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
} else {
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
}
}
}
Now, if I click logout from homepage while still logged in, it disconnects the session fine but doesn't change text back to "Login" in homepage after refresh.
In other words, it always shows text as "Logout" whether or not user is logged in.
dashboard.php (controller)
public function logout() {
$this->session->sess_destroy();
$data['title'] = "Logged out";
$data['logout_msg'] = "You have successfully logged out.";
$this->load->view('headfoot/header-login',$data);
$this->load->view('admin/login', $data);
$this->load->view('headfoot/footer-login');
}
Is it a good practice to create is_logged_in.php a separate file? If yes then how to link sessions to it?
change this:
<?php
if( !isset($_SESSION) ){
to:
<?php if($this->session->userdata('username') == null ){
i'm using 'username' here by assuming you have set username as session data when you allow user to log in.
Use $this->session->sess_destroy(); to kill session. Check this URL. Kill session by controller then use redirect.
By the way, create a file under application/core folder called MY_Controller.php and make your session things on it. If you want to learn more just google it.
I am trying to make a log in work. I am all good with the verification but is stuck when it redirects to an another controller to show the view of the logged in page. I am still new to codeigniter and is still not sure about how controllers work.
This is my controller for verifying logged in users:
function index() {
$this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE) {
$this->load->view('v_login');
} else {
//Go to private area
redirect(base_url('c_home'), 'refresh');
}
}
Its function is to validates the user if its in the database, however when the user is successfully logged in, it won't redirect to this redirect(base_url('c_home'), 'refresh'); It tells me that
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
This is the c_home.php where it is supposed to be redirected:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('m_login','',TRUE);
$this->load->helper('url');
$this->load->library(array('form_validation','session'));
}
function index() {
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['studentid'] = $session_data['studentid'];
$this->load->view('v_home', $data);
} else {
//If no session, redirect to login page
redirect('c_login', 'refresh');
}
}
function logout() {
//remove all session data
$this->session->unset_userdata('logged_in');
$this->session->sess_destroy();
redirect(base_url('c_login'), 'refresh');
}
}
is it okay to redirect a controller from another controller?
after it redicts to c_home.php
it will show the v_home.php
<!DOCTYPE html>
<head>
<title>Simple Login with CodeIgniter - Private Area</title>
</head>
<body>
<h1>Home</h1>
<h2>Welcome <?php echo $studentid; ?>!</h2>
Logout
</body>
</html>
You don't need the base_url() in the redirect. just use
redirect('c_home',refresh);
Although there are 2 things i should make you aware of, you need ('controller/function') not just ('controller'). And also make sure you're loading $this->load->helper('url') in your __construct function on both controllers.
Although just for future reference, i think this is what you meant.
redirect(base_url().'c_home',refresh)
Codeigniter's redirect function already has the site_url() embedded.
So, instead of this;
redirect(base_url('c_login'), 'refresh');
Use this, as you have earlier in your code
redirect('c_login', 'refresh');
Hope this helps
Hi I am developing a website using codeigniter,php that requires secure login, The problem arises when I logout, & firstly I destroy the session but trouble is, when I click back on the browser it is displaying the Login page again.. Thanks for answers in advance
Following is my index function &
website is my controller .
public function index()
{
if(logged_in() )
{
redirect('/website/dashboard');
}
else
{
redirect('/website/login');
}
}
code works for me.. But when i logout from site & press back button i am seeing my dashboard again...
my log out function :
public function logout($redirect = false)
{
$this->CI->session->sess_destroy();
if($redirect)
{
$this->CI->load->helper('url');
redirect($redirect, 'refresh');
}
}
I suggest you to add something like following code to each of your VIEW page
<?php
if($this->session->userdata('session_set')!='true' )
{
redirect('/website/login');
}?>