I developed this functionality locally on my laptop and all worked well, but when I uploaded changes on the server, I received an issue.
The check_admin_auth() function checks session variable and it is NULL when it calls from my controller (all other controllers in the project with the same code works well).
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Ctransaction extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->auth->check_admin_auth(); // this function
/* checks one session item to check is user logged and admin or not,
if not - redirects to login page. But in my case redirecting doesn't apply,
I see just white screen, page status 200. */
$this->db->query('SET SESSION sql_mode = ""');
}
public function index()
{
// some code here
$content = $this->parser->parse('gltransaction/list', $data, true);
$this->template->full_admin_html_view($content);
}
public function ajaxLoadGLTransactions()
{
// some code here
echo json_encode($result);
}
}
Please, can you help me to find where the problem is?
I think that's settings in php.ini or in the same place, but I didn't find a reason.
I tried to modify /system/libraries/Session/Session.php as it was recommended in web, but it didn't help.
Files with sessions are created in folder /application/ci_sessions and they stored all necessary data, but for some reason, my controller doesn't load it and returns on its page session with only one field - __ci_last_regenerate.
The library session is loaded in autoloader in configuration file.
Cookies are the same on working pages and on page of my controller.
CI Version 3.1.4
PHP Version 7.2.32
The problem was that before <?php I've added one more line. The file of controller was like:
// empty line without anything
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// and all other code
Didn't think that it can make sense!
Related
I am accessing a Putty server to try and run a method from a controller.
I have a controller inside a batch folder named GetPrefectures.
The controller's code:
<?php
require_once(APPPATH."controller/batch/GetIndustry.php");
class GetPrefecture extends BaseController
{
function __construct()
{
$this->load->model("PrefectureModel", "prefModel");
}
public function init()
{
log_message('test_debug');
$this->checkSteps();
}
public function checkSteps()
{
$completed_steps = $this->prefModel->checkStepstbl();
$this->getList($completed_steps);
}
}
?>
I try to add a log message so I know that the controller is working.
Here's how I do it.
after I access the server and locate the path for my file, I enter the code to run the controller.
php /var/www/listingapp/public/index.php batch/GetPrefecture init
then, I use the logs with this code to check if it is running.
tail -n1000 -f application/logs/log-2017-08-25.php
But after this, the cmd shows me this as logs:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
DEBUG - 2017-08-25 10:55:46 --> UTF-8 Support Enabled
DEBUG - 2017-08-25 10:55:46 --> Global POST, GET and COOKIE data sanitized
I cannot see any log message "test_debug", I think it cannot detect the init, so this means nothing is running.
How can I do this? Am I missing something?
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'?
I built an user login to my site by this guide:
http://www.iluv2code.com/login-with-codeigniter-php.html
I have few question about session's
I need to put the session_start(); in every controller or there is a way in codeigniter that it will automaticlly be in all controllers? (should I do that?)
and there is a other way rether to put in every function that:
if($this->session->userdata('logged_in'))
{
//function code
}else{
//If no session, redirect to login page
redirect('../login', 'refresh');
}
or should I do that for every controller function (for example if I have controller named page and he have the functions :index,edit,view I need to put it for every one of them?
and last question, I have logout button on the top of every page called by view/header
should I also put this function:
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
in every controller or I can do it a "golbel" function in some way?
EDIT:
I use this in hooks.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files. Please see the user guide for info:
|
| http://codeigniter.com/user_guide/general/hooks.html
|
*/
$hook['post_controller_constructor'] = array(
'class' => 'SessionData',
'function' => 'initializeData',
'filename' => 'loginHelper.php',
'filepath' => 'hooks',
'params' => array()
);
and this in loginHelper.php:
<?
class SessionData {
var $CI;
function __construct(){
$this->CI =& get_instance();
if(!isset($this->CI->session)) //Check if session lib is loaded or not
$this->CI->load->library('session'); //If not loaded, then load it here
}
function initializeData() {
// This function will run after the constructor for the controller is ran
// Set any initial values here
if(!$this->CI->session->userdata('logged_in')){ //call session methods with super object
redirect('../login', 'refresh');
}else{
$data['user'] = $this->CI->session->userdata('logged_in');
}
}
}
?>
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */
the user['data'] not created in all the pages. where am I wrong?
For your second question about logout, I usually put the logout function in a User controller and call it as Log Out.
For your first question, I saw a tutorial how you do user login in a controller and extend that controller in your regular controller, thats how you avoid login check in every function. I am trying to find that tutorial, once I get it, I'll share it, but the concept is like that way.
you do not need to put the session_start(); in every controller!
You could simply start the session class in the autoload.php file in your config directory!
$autoload['libraries'] = array('database', 'session', 'encrypt');
Also, it is better to check if the user is logged in, inside the constructor function of the classes!
if(!$this->session->userdata('logged_in'))
redirect('loginController/loginFunction', 'refresh');
and to destroy all the sessions when logging out, you could use sess_destroy();
Initializing a Session
To autoload Session, open the application/config/autoload.php file and add the item you want loaded to the autoload array so: $autoload['libraries'] = array('session'); Yo don't use session_start(); and session_destroy(); at all.
Session Documentation:
https://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Auto-loading Resources:
https://ellislab.com/codeigniter/user-guide/general/autoloader.html
To Check Login
Use Hooks to avoid chunky code dupplications and it is better practise. Navigate bottom of the page and read Hook Points so you get an idea. It is easy! Definitely learn hooks.
Logout
You answer explained here.
After alot of working I solved the problem with the login check.
I didn't use the hooks. I build a MY_Controller in core folder and exteds it in all my controllers expect from the login controller.
In the MY_Controller I use thie login check.
CodeIgniter core controllers:
https://philsturgeon.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY/
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 :)
Forgive me if this is a "duh" question or if you need more information to answer, I am new to CodeIgniter and still haven't figured out a few things with best practices and such...
In routes.php I have $route['default_controller'] = "home"; so my default_controller is obviously "home".
Inside my home.php controller I have:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct() {
// blah
}
function index(){
// blah
}
function login(){
// blah
}
}
Which works fine and everything, there is no problems with that. The only thing I can't figure out is if I want to access the login function, I currently have to go to www.blah.com/home/login. How can I change it so it just goes to www.blah.com/login, without creating a new controller (I would like to keep some of these one time base urls all in my default controller)? Is that even possible or do I just have to create a new controller?
If I just have to create a new one, is there a best practices for how many controllers you have, etc.
Documentation says: This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.
So use $route['login'] = 'home/login';
The way I have it set up is to have the index function act as the gatekeeper: if not logged in, show a login form view, if logged in, redirect to your (protected) home page. The login function is only accessed via post, when the user submits his login/pwd, and performs the login logic.
You need add a line to your application/config/routes.php file
$route['login'] = "home/login";