After redirect session lost in codeigniter - codeigniter

The session value lost after the redirect. I m redirecting from pay_order method to do_payment method. when I print the session value in do_payment method its return false. just simple calling the do_payment method display the session value. Please help what is the problem with redirect..?
function pay_order($order_id)
{
$this->load->helper('url'); //loading url helper
$this->load->library('session');//loading session lib
$this->load->library('cart'); //loading cart lib
$this->load->helper('form');//loading form helper
$output = $this->cart->contents();// getting data from cart.
$output = $this->sort_array($output);// sorting the array
$list['data'] = $output;
$list['order_id'] = $order_id;
$this->session->set_userdata('abc', $list);// setting the session
redirect('checkout/do_payment'); // redirecting to do_payment
}
function do_payment()
{
$this->load->helper('url'); //loading url helper
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
var_dump( $arr );// return false value.
//$this->load->view('order/pay_through_gateway');
}
when I redirect from pay_order method the session is not available in do_payment method. why?

There is a problem, check the line in the method pay_order() where you are setting the session.
$list['order_id'] = $order_id
$this->session->set_userdata('abc', $list);// setting the session
Before that line you have missed semicolon. And everything looks fine.
Update:
There is no error as I think, check whether you may have null value while you are storing the values in session. And check the values by following code in do_payment() method.
function do_payment(){
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
print_r( $arr );// print the $arr content
}

Change this:
$list['order_id'] = $order_id
To this:
$list['order_id'] = $order_id;
You have a semicolon missing. You do not see any errors because of the redirection.

Related

CodeIgniter Getting Not Found Image Path In Session

I'm implementing redirect to previous page after login and logout.
So in each methods of controller I've saved session like as follow.
$this->session->set_userdata('previous_page', current_url());
And after successful login and logout, I'm calling a library method as follows.
function redirect_to_previous_url() {
$url = base_url();
if($this->_CI->session->userdata('previous_page')) {
// Get previous_url
$url = $this->_CI->session->userdata('previous_page');
$this->_CI->session->unset_userdata('previous_page');
}
return $url;
}
But Its redirecting to base_url of the site. After checking the session value Its showing not found image path but not what I've saved it before.
I'm not able to find out what is the problem behind this.
Please help me to rectify and the work would be appreciated
Try this..
function redirect_to_previous_url() {
$url = base_url();
if($this->_CI->session->userdata('previous_page')) {
// Get previous_url
$url = $this->_CI->session->userdata('previous_page');
$this->_CI->session->unset_userdata('previous_page');
return $url;
}
return $url;
}
I would ensure the session was set. Like this;
if($this->_CI->session->userdata('previous_page')) {
show_error('The session is set');
}
If you don't see the error, the session isn't set. Then you know this isn't where the problem lies.
No need to store Previous URL in session.
In core php you can get previously visited URL in following server variable
$_SERVER['HTTP_REFERER'];
Same can be achieved in CodeIgniter as
$this->load->library('user_agent');
echo $this->agent->referrer();

handling database error with ajax request codeigniter

I am intentionally making error i.e. using one of the coulmn's name wrong to learn how to handle the error with ajax call with codeigniter.
Last Comment in controller function at the end is my question/problem
My AJX Code is following. I am using ajaxform plugin. It shows me all response from controller perfectly but problem is only I am unable to get response from model in controller while using ajax call that is little weird
$('#myForm').ajaxForm
({
success: function(responseText)
{
if(responseText != "1")
$('#feedback').html(responseText);
}
});
Following is snapshot of exact error which i can see in console but unable to get in controller and hence in view. It describes complete error i.e unknown column in given query, but i captured only upper portion.
My model function is below
public function updateItem($id, $data, $tbl)
{
$this->db->where('id', $id);
$r = $this->db->update($tbl, $data);
if($r)
return $r;
else
{
$r = $this->db->_error_message();
return $r;
}
}
My controller function code
public function upadteme()
{
$r = $this->ajax_model->updateItem($uid, $data, 'users');
echo $r." -- "; // Unable to get this echo working
//when using ajaxcall (calling controller through ajax) otherwise fine
}
It looks like the class will not populate _error_message if db_debug is on which it appears to be by default.
In config/database.php, set
$db['default']['db_debug'] = FALSE;
and try again.

Catch url in CodeIgniter

I am getting url like http://localhost/webpt/ipn/checkout/?token=EC-2YD51592ET0280122&PayerID=VNH3J2KQEK8AS and want to catach in my controller. in my controller code
function checkout($token = array()) {
echo"<pre>";
print_r($token);
echo"</pre>";
}
but it show empty array.
Ok I just set $config['uri_protocol'] = 'AUTO'; in config.php & use
echo ($_GET['token']); or print_r($this->input->get()); // print all the get values & it work fine thanks to all.
you can grab value of token by $this->input->get('token'); since it is passed in url following a question mark.
if you mean that you want to catch the param token,
you have two options :
format your url to be like
http://localhost/webpt/ipn/checkout/nouman
and you catch it in your controller like this :
function checkout($token) {
echo $token;
}
or use $this->input->get('token')
function checkout() { // http://localhost/webpt/ipn/checkout/?token=8767&param2=333
echo $this->input->get('token'); // echo the name param
print_r($this->input->get()); // print all the get values
}

Magento - Last event name that is executed after a page is loaded

In my custom module i have set a flag in session variable, and in controller_action_layout_render_before event, checking if the flag is set in then append a custom content.
I want to unset the flag in session variable after the page is loaded.
How can it be done.
Is there any event which is executed at the end when the page is getting loaded?
The
controller_front_send_response_after
event should work for you. Temporarily adding some logging to app/Mage.php
public static function dispatchEvent($name, array $data = array())
{
//Mage::Log($name);
file_put_contents('/tmp/event.log',"$name\n",FILE_APPEND);
Varien_Profiler::start('DISPATCH EVENT:'.$name);
$result = self::app()->dispatchEvent($name, $data);
#$result = self::registry('events')->dispatch($name, $data);
Varien_Profiler::stop('DISPATCH EVENT:'.$name);
return $result;
}
can get you a list of events that have fired for a particular request, which is usually enough to track down the event you're looking for yourself.
I've you're looking to store a variable only for the life of a single request, you might consider using Magento's registry. Basically, you insert a value with Mage::register('some_variable_name',$variable); and retrieve it in some other context in the same request with Mage::registry('some_variable_name');
Refer to #Alan Storm's answer here for more information.

CodeIgniter - showing original URL of index function?

I'm not sure if I'm approaching this fundamentally wrong or if I'm just missing something.
I have a controller and within it an index function that is, obviously, the default loaded when that controller is called:
function index($showMessage = false) {
$currentEmployee = $this->getCurrentEmployee();
$data['currentEmp'] = $currentEmployee;
$data['callList'] = $currentEmployee->getDirectReports();
$data['showMessage'] = $showMessage;
$this->load->view('main', $data);
}
I have another function within that controller that does a bulk update. After the updates are complete, I want the original page to show again with the message showing, so I tried this:
/**
* Will save all employee information and return to the call sheet page
*/
function bulkSave() {
//update each employee
for ($x = 0; $x < sizeof($_POST['id']); $x++) {
$success = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
}
$this->index($success);
}
What is happening is that the original page is accessed using:
localhost/myApp/myController
after the bulk update it is showing as:
localhost/myApp/myController/bulkSave
when I really want it to show the url as the index page again, meaning that the user never really sees the /bulkSave portion of the URL. This would also mean that if the user were to refresh the page it would call the index() function in the controller and not the bulkSave() function.
Thanks in advance.
Is this possible?
You are calling your index() funciton directly, within bulkUpdate() hence the uri does not change back to index because you are not making a new server request, you are just navigating within your controller class.
I usually use the same Controller function for tasks like this, directing traffic based on whether or not $_POST data has been passed or not like this...
function index() {
if($_POST) {
//process posted data
for ($x = 0; $x < sizeof($_POST['id']); $x++) {
$data['showMessage'] = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
}
}
else {
//show page normally
$data['showMessage'] = FALSE;
}
//continue to load page
$currentEmployee = $this->getCurrentEmployee();
$data['currentEmp'] = $currentEmployee;
$data['callList'] = $currentEmployee->getDirectReports();
$this->load->view('main', $data);
}
Then if it is a form that you are submitting, just point the form at itself in your view like this...
<?= form_open($this->uri->uri_string()) ?>
This points the form back at index, and because you are posting form data via $_POST it will process the data.
I usually do a redirect to the previous page as it prevent users to refresh (and submit twice) their data.
You can use the redirect() helper function of CI.
http://codeigniter.com/user_guide/helpers/url_helper.html (at the bottom)

Resources