Session Data, CodeIgniter Display Profile URL (w / Tank Auth)

I am developing an application in CodeIgniter and MySQL. The application includes user profiles; I use Tank Auth to register and authenticate users.

I installed a couple of users and now I want to view each user profile. I need to know:

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

2 - How to transfer user data to a view . I set up a function to extract user data (see below) and I want to transfer it to my profile - I think that userdata (in code) will represent user session data, which will include the user ID and username, one of which I will need to URL

3 - URL . I want the urls to look like this: http://example.com/users/3394 or http://example.com/users/fooy_foo . I know that I need to do something with the CI URI routing, but I'm not sure how to relate it to the results that I get from the request.

Here is 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); } } 
+4
source share
2 answers
  • I am not familiar with Tank Auth, but I would advise you to check the official page for Tank Auth. Maybe you will get a better understanding from reading about the library. The following is a guide that shows how to configure Tank Auth using CodeIgniter.

  • Looking at my code from the user controller, I see that you are transmitting the data in the correct way. You pass it as an array. In your opinion, the array element will be available as a variable. Therefore, to use the data in the view, you simply use the $userdata variable. If you want to add more data to be included in the view, you simply add another element to the $data array!

  • If you create a controller named users , you can reach it at www.example.com/users . You can then edit the index function to include the $uid parameter, which will generate the desired URL: www.example.com/users/3394 .

Example at # 3:

Suppose you have created a users controller. This will 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); } 

This way you can customize the index function. The $userid variable is defined by the URL www.example.com/users/ 1234 . This is the way URLs work in the encoder. You can read about it here .

+2
source

I think Runar answered # 2 and # 3 for you. For # 1, open application / libraries / Tank_auth.php and the function login name. 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 establishes a session. You can add more variables to be set in the session here.

+5
source

Source: https://habr.com/ru/post/1389862/


All Articles