0

I am using CodeIgniter and have a login system. I am still fairly new with php and I am wondering once the user is logged in, how I can access his/her data to use throughout the site? Right now I am trying to make it say welcome "username" but I dont know where the variables come from and how I should use them. Right now in my home_view.php I have:

    <div class="main">
    <h2>Welcome <?php echo $username; ?>!</h2>
</div>

It throws this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: username

Filename: views/home_view.php

Line Number: 22

Can someone tell me how to do this?

2
  • Have you read the user guide? I highly recommend you read the introduction tutorial. This will guide you through the basics of retrieving and setting data to/from the database, and displaying it in views: ellislab.com/codeigniter/user-guide/tutorial/index.html Commented Sep 17, 2014 at 18:18
  • 1
    You may also consider working with just PHP basics (without framework) so you get a good understanding for PHP syntax and such before you jump into a framework like CodeIgniter. As your question stands right now, it is too broad, in that you are basically asking as how to use CodeIgniter. Commented Sep 17, 2014 at 18:21

2 Answers 2

1

Question: How do you access a logged-in user's data for use throughout the site?

The logged-in user specific data is usually stored in a session. For codeigniter, you can access session data using $this->session->userdata('item');, or for example $this->session->userdata('username');.

However, if you are using a specific login system it depends on the login system that you are using.


Custom Login System

After receiving the login information from a login form, pass the data to the session using the following:

$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => '[email protected]',
                   'logged_in' => TRUE
               );

$this->session->set_userdata($newdata);

Then refer to the username with the following:

$this->session->userdata('username');

See Adding Custom Session Data in the Codeigniter Documentation.


Login System Example

A popular codeigntier authentication library is Tank-Auth.

When using Tank-Auth, after a user is logged in, you can call $this->tank_auth->get_username() (which calls $this->ci->session->userdata('username'), as mentioned above). This can then be stored and passed to a view.

In Tank-Auth you will see:

application/controller/welcome.php

$data['user_id']    = $this->tank_auth->get_user_id();
$data['username']   = $this->tank_auth->get_username();
$this->load->view('welcome', $data);

application/views/welcome.php

Hi, <strong><?php echo $username; ?></strong>! You are logged in now.
Sign up to request clarification or add additional context in comments.

Comments

0

ok when user is logged in you need to store his or her data to session . when you verify user get his or her data and store that in session.

$data = array(
                   'username'  => 'johndoe',
                   'email'     => '[email protected]',
                   'logged_in' => TRUE
               );

$this->session->set_userdata('logged_info', $data );

and then you can get it any where like

$userdata = $this->session->userdata('logged_info');

1 Comment

This question was answered hours ago, isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.