4

I'm developing an app in CodeIgniter and MySQL. The app include user profiles; am using Tank Auth to register and authenticate users.

I've setup a couple of users and now want to view each user's profile. I need to know:

1 - How to add custom session data into Tank Auth. I have an idea of how the code should look (http://codeigniter.com/user_guide/libraries/sessions.html), but am not sure where the code should go in the auth controller, which is rather extensive - https://github.com/ilkon/Tank-Auth/blob/master/application/controllers/auth.php.

2 - How to pass user data through to a view. I've setup a function to retrieve user data (see below) and want to pass it through to my profile view--I'm thinking that userdata (in the code) will represent the custom session data, which will include the user's id and username, one of which I'll need for the URL.

3 - URLs I want the URLs to look like this: http://example.com/users/3394 or http://example.com/users/fooy_foo. I know I need to do something with CI URI routing, but am not certain how to tie it in with the results I get from the query.

Here's the code from the User controller {

function index()
    {    
    $id = $this->tank_auth->is_logged_in('id');

    $this->db->where('id', $id);
    $q = $this->db->get('user');
    $data['userdata']=$q;

    $this->load->view('user_view', $data);
    }
}
Runar Jørgensen
  • 564
  • 3
  • 13
chowwy
  • 1,126
  • 8
  • 26
  • 45

2 Answers2

5

I guess Runar has answered #2 and #3 for you. For #1, open application/libraries/Tank_auth.php and the function name login. You will see these lines of code:

$this->ci->session->set_userdata(array(
                                       'user_id'       => $user->id,
                                       'username'      => $user->username,
                                       'status'        => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
                                ));

set_userdata sets the session. You can add more variables to be set in the session here.

Ninja
  • 5,082
  • 6
  • 37
  • 59
  • Thank you for this. I could only accept one answer, but I upvoted you for this. I was looking in the auth controller, which is why I didn't see it. THANK YOU!! – chowwy Jan 08 '12 at 19:50
2
  1. I'm not familiar with Tank Auth, but I'd advice you to check out the official page for Tank Auth. Maybe you'll get a better understanding from reading about the library. Here's a tutorial that shows how to set up Tank Auth with CodeIgniter.

  2. By looking at your code, from the user controller, I see that you're passing data the correct way. You're passing it into the view as an array. In your view the array element will be available as a variable. So to use the data in the view you simply use the variable $userdata. If you'd like to add more data to include in the view, you simply add another element to the $data array!

  3. If you create a controller named users you will be able to reach it at www.example.com/users. You can then edit your index function to include a parameter $uid which will generate your desired url: www.example.com/users/3394.

Example on #3:

Lets say you have created the users controller. This would then be your index() function:

function index($userid) {
  // You should probably have a model here that retrieves information
  // about the user based on the userid
  $data['user'] = $this->User_model->getUserInformation($userid);
  $this->load->view('users', $data);
}

That's one way you could set up your index function. The $userid variable is defined by the url www.example.com/users/1234. That's the way urls work in codeigniter. You can read more about it here.

Runar Jørgensen
  • 564
  • 3
  • 13
  • Thanks for your response. I've checked the Tank Auth docs and wasn't able to find info on adding custom session data. But could you give me an example of #3? How would you include a $uid parameter to get the URL? – chowwy Jan 08 '12 at 04:11
  • Btw. I found a tutorial on how to set up tank auth with codeigniter. It's a year old, but will probably be useful for your purpose. http://expertnotfound.wordpress.com/2011/01/20/setting-up-tank-auth-2/ – Runar Jørgensen Jan 08 '12 at 04:26
  • Thanks, I've already got Tank Auth setup and working. I looked through the code and couldn't find which variables were included in the session data. That's why I asked the first part of the question. – chowwy Jan 08 '12 at 14:58
  • It was already working before I posted the question. I was trying to find out how to render data. I selected your answer for your response to questions #2 and #3. Thank you. – chowwy Jan 08 '12 at 19:49