Not receiving URL variables in controller with GET, codeigniter - php

I'm not receiving variables in my controller from a URL from Paypal indicating a successful transaction. They appear in the URL fine but my controller is not receiving them for some odd reason. I think the code is absolutely correct. What could be the reason?
Example:
Received URL: http://example.com/Paypal/success?tx=8FA47070HF454623K&st=Completed&amt=20%2e00&cc=USD&cm=&item_number=
Controller, PHP:
function success(){
//get the transaction data
$paypalInfo = $this->input->get();
$data['item_number'] = $paypalInfo["item_number"];
$data['txn_id'] = $paypalInfo["tx"];
$data['payment_amt'] = $paypalInfo["amt"];
$data['currency_code'] = $paypalInfo["cc"];
$data['status'] = $paypalInfo["st"];
//pass the transaction data to view
$this->load->view('paypal/success', $data);
}
I get this result for all variables:

Looks like item_number is not set. Performing a GET on it can cause an error.
You are using a class function to obtain the $_GET items.
Within the function $this->input->get() you should check if the var is available (if(isset($_GET[..]))) and otherwise return a default, so you don't run into these problems.
On the other hand, it seems that you don't submit a item_number to Pay Pal. May also be a problem for processing your order later on in the script and backtracing it to the original...
---- ALSO SEE ANSWER BELOW ----
Paypal sends POST vars...

Remember Paypal not send data via URL, So you couldn't receive data via GET method. You should try this
$paypalInfo = $this->input->post();
instead what you are using now. If it not works then use following
$paypalInfo = $_POST;
it will must work

Just processing $_POST gives you a huge security risk! Also for $_GET. Change $_POST to $_GET below to use $_GET in stead of $_POST.
I would suggest at least the following;
foreach ($_POST as $key->$value)
{
$cleankey = addslashes($key);
$paypalInfo[$cleankey] = addslashes($value);
}
Then;
$req_keys = array(
'item_number' => 'item_number',
'txn_id' => 'tx',
'payment_amt' => 'amt',
'currency_code' => 'cc',
'status' => 'st',
);
$data = array();
$error = array();
foreach($req_keys as $req_data_key=>$req_paypalinfo_key )
{
if(isset($paypalInfo[$req_paypalinfo_key]))
{
$data[$req_data_key] = $paypalInfo[$req_paypalinfo_key];
}
else
{
$error[] = "missing POST data:".$req_paypalinfo_key;
}
}
if(count($error) > 0)
{
var_dump($error);
}
else
{
$this->load->view('paypal/success', $data);
}
This will give you insight if your required info is there and also gives you some protection against SQL injection.

Related

Codeigniter Database Error Number 1048 Values show NULL even though they are NOT NULL

I have situation where codeigniter shows database Error Number 1048. It seems Values NULL but when I try to check it usign var_dump($_POST) Values are not NULL.
Controller : Jurusan.php
public function simpan()
{
$this->form_validation->set_rules('code','Kode','required|integer');
$this->form_validation->set_rules('jurusan','Jurusan','required');
$this->form_validation->set_rules('singkatan','Singkatan','required');
$this->form_validation->set_rules('ketua','Ketua','required');
$this->form_validation->set_rules('nik','NIK','required|integer');
$this->form_validation->set_rules('akreditasi','Akreditasi','required');
if($this->form_validation->run() == FALSE)
{
$isi['content'] = 'jurusan/form_tambahjurusan';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Tambah Jurusan';
$this->load->view('tampilan_home',$isi);
} else {
$this->model_security->getSecurity();
$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code'];
$data['prodi'] = $this->input->post['jurusan'];
$data['singkat'] = $this->input->post['singkatan'];
$data['ketua_prodi'] = $this->input->post['ketua'];
$data['nik'] = $this->input->post['nik'];
$data['akreditasi'] = $this->input->post['akreditasi'];
$this->load->model('model_jurusan');
$query = $this->model_jurusan->getdata($key);
if($query->num_rows()>0)
{
$this->model_jurusan->getupdate($key,$data);
} else {
$this->model_jurusan->getinsert($data);
}
redirect('jurusan');
}
}
Model : model_jurusan.php
class Model_jurusan extends CI_model {
public function getdata($key)
{
$this->db->where('kd_prodi',$key);
$hasil = $this->db->get('prodi');
return $hasil;
}
public function getupdate($key,$data)
{
$this->db->where('kd_prodi',$key);
$this->db->update('prodi',$data);
}
public function getinsert($data)
{
$this->db->insert('prodi',$data);
}
}
Here is the error shown :
Here is the database structure :
You have a wrong syntax in these lines:
$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code']; // <-- use ('code')
$data['prodi'] = $this->input->post['jurusan']; // <-- use ('jurusan')
Change this to
$this->input->post['array_key'];
this
$this->input->post('array_key');
Read : Input Class in Codeigniter
Well the problem lies in your way of accepting input parameters.
$this->input->post
is a method which accepts the variable name, not an array. So all the input parameters need to be passed as a function parameter to post method. These lines need to be altered to.
$data['kd_prodi'] = $this->input->post('code');
$data['prodi'] = $this->input->post('jurusan');
$data['singkat'] = $this->input->post('singkatan');
$data['ketua_prodi'] = $this->input->post('ketua');
$data['nik'] = $this->input->post('nik');
$data['akreditasi'] = $this->input->post('akreditasi');
Hope this solves the problem.
EDIT:
You did a var_dump($_POST) which works as it is supposed to and it will read the values of the post parameters. So either you fetch the parameters from $_POST array, or you use the $this->input->post() method. But I would suggest using the $this->input->post() method as it provides additional sanitization such as xss attack handling etc, which could be turned on an off from the config.
i have tried your code...it works. I think there some mistakes in your <input> tags, You must use <input name=""> not <input id=""> or something else. Hope it can help you out
You are try to get value from post is wrong. You should use at this way
$_POST['array value'];

Additing two array variable in codeigniter

I am building a system where i need to update the value in the database field but before doing that i need to take the current value in the database then adding it to the current value the following is a code from the controller.
public function transfer_amount(){
$email = $this->input->post('view');
$this->load->model('user_model');
$data['user_balance'] = $this->user_model->fetch_balance($email);
$data['balance'] = $this->input->post('amount');
$data['total'] = $this->math->add($data['balance'],$data['user_balance']);
$data = array(
'balance' => $data['total'],
);
if($this->user_model->transfer_amount($data,$email)== true){
$this->load->view('layer_service/success',$data);
}
else
{
$this->load->view('layer_service/unsuccessfull',$data);
}
}
}
then the code in the module that fetch the current balance from the database is as following.
function fetch_balance($email){
$this->db->select('balance');
$this->db->from('tbl_users');
$this->db->where('email', $email);
$query = $this->db->get();
$result = $query->result();
return $result;
}
not sure but look at this part - don't call it $data because i think thats messing you up later
for example call it $balance
$balance = array(
'balance' => $data['total'],
);
// pass the $balance to your model method
if($this->user_model->transfer_amount($balance,$email) == true){
// keeping $data here to pass to the view
$this->load->view('layer_service/success',$data);
}
getting the balance should be wrapped in an if in case it fails
if( ! $data['user_balance'] = $this->user_model->fetch_balance($email) )
{
$this->_showNoUserFor($email) ;
}
another suggestion:
$email = $this->input->post('view');
validate the email first before sending it to your database table. codeigniter form validation library works really well.
======
EDIT ok this part
$data['total'] = $this->math->add($data['balance'],$data['user_balance']);
means that you have a model called math with a method called add()
so if you don't have that you would just use php math which is very simple
$data['total'] = $data['balance'] + $data['user_balance'] ;
of course this assumes everything has been validated first so you are actually adding together two numbers.

Check if POST data was sent to page

I am trying to check if POST data has been sent to a page. A quick Google search turned up nothing.
if(postdataisSent)
{
//do this
}
else
$items = Gamefarm::where('roost_hen', '=', 1)->paginate(6);
return View::make('gamefarms/index',compact('items'));
You can use if ( Input::has('parameter') ) to check for the existence of a certain parameter in the POST, or you can pass a default into the function, and then test if it's there.
$parameter = Input::get('parameter', false);
if ($parameter)
{
// do something with the data
}
else
{
// it's not present in the POST
}
To check for the presence of any data at all:
$data = Input::all();
if (count($data) > 0)
{
// there is data in the POST
}
else
{
// there is no data in the POST
}
Note - You can access the data from any HTTP verb (GET, POST etc) using the same Input::get('data')

Using flashdata while posting same controller twice in Codeigniter

I am trying to submit a EDIT form which edits Users Academics Details,
These Details have unique id in DB and my Code in Short Looks like below :
class edit extends ci_controller
{
function user_academics($id = NULL)
{
if(isset($id) == FALSE) //if link is ./edit/user_academics
{
$id = NULL;
$link = site_url('profile');
show_error("Invalid Page Request! <a href='$link' Go to Profile </a>");
}
$user_id = $this->session->userdata('user_id');
$data['fill'] = $this->edit_model->get_user_academics($id);
if($user_id != $data['fill']['user_id']) // check if logged in user is accessing his record or others
{
$link = site_url('profile');
show_error("This is an Invalid Request ! <a href='$link'>Go to Profile </a>");
}
else // actual work starts here
{
$this->session->set_flashdata('ua_id',$id); // update_academics will get this data
$this->load->view('edit/edit_3_view',$data);
}
}
function update_academics()
{
$ua_id = $this->session->flashdata('ua_id'); // flash data used here .
if( !$ua_id )
{
show_error('Sorry, This request is not valid!');
}
$academics = array(
// All post values
);
$this->edit_model->update_user_academics($academics,$ua_id);
//print_r($academics);
redirect('profile');
}
}
Now the problem is
- If I open two different records to edit, then It will set only one Session Flash value.
- And No matter what I edit , the existing values of the last flash value gets updated.
Please Suggest me another way or Correct me if I am wrong in above code . Thanks
save that flashdata in array, like:
$myArr = array('value 1', 'value 1');
//set it
$this->session->set_flashdata('some_name', $myArr);
And in view:
$dataArrs = $this->session->flashdata('some_name');
//loop thru $dataArrs to show the flashdata
Flash data is simply like variable which is available only in next request, you can bypass this behavior by using two different keys with record id in it, so that when you use flash data for showing message you can access key with particular record id.

CakePHP API PUT with JSON input

I am building an API using CakePHP.
I want to use PUT from my mobile application to update data. The format is JSON as input but $this->data seems to be null.
I call this url (as specified in the docs) from my application:
/recipes/123.json
And in my "recipes" (or whatever) I have the following controller:
function edit($id = null) {
$this->User->id = $id;
if (empty($this->data)) {
$this->data = $this->User->read();
$message = array('StatusCode' => 999, 'ERROR' => "");
} else {
if ($this->User->save($this->data)) {
$message = array('StatusCode' => 200, 'ErrorCode' => "");
} else {
$message = array('StatusCode' => 400, 'ErrorCode' => "UnknownError");
}
}
$this->set(compact("message"));
$this->set('albums', $this->User->Album->find('list'));
}
I correctly receive the JSON response in my application however I get the 999 error - meaning that $this->data is empty.
In my add function in my controller where it receives JSON using POST - the $this->data gets assigned correctly. And oh ye, if I use POST instead of PUT in my edit - the $this->data gets set, but I cannot save the data..
So.. how do I do this ? :S
Insert from luchomolina's link
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-xml-or-json-data
//Get JSON encoded data submitted to a PUT/POST action
$data = $this->request->input('json_decode');
and you get your object.
$data->Model->field ...
I haven't tested it, but I think your data is in $this->request->input() or $this->request->data()
More info:
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-xml-or-json-data
http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::data

Categories