CodeIgniter - change nav link based on session data - php

I have an home controller who control the homepage (that is a simple landing page with no user interaction or dynamic data):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
private $data;
protected $pagedata;
function __construct()
{
parent::__construct();
if ($this->session->userdata('is_logged_in') == true) {
$this->data['nav'] = 'auth/template/homelogin_nav';
}
$this->pagedata['title'] = 'La Giumenta Bardata Cosplay & Props';
}
/**
* Index Page.
*
**/
public function index()
{
$this->load->view('template/header', $this->pagedata);
$this->load->view("template/nav", $this->data);
$this->load->view('section_header');
$this->load->view('section_about');
$this->load->view('section_services');
$this->load->view('section_portfolio');
$this->load->view('section_social');
$this->load->view('template/footer');
}
}
So basicly, if user is log in I load a certain view that correspond to a nav, if not it's loaded the normal menu.
Now, the two navs are different just for one link (one nav's view has a link to the login page and the other one has a link to the user dashboard).
I also try this:
$this->load->view($this->data);
but of course is illegal and it doesn't work.
The problem starts because I have to check for the session in the costruct and not inside the function index() or I can't check it.

Why don't you check for session inside of view?
Make one navigation and if user is logged in show dashboard, if not show login button in navigation.
Navigation view:
<ul>
<li> <?php echo anchor('home','Home'); ?> <li>
<?php if($this->session->userdata('is_logged_in')== true){
echo "<li>".anchor('dashboard','dashboard')."</li>";
echo "<li>".anchor('logout','logout')."</li>";
}else
echo "<li>".anchor('login','login')."</li>"; ?>
</ul>

Related

Not able to restrict user activities after successful login in codeigniter

i have a login process where the user can view his dashboard after login.
The code in controller:
$adminid = $this->am->login_admin($email, $password);
if ($adminid) {
$admin_data = array(
'adminid' => $adminid,
'email' => $email,
'logged_in' => true,
'loggedin_time' => time()
);
$this->session->set_userdata($admin_data);
$this->session->set_flashdata('login_success', 'You are logged in');
redirect('Admin_dashboard/dashboard/' . $adminid);
} else {
$this->session->set_flashdata('login_failed', 'Invalid login!!');
redirect('admin/index');
}
After successful login the user is getting redirected to the following url
localhost/project/Admin_dashboard/dashboard/1
The issue is that if the user manually changes the url to something like this-
localhost/project/Admin_dashboard/dashboard/2
he is able to access the data of user whose id is 2 without login
To solve the issue i tried using the following codition in the view
<?php if($this->session->userdata('logged_in')): ?>
<? endif; ?>
However the 2nd url is still accessible
After login the user gets redirected to dashboard that also contains few other pages such as profile page, payment page etc which contains data that is only related to him.
I want that after login he should be able to see all his pages but not anyone else data by changing the url
Simply do one thing, instead of passing $adminid with the url, get the adminid with session, because you also storing values in session.
Instead of
redirect('Admin_dashboard/dashboard/' . $adminid);
Use this
redirect('Admin_dashboard/dashboard');
and inside the dashboard function in Controller use this
public function dashboard (){
$admin_data = $this->session->userdata('admin_data');
if(!isset($admin_data['adminid']) || empty($admin_data['adminid'])){
//Error message Login First
redirect('admin/index');
}
$adminid = $admin_data['adminid'];
//Proceed with this $adminid
}
Simply add this code to all controllers for maintaining user restrictions throughout all URLs.
Class Controller_name extends CI_Controller{
function __construct(){
parent::__construct();
if(!isset($this->session->userdata['logged_in'])){
//redirect login page
}
}
/**
Your Other Functions
**/
}
Let me know If you have anymore doubts..
set user session is valid or not in dashboard controller before load dashboard view and also check user session adminid value with uri segment value
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->session->userdata('logged_in')) {
redirect('Login', 'refresh');
}else{
$uri_admin_val=$this->uri->segment(2);
$adminid=$this->session->userdata('adminid')
if($adminid!=$uri_admin_val){
redirect('Admin_dashboard/dashboard/' . $adminid);
}
}
}
}
And extend this my controller on dashboard and other controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Dashboard extends MY_Controller {
public $data;
public function __construct() {
}
}

Loading view and routing in CodeIgniter after user logging in

I am trying to make a login with CI which basically after the user is logged in, where he'll be redirected to either page admin/dashboard or superadmin/dashboard depending on their user type role. I've successfully loaded the views but there's something wrong with the url. After logging in, the website manages to load the right view depending on the user's type, but the url still says .../auth/login. How do I change this? I want the url to say .../admin/dashboard or .../superadmin/dashboard
This is what I've got in my controller Auth function login.
if($this->ion_auth->is_admin()){
$this->load->view('superadmin/dashboard_view');
}else if($this->ion_auth->in_group('members')){
$this->load->view('admin/dashboard_view');
}else{
redirect('/', 'refresh');
}
And here's my login form.
<?php echo form_open("auth/login");?>
<p>
<?php echo lang('login_identity_label', 'identity');?>
<?php echo form_input($identity);?>
</p>
<p>
<?php echo lang('login_password_label', 'password');?>
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('login_remember_label', 'remember');?>
<?php echo form_checkbox('remember', '1', FALSE, 'id="remember"');?>
</p>
<p><?php echo form_submit('submit', lang('login_submit_btn'));?></p>
<?php echo form_close();?>
Instead of loading views from the login you will need to redirect. This means two different controllers, one for admin
class Admin extends CI_Controller
{
public function dashboard()
{
$this->load->view('admin/dashboard_view');
}
}
And another for superadmin
class Superadmin extends CI_Controller
{
public function dashboard()
{
$this->load->view('superadmin/dashboard_view');
}
}
And controller Auth function login
if($this->ion_auth->is_admin()){
redirect('superadmin/dashboard');
}else if($this->ion_auth->in_group('members')){
redirect('admin/dashboard');
}else{
redirect('/', 'refresh');
}

Codeigniter Route url parents page and child page

Edit: I am new Codeigniter I am not how to use Codeigniter Routing. I create Contact Us page and Map page. Map page is the subpage of Contact Us page.
Table Name : Pages
id label link parent
1 Contact Us contact-us 0
3 About Us about-us 0
2 Map map 1
Here my Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('page_model');
}
public function index()
{
$data['getAllPage'] = $this->page_model->getAllPages();
$this->load->view('page_listing',$data);
}
public function view($id) {
$data['single_page'] = $this->page_model->displaySinglePage($id);
$this->load->view('single_page',$data);
}
}
In routes.php I have put $route['(:any)'] = "page/view/$1";
When I enter url "http://mytest.dev/contact-us/" or "http://mytest.dev/about-us/" it show correct content of Contact page but I enter "http://mytest.dev/contact-us/map" it still show content of Contact page.
What I want when I enter "http://mytest.dev/contact-us/map" it shuold show content of Map page
Thanks in advance.
Please try code maybe can help
// Parents and Child page.
$route['page/(:any)/(:num)'] = "page/view/$1/$2";
// For Main Home Page.
$route['(:any)'] = "page/view/$1";
I think following routes should work
$route['contact-us'] = 'page/view/$1';
$route['contact-us/map'] = 'page/view/$2';
when you'll echo $id in view method.
"http://mytest.dev/contact-us/" will print $1
and
"http://mytest.dev/contact-us/map" will print $2
Hope this solution will help.
My site has a main 'events' page and an 'events/calendar' page. Create the pages as separate views and then create a function for each sub page in the main controller. Here's my Events controller. If you want a dynamic page you can add the appropriate code in the function.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Events extends CI_Controller {
public function index()
{
$this->load->view('inc/header.php');
$this->load->view('events_view');
$this->load->view('inc/footer.php');
}
public function calendar()
{
$this->load->view('inc/header.php');
$this->load->view('calendar_view');
$this->load->view('inc/footer.php');
}
}

Changing content when user has logged in i.e. when session starts

How do I change the content for a user when he logs in? I mean like enabling voting, changing "login" to "logout" etc.
What I think to do is to start the session when user logs in (I am preferring to start session only when user logs in, not all the time). Then add data to the session's cookie like-
//controller
$moredata = array(
'username' => $this->username,
'login' => TRUE
);
$this->session->set_userdata($modedata);
//redirect
Then in the other controller, where he has been redirected I check the following-
$login = $this->session->userdata('login');
if ($login==TRUE)
Depending on the 'if' condition I will pass a variable to the view, with the help of that variable I will forward only the div/sections which should be shown to a logged-in user.
The problem is, while performing the above comparison Codeigniter shows following error (remember I haven't added 'session' in autoload array yet)
Message: Undefined property: NameOfController::$session
And If I set following in the autoload file
$autoload['libraries'] = array('session');
then the "if ($login==TRUE)" comparison always shows FALSE.
What should I do?
If I were you, I'd place all your session checks in a base controller which all your other main controllers extend. This allows you to keep things DRY:
class BaseController extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function isLoggedIn()
{
// Will return true or false
return $this->session->userdata('login');
}
}
And in one of your functional controllers (the example below handles users):
class UserController extends BaseController {
public function __construct()
{
parent::__construct();
}
public function profile()
{
// Redirect if not logged in
if (!$this->isLoggedIn()) {
$this->redirect('some/other/page')
}
}
public function register()
{
// Show different HTML if not logged in
$data = array(
'isLoggedIn' => $this->isLoggedIn()
);
$this->load->view('register', $data);
}
}
The second method in UserController allows you to render different content in your view:
<? if ($isLoggedIn): ?>
<p>You're logged in!</p>
<? else: ?>
<p>Not logged in</p>
<? endif; ?>
On my last project we created a simple permissions helper that had functions to check for logged-in status and for privilege levels. Then we'd just call the helper's functions as needed from anywhere in the system. If the user is logged in and has privs for that content then they get the content - otherwise we'd redirect them to a registration or other error page. Since all of that logic is in the helper functions, we could wrap any permission-requiring code in a quick permissions call like if(is_logged_in()){code requiring login to access}.

Change url in php after reloading a page

My home page url looks like this
http://localhost/mediabox/home/box/12
I have a link of language on home page when a user clicks on that link i am sending that language id as query string , the page reloads and the url converts to
http://localhost/mediabox/home/box/?bid=12&ln=2
I want to reload the page with the new language but don't want to change my url i-e I want my URL to be
http://localhost/mediabox/home/box/12
after the page loads
How it is possible please me some gud ideas
Thanks
VIEW
<a href=<?php echo site_url('home?language=indonesian');?>>Indonesian language</a>
CONTROLLER
class Home extends CI_Controller {
public function index()
{
$language = $this->input->get('language');
if($language){
// Put your code here
// Now u can set session
$this->session->set_userdata('language', $language);
redirect('home');
}
if($this->session->userdata('language'))
{
var_dump($this->session->userdata('language'));
}
echo 'Hello World!';
}
}

Categories