• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a function on each page

#1
[eluser]Grim[/eluser]
Hey all started using CI a few weeks ago, and is swapping out old code with it :-) But a small snag I've hit is that before I used to have a PHP file called startsession.php which is called on every page to start the session and set vars if there is cookies available.
Code:
<?php
  session_start();

  // If the session vars aren't set, try to set them with a cookie
  if (!isset($_SESSION['user_id'])) {
    if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
      $_SESSION['user_id'] = $_COOKIE['user_id'];
      $_SESSION['username'] = $_COOKIE['username'];
    }
  }
?>

So I have a controller named auth and have a function called auto_login which does the same thing but how do I call it on every page ?

Sorry If its a little redundant :blank:
Thanks In Advance :-)

#2
[eluser]rogierb[/eluser]
you can do this by extending the CI_controller

Read up on it at http://ellislab.com/codeigniter/user-gui...asses.html

You extend the controller so you can load your own functions.
Dont forget to call your new controller (MY_controller) instead of the CI_Controller

#3
[eluser]Grim[/eluser]
[quote author="rogierb" date="1347623862"]you can do this by extending the CI_controller

Read up on it at http://ellislab.com/codeigniter/user-gui...asses.html

You extend the controller so you can load your own functions.
Dont forget to call your new controller (MY_controller) instead of the CI_Controller[/quote]

Thanks! It Worked and sorry for the late reply. And also, I extended the controller. And is it okay to use $this->data->varname directly in the view file. Instead of passing it through $data['varname'] ?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.