codeigniter - test controller/method not working - php

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?

Related

In one CodeIgniter controller session is empty, in other all works

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!

codeigniter 403 override status

i know this question has been asked before. But completely there is no any good answer for this problem.
i was using CSRF security and completely know how to use it. if the token is not valid, the csrf will return me like
An Error Was Encountered
The action you have requested is not allowed.
and in Security.php there is a function how the status was set
public function csrf_show_error()
{
show_error('The action you have requested is not allowed.', 403);
}
kindly, i need to custom the the error page.
like in config if we set $route['404_override'] = 'Page_error';
it would show the Page_error what we have set before.
is there any case how to override the 403 ?
thank you
All built-in error pages use templates, which are located under application/views/errors/ - you can edit those to change the appearance.
The CSRF error page in particular uses the 'error_general' template.
Can't use a controller for anything but the 404 page though.
Here is how I handled this. I created a MY_Security.php file in application/core with this PHP code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Security extends CI_Security{
public function __construct(){
parent::__construct();
}
public function csrf_show_error(){
if((!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'))
exit(json_encode(array(//put your array elements here)));
else
show_error('The action you have requested is not allowed.', 403);
}
}
?>

CodeIgniter redirect from a view page to another view page

I'm currently using CodeIgniter and trying to create two form.
One form for username/password which is already done. (So we assume that the user has an account).
One form for create an account.(Not finish yet).
I load the first link using my controller like
$this->load->view('Login_view');
The first problem I encountered is to find a way to load a view file from another view file ?
Once in my 'Login_view' file I tried this way for load another view file by this way:
<p>New account</p>
or this way
<p>New account</p>
and the last one
<p>New account</p>
Otherwise neither of those works. So is it possible to open another page from a view file or the only way is to get back into my controller class and open the desire view file ? Or perhaps is it the wrong way to do it ?
This is the beginning of my controller code (I suppose that the beginning is reasonable)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Verifylogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->helper->helper('url');
$this->load->model('user','',TRUE);
}
Thanks
You can simply make registration function in controller and load view from it, like
function registration()
{
$this->load->view('New_account');
}
and in login view add link for registration
<a title="New account" href='<?php echo base_url ('controller_name/registration'); ?>'>New account</a>
Using Active record
echo anchor('verifylogin/user', 'New account', 'title="New account"');
Make sure you upload
$this->load->helper('url');
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Verifylogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->helper->helper('url');
$this->load->model('user','',TRUE);
}
function user()
{
$this->load->view('New_account');
}
}
You can use the redirect method in CodeIgniter to switch between the page views. Then for each page form add the <?php echo site_url('controller_class/Controller_funtion');?> . Then You have to make 2 controller functions,
1. For the $this->load->view('Login');
2. For the actions what should happen when the form is submitted.
As an example,
//For the login page load
function Login_load(){
$this->load->view('login');
}
function LoginLoad(){
if($this->input->post('submit')){
//Things should happen when the form is submitted
}
}
When you need to change the view just type on the current function you're in,
redirect('Fuction_class/Login_Load', refresh);

Codeigniter RESTful API Server - XML Error?

I've read the following topics/tutorials:
Codeigniter RESTful API Server
http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814
https://github.com/chriskacerguis/codeigniter-restserver
And I still can't figure out why I'm with route problems and XML problems.
My webservice controller is in the folder controllers/api/webservice.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/RESTful/REST_Controller.php';
class webservice extends REST_Controller{
function get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
In the tutorials there is no need to add routes, and in order to receive the XML page error I needed to add the following route or it wouldn't work:
$route['api/webservice/get'] = 'api/webservice/get';
My structure folder of codeIgniter:
> application
> config
rest.php (did not change anything from the GitHub download)
> controllers
> api
webservice.php
key.php
> libraries
> RESTful
REST_Controller.php (changed line 206 to: $this->load->library('RESTful/format');)
format.php
From the tutorial, the following link works without routes:
http://localhost/testRestful/index.php/api/example/users
Mine only works with route
http://localhost/myproject/index.php/api/webservice/get
And I receive the following error:
It does not say anything else. I can't figure out which file is the error talking about.
you cannot write get function if you use REST_Controller.
give that function name test_get.
class webservice extends REST_Controller{
function test_get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
Now you can access the page with this link
http://localhost/myproject/index.php/api/webservice/test
_get and _post is added end of the function to detect either it is get request or post requerst.

Controller not executed: Error 404

I use CI v2.0.3 , xampp windows 1.7.0 , and I renamed the CI folder to "Hello"
I created a Blog.php at application/controller.
The content of Blog.php is:
<?php
if(!defined('BASEPATH')) exit('No Direct Script Access Allowed');
class Blog extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
echo "Haloo.. CI pertama";
}
}
I want to access localhost:8080/hello/index.php/blog or localhost:8080/hello/index.php/Blog but both of them still show 404 not found.
This is what I expect instead: "Haloo.. CI pertama".
If your CI folder is named 'Hello' then you access it like this:
http://localhost:8080/Hello/index.php/blog
Whatch out! the folder name is case sensitive so /hello/index.php/blog will not work
The URL you have entered isn't correct if you haven't enabled the Rewrite module for Apache. For your settings considering the folder name also try:
http://localhost:8080/Hello/index.php?/blog
Edit: Folder name added
You need to render a view in your index action.
Your views are located in application/views. So you'll have to create a file called index.php there in which you'll put Hello World. And then you'll add this to your function:
$this->load->view('index');
Hope this helps.

Categories