Codeigniter Session is not working when uploaded to host - php

I have created a PHP project using Codeigniter and its working perfectly with the localhost. I'm using Xampp 3.2.1 and when I upload the project to the server and try to load the project its working and show the login page. When I enter credentials and login, it redirect me to the home page of my project and when I try to navigate to any other location it redirects me to login page. Please can any one help me on this matter?

This cause because Codeigniter In-built session is not supported in the sever. What you can do is Use PHP Sessions
refer to this link if you want more details -
http://www.php.net/manual/en/book.session.php
What you can do is use start_session() in you controller and use $_SESSION to save your session data and access it.
If you have More than one Controller the approach is different,
You have to create a controller in your project's \application\core\ call MY_Controller(Its okay if you want to use a different Name). The Code of the Controller Should be
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!isset($_SESSION))// to avoid A session had already been started - ignoring session_start()
{
session_start();
}
}
}
?>
and extend all your controllers in your \application\controllers\ with this controller in order to access the session globally
Now use
$_SESSION['data_name'] = $Your_Data;
to set values to session
and in log out function just use session_unset() to clear your current session data
Hope It helps :)

Related

How to separate frontend and admin in CodeIgniter framework?

I started working on a project which should have admin panel and frontend and I want to use CodeIgniter framework on client request. But the problem is I am not able to understand how to start the project as mentioned above.
I want folder similar to the image shared
Besides the Admin_controller (for separeted security rules), for better organization, it's good to use some extension like this one:
HMVC: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src
With this you'll be able to that this type os structure:
URLs
http://awesome.site/public_controller
http://awesome.site/*module_name*/*controller_inside_module*
http://awesome.site/admin/login
Try using Codeigniter's session functionality to authenticate the user and his role (e.g., "admin", "customer", etc)
Then add a constructor like this to every controller (this is just an example)
class Admin_only extends CI_Controller {
public function __construct()
{
parent::__construct();
if( !isset($this->session->userdata['logged_in']) || $this->session->userdata['logged_in']['user_type'] != 'administrator' )
{
// you're not welcome here
redirect('welcome/access_error');
}
}
The __construct() is run every time anything within the controller is accessed.
See how in my example (there's cleaner ways, but this will definitely work), I'm constantly checking if the user is logged in AND if the user is an administrator (actually I'm checking the opposite... logged out OR not administrator, but it's pretty much the same thing logically) and if the check fails, the user is redirected away from the controller.

Set session data after authenticate user using default auth route in laravel 5.2

I am developing a project using laravel 5.2. I use default authentication in this project by running this command.
php artisan make:auth
Everything is working great. Now I want to put some data in session after authenticate the user. I can not find the place where I put my code to store data in session after authenticate the user. I googled but not found any solution.
Finally I got answer from another question. Here i post the solution for who are looking this type of solution
https://stackoverflow.com/a/36491235/2738927
You can check whether the user is logged in or not using the following code.
if (Auth::check()) {
// The user is logged in...
}
Then you can use the session helper class to set the required value in session.
session()->set('session_key', 'session_value');
Additionally, you can remove the session using the forget method.
session()->forget('session_key');
Resources to read.
https://laravel.com/docs/5.3/authentication
https://laravel.com/docs/5.3/session
https://laravel.com/docs/5.3/helpers#method-session
In 5.2 you need to override login() method from Illuminate\Foundation\Auth\AuthenticatesUsers trait in AuthController.php.
To set data use session() helper in login() method:
session(['key' => 'value']);

Including directory and keep it working php

I'm using Codeigniter and facing the following problem.
In a controller, I want to include in some way an application (Not written in Codeigniter) in my controller. I am using file_get_contents now. It's working fine and the application is shown in the controller I made. The problem is, the application contains a lot of forms which are posted to other pages in the application itself. Using file_get_contents, the forms redirect the user to a target outside my controller. This may sound a bit vague, so here's an example:
The user navigates to the controller: 'game/login' and therefore wants
to visit the login page. The user fills in the form. The form posts
the data to assets/game/index.php?page=login. This makes the user
redirect to assets/game/index.php?page=login, and therefore making him
leave the controller page.
Does anyone have an idea on how to fix this problem?
defined('BASEPATH') OR exit('No direct script access allowed');
class Game extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index($page = ''){
file_get_contents(FCPATH . 'assets/game/index.php?page=' . $page);
}
}
You can load your external php pages in a iframe but i would suggest you should convert your php files to codeigniter MVC structure to get the advantages.
The quick way is using iframe,but i would ask you to refer this post to get an idea on Iframes when to use & when not to.
Are iframes considered 'bad practice'?

How to include Codeigniter from external page?

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

how to prevent entering to the site using url typing in codeigniter

I have a site using CodeIgniter that is almost complete now. My problem is that, even though I have implemented sessions and maintain a login system, a person can access any page by typing the URL into the browser address bar.
I have implemented the session for patient registration like this:
function index(){
$this->is_logged_in();
}
function log_out(){
$this->session->sess_destroy();
redirect('login_controller');
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in)||$is_logged_in!= TRUE ){
redirect('login_controller');
}else{
$this->main();
}
}
Anonymous users can't acess the system just by typing the controller name like this:
http://localhost/demo_site/index.php/register_controller
But they can do it like this:
http://localhost/demo_site/index.php/register_controller/search_patient
Person can't access by typing the controller name, but can enter the system by typing a longer url than the controller, like the one shown above.
What is the problem here? What are the possible solutions??
You will have to implement a login check in the controller's constructor.
Whenever the controller is called, it should check if the user is logged in - if they are not, redirect to a login page or an error page.
To confirm if it is entering the login check put an echo and exit inside the is_logged_in() function and check if it appears in case of http://localhost/demo_site/index.php/register_controller/search_patient
You are probably doing login check in your respective modules and thus you missed for some cases.
It is better to define a set of private modules (say in an array) and do the login check in the frontcontroller itself (in one place) instead of repeatedly in module level.
Sounds like a routing problem. You need to set up your routes to make the second case illegal or at least map to the same controller as the first case. More on routing here.
I agree with tHeSiD. This code should go in the constructor. Ideally in a base class which you use to extend all admin related or restricted classes with. Normally I use an Admin_Controller base class that extends CI_Controller (2.0) or Controller (1.7.x) and then create my application controllers by extending the Admin Controller.

Categories