A client requires that all GET/POST requests are logged and stored for 90 days for their applicaiton. I have written a HOOK which seems to record some of the GETS / POSTS but there is less data than I would expect. For example, when submitting form data, the entries don't seem to be put in the log. Has anyone written something similar which works?
Here is my version thus far:
class Logging {
function __construct() {
$this->CI =& get_instance();
}
function index() {
$this->CI->load->model('Logging_m');
$this->CI->load->model('Portal_m');
//get POST and GET values for LOGGING
$post = trim(print_r($this->CI->input->post(), TRUE));
$get = trim(print_r($this->CI->input->get(), TRUE));
$this->CI->Logging_m->logPageView(array(
'portal_id' => $this->CI->Portal_m->getPortalId(),
'user_id' => (!$this->CI->User_m->getUserId() ? NULL : $this->CI->User_m->getUserId()),
'domain' => $_SERVER["SERVER_NAME"],
'page' => $_SERVER["REQUEST_URI"],
'post' => $post,
'get' => $get,
'ip' => $this->CI->input->ip_address(),
'datetime' => date('Y-m-d H:i:s')
));
}
}
This data is stored in a model called 'Logging_m' which looks like this:
<?php
class Logging_m extends CI_Model {
function __construct() {
parent::__construct();
}
function logPageView($data) {
$this->db->insert('port_logging', $data);
}
}
/* End of file logging_m.php */
/* Location: ./application/models/logging_m.php */
As mentionned by Patrick Savalle you should use hooks. Use the post_controller_constructor hook so you can use all other CI stuff.
1) In ./application/config/config.php set $config['enable_hooks'] = TRUE
2) In ./application/config/hooks.php add the following hook
$hook['post_controller_constructor'] = array(
'class' => 'Http_request_logger',
'function' => 'log_all',
'filename' => 'http_request_logger.php',
'filepath' => 'hooks',
'params' => array()
);
3) Create the file ./application/hooks/http_request_logger.php and add the following code as example.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Http_request_logger {
public function log_all() {
$CI = & get_instance();
log_message('info', 'GET --> ' . var_export($CI->input->get(null), true));
log_message('info', 'POST --> ' . var_export($CI->input->post(null), true));
log_message('info', '$_SERVER -->' . var_export($_SERVER, true));
}
}
I've tested it and it works for me (make sure you have logging activated in your config file).
Related
I'm currently using the Omnipay extension library to simply handle my Rabobank omnikassa transactions. Now when i use the code below i get a selection of all credit card methods but IDEAL and MINITIX are not listed on the page. Not sure what i'm doing wrong, first time i'm using an external library to handle my payments. Rabobank Omnikassa should display all available payment methods on default.
The Library:
https://github.com/thephpleague/omnipay-rabobank
My Code: iDealController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Omnipay\Omnipay;
class iDealController extends Controller
{
public function loadPage()
{
$sOrderId = 'WEB' . time(); // This should be unique - Order id
$sTransactionReference = $sOrderId . date('His'); // This should be unique - Identifier of transaction
$amount = 10.00;
$gateway = Omnipay::create('Rabobank');
$request = $gateway->purchase(array(
'testMode' => true,
'merchantId' => '002020000000001',
'keyVersion' => '1',
'secretKey' => '002020000000001_KEY1',
'amount' => $amount,
'returnUrl' => 'http://localhost:8888/',
'automaticResponseUrl' => 'http://localhost:8888/',
'currency' => 'EUR',
'transactionReference' => $sTransactionReference,
'orderId' => $sOrderId,
'customerLanguage' => "EN"
)
);
$data = $request->getData();
$response = $request->sendData($data);
if ($response->isSuccessful()) {
// payment was successful: update database
print_r($response);
} elseif ($response->isRedirect()) {
// redirect to offsite payment gateway
$response->redirect();
} else {
// payment failed: display message to customer
echo $response->getMessage();
}
return view('omnikassa');
}
}
When i add: 'PaymentMethod' => 'IDEAL' to the request array it gives the following error: Technical problem : code=03 message=None of the merchant's payment means is compliant with the transaction context. So definitely something is going wrong.
I'd like to write a line in the log with the duration every controller method takes to run (public and private). Originally I set this up as one would imagine, very manually, subtract start seconds from end seconds and write log, with that code at the start and end of every method.
Keeping as DRY as possible, I'm wondering if someone knows a way to abstract this so I don't have to write the same code in every single method.
This is using Codeigniter.
It appears you don't even need the profiler class; can just use benchmark class:
application/config/config.php:
$config['enable_hooks'] = TRUE;
application/config/hooks.php:
$hook['post_controller_constructor'] = array(
'class' => 'MyProfiler',
'function' => 'start_profiler',
'filename' => 'profiler.php',
'filepath' => 'hooks',
);
// or 'post_system' to benchmark page render too
$hook['post_controller'] = array(
'class' => 'MyProfiler',
'function' => 'stop_profiler',
'filename' => 'profiler.php',
'filepath' => 'hooks',
);
application/hooks/profiler.php:
class MyProfiler {
var $CI;
function __construct(){
$this->CI =& get_instance();
}
function start_profiler() {
$this->CI->benchmark->mark('code_start');
}
function stop_profiler() {
$this->CI->benchmark->mark('code_end');
echo $this->CI->benchmark->elapsed_time('code_start', 'code_end');//or log
}
}
https://www.codeigniter.com/user_guide/general/hooks.html
https://www.codeigniter.com/user_guide/libraries/benchmark.html
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I have a login form that is being created in the view. Then in my controller I'm trying to load my model but it doesn't seem to load. Looked up a few answer here on stackoverflow and almost everybody says autoload model, import the database library,etc. I did that but I'm still getting the error
Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\\project\application\controllers\login.php on line 11
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$load
Filename: controllers/login.php
Line Number: 11
login_view
<?php
$loginEmail = array('placeholder' => "Email", 'name' => "loginEmail");
$loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword");
$loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen");
$loginForgot = array('name' => "loginForgot", 'class' => "link", 'value' => "Wachtwoord vergeten?");
echo form_open('login/login', array('class' => 'grid-100 formc'));
echo form_input($loginEmail);
echo form_password($loginPassword);
echo form_submit($loginSubmit);
echo form_submit($loginForgot);
?>
login_controller
<?php
Class Login extends CI_Controller{
function index(){
$data['content'] = 'login_view';
$this->load->view('templates/template', $data);
}
function login(){
$this->load->model('login_model');
$query = $this->login_model->validate();
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'loggedin' => true
);
$this->session->set_userdata($data);
redirect('profile/myprofile');
}
}
}
?>
login_model
<?php
Class Login_model extends CI_Model{
function validate(){
$this->db->where('email', $this->input->post('loginEmail'));
$this->db->where('password', md5($this->input->post('loginPassword')));
$query = $this->db->get('tbl_users');
if($query->num_rows == 1){
return true;
}
}
}
?>
autoload.php
$autoload['libraries'] = array('database', 'session');
What am I missing here?
You don't have a constructor, in your login controller:
public function __construct() {
parent::__construct();
}
According to CodeIgniter docs
If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::__construct();
Refer CI Constructor for more.
I am trying to integrate the following code into my project. it is held in a library
function do_std_login($email, $password) {
$CI =& get_instance();
$login = $CI->users_model->login($email, md5($password));
if($login){
$session_array = array(
'user_id' => $login->user_id,
'name' => $login->name,
'type' => 'Standard'
);
$CI->session->set_userdata($session_array);
// Update last login time
$CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);
return true;
} else {
$this->errors[] = 'Wrong email address/password combination';
return false;
}
}
I am calling it this way:
$login = $this->jaclogin->do_std_login($this->input->post('email'),$this->input->post('password'));
but when I run it I get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$users_model
Filename: libraries/jaclogin.php
Line Number: 45
I have check I am do load the correct library in the codeigniter autoload file.
Any Ideas?
Thanks
Jamie Norman
Using your CI instance, load your model explicitly in the library like so..
function do_std_login($email, $password) {
$CI =& get_instance();
//--------------
$CI->load->model('users_model'); //<-------Load the Model first
//--------------
$login = $CI->users_model->login($email, md5($password));
if($login){
$session_array = array(
'user_id' => $login->user_id,
'name' => $login->name,
'type' => 'Standard'
);
$CI->session->set_userdata($session_array);
// Update last login time
$CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);
return true;
} else {
$this->errors[] = 'Wrong email address/password combination';
return false;
}
}
I'm following the blog tutorial on CakePHP website, but validation doesn't work and I don't understand why, because I'm using the blog tutorial code.
I'll report directly from my files anyway...
MODEL
class Post extends AppModel
{
var $name = 'Post';
var $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
}
CONTROLLER
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function view($id) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
function add() {
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
}
VIEW (add view)
<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>
When I reach the /posts/add page and then click to Save without any input text it doesn't reload with error; it inserts the empty data into db and then redirects me with the message "Your post has been saved".
Why doesn't validate itself? I've read into docs that it's not necessary to call validate(); save() is enough.
Any ideas about this?
I found the error: I was naming the model files capitalizing them. So, I had a Post.php instead of a post.php.
CakePHP didn't find my models and so it was using its "generated" models.