Welcome Guest, Not a member yet? Register   Sign In
header("Location: " ... MY_Controller
#1

[eluser]Nick_MyShuitings[/eluser]
A warning to the wise that has cost me an absurd amount of debugging time.

I run a modified version of Jamie's My_controller for autoloading of views and layouts

Quote: * @package CodeIgniter
* @subpackage MY_Controller
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.txt>
* @LinK http://github.com/jamierumbelow/codeigni...controller
* @version 1.1.1
* @author Jamie Rumbelow <http://jamierumbelow.net>
* @copyright Copyright © 2009, Jamie Rumbelow <http://jamierumbelow.net>

Code:
header("Location: " . $payPalURL);
- Avoid this like the plague, and check all the sample code you may download from places like paypal and facebook. Since this may lead to views being called when you do not expect them to. (and some of us have dirty logic in our views that we should perhaps abstract out...)

It appears (and my quick tests now show) that using the header in that format will allow the rendering of the subsequent layout and views, and then redirect you.

That said, there are a thousand ways to avoid this with restructuring for this specific case... but the point is to be careful with header location. Using codeigniter redirect function works like a charm and redirects without processing the rest of the layouts and views.

This code is bad:

Code:
public function test() {
  $this->session->set_userdata('okay','ready');
  header("Location: " . $payPalURL);
  //mycontroller will call the layout and then the view;
  // if the view for some reason clears the session, or rewrites that var... you will have unexpected results.
}

versus this better code:

Code:
public function test() {
  $this->session->set_userdata('okay','ready');
  redirect($payPalURL);
  //mycontroller will NOT call the layout and then the view;
}





Theme © iAndrew 2016 - Forum software by © MyBB