I am using codeigniter and what i want to do is when user logs in, I am storing his first name and last name in session.
i have header file which i include in all the views
i want to show something like this
welcome "amit patil" | Logout
where "amit patil" is coming from session.
How can i access session value and display in header file
I know it can be possible with this
$data['admin_fname'] = $this->session->userdata("first_name");
$this->load->view("header",$data);
I dont want to repeat this process in all the controllers, Is there any easy way ??
You can create an helper to do it.
function getUserFirstName() {
$ci =& get_instance();
return $ci->session->userdata("first_name");
}
And then load the helper with $this->load->helper() in your controller. If something related to your user changes you will need to change only in the helper.
Related
i'm having issue getting session variable in a custom page like a controller.
For example if i create a controller like myController.php and i execute the #dd of data
$data = session::all();
dd($data);
I get no results.
But if i execute the same code in another page like homeController.php i retrieve everyhing the session have like user id etc. My question is how i can retrieve session variable in another page in laravel like i do in plain php with start_session() ?
Check that your route calling this controller you are working in, is under the web middleware, otherwise the session won't be shared.
I'm trying to code the system in that things are separated using "userId". That means, products or services are separated on the basis that user who add them in the system.
For that I'm trying to pass the userId from user controller to service and product controller. But I got an error that undefined variable userID.
$data['id'] = $this->session->userdata('user_id');
pass this in create() function of department controller.
i'm loading user model in __construct() function also.
Thanks in advance.
You can do it using two ways.
1) By Helper - simple create a custom helper file and create a function inside helper file and inside function add get userid code and then using function name you can access this userid anywhere in whole project.
2) Using By Require Controller Class -- if you want to access user id in another controller so i will explain with you by example.
Ist Controller - Test.php
here i defined a variable as a public and this variable contains userid value.
2nd Controller - Test2.php
here we will require Test.php file
require('Test.php file path');
and then create object and then we can access Test.php file variables and methods inside Test2.php file.
But i will suggest you first way it`s very easy way and multiple advantages.
Hope it will work for you.
I want to be able to check a page outside of the CodeIgniter environment to see if a user is logged in.
My setup might be as follows:
test.php
/codeigniter/index.php
In my CodeIgniter application, I have a method which checks to see if a user loggedin:
if($this->user->loggedin) {
// Logged in
} else {
// not logged in
}
It's basically calling my user class and checking the loggedin variable. It's easy to use inside the CodeIgniter environment.
How can I use the CodeIgniter functions from my script test.php?
You could set a cookie in CI that your test.php page can check. That would do it.
Or here is something that might help:
Accessing CodeIgniter super object from external php script outside codeigniter installation
I have some problems using Code Igniter and I feel there is something I don't understand because I can't get my redirects and my headers to work. Here is the situation :
When site is entered, the default "home" controller is called.
public function initialize()
{
printf("CONSTRUCTION OF HOME CONTROLLER - \n");
// print_r($_SESSION);
//TODO : CONSIDER CREATING A LIBRARY TO AVOID WRITING THIS OFTEN. NOT
// SESSION TROLLING DETECTION
if( isset($_SESSION['banana']))
{
echo "SPLITTING THE TRUTH";
}
// GETTING AS SERIOUS AS GREG
if( !isset($_SESSION['username']))
{
printf("USERNAME IS NOT SET. SETTING UP THE LOGIN PAGE. \n");
redirect('home_invite');
}
else
{
$this->load->view('welcome_message');
}
}
public function index()
{
//INITIALIZING THE PATH USED FOR THIS NAVIGATION
printf("TROLLING THE BEGINNING OF THIS CONTROLLER HOME - ");
$this->initialize();
printf("TROLLING THE END OF THIS CONTROLLER HOME - ");
//TODO : CONSIDER CREATING A LIBRARY TO AVOID WRITING THIS OFTEN
}
Index calls initialize who verify if the user has already a session variable with username in it. If that's the case, we would proceed to check his level of privileges, etc, and load corresponding view. Thats not the problem.
If the session is not started, I want to load the "login" view, called here "home_invite". And I want to redirect him to that page. But if I use this code, the page will show a 404 error.
If I use $this->load->view('home_invite'), it works, but I don't understand and I feel it isn't what I want it to do.
Why is redirect not working in this context?
Using the redirect() method redirects to a URL. You therefore need to pass it a full URL (as it uses the header() function which according to the RFC for HTTP1.1 requires a full URL.
This means that you can use
redirect(site_url('home_invite'));
Which will redirect your user to http://www.yoursite.com/home_invite
This means that you must have a controller called home_invite available as you can't load a view from the URL. Equally you could create a method in your existing controller and use the routes.php file to masquerade /your_controller/home_invite as /home_invite
The site_url() function is also part of the URL helper you've already included to use redirect().
If you don't want to use site_url(), you could just as well hard code the URL in like
redirect('http://www.yoursite.com/home_invite');
I'm not looking for the whole ACO-ARO implementation... I just want to use Auth, and check against the user's role....
What do I put where in order to simply deny users from a given controller unless they have a certain role.
I'm trying to use the $this->Auth->authorize = 'controller';
... but I don't even know where to put that??
Any help would be awesome!
Thanks in advance.
Short answer: Sounds like you need to create and app_controller.php and put your code in the beforeFilter method.`
Longer Answer: Create an app_controller.php file in you app directory and put the following code in beforeFilter().
if (isset($this->params[Configure::read('Routing.admin')])) { //User is trying to access a page using the admin route
if ($this->Session->check('someSessionVariable')) { //Check user has some session variable set.
// User is accessing an admin page and has permission, do something, or in most cases do nothing.
} else { //No sessions set for user, redirect to login page.
$this->redirect('/yourLoginPage'); //Redirect
}
}
This is no substitution for proper user of the Auth component, but should do what you need. Make sure you check its secure before you put it into production.