session data, display profile URLs in CodeIgniter (w/Tank Auth)

3.8k views Asked by At

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);
    }
}
2

There are 2 answers

4
Runar Jørgensen On BEST ANSWER
  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.

1
Ninja On

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.