Welcome Guest, Not a member yet? Register   Sign In
Change content on a page if user is logged/not logged in
#1

[eluser]thirteen[/eluser]
hi everyone,
i'm working on a codeigniter project where users sign up first and then log in to upload files. i'd like to know if it's possible to modify content on a page depending on whether a user is logged in or not.

for example, the homepage has a sign-up form and a sign-in form. but after the user signs in, these forms should not show up. instead, the upload form should be shown any thoughts? thanks
#2

[eluser]jedd[/eluser]
Hi thirteen and welcome to the CI forums.

I'm guessing you're using your own authentication system? If so, you probably have a simple is_logged_in() style function somewhere. You can have that in a library, model, or [url="/wiki/MY_Controller"]MY_Controller[/url] so that it's accessible from everywhere.

Is this what you're doing?

[quote author="thirteen" date="1262527448"]
for example, the homepage has a sign-up form and a sign-in form. but after the user signs in, these forms should not show up. instead, the upload form should be shown any thoughts? thanks[/quote]

Have a read of this thread: [url="http://ellislab.com/forums/viewthread/110969"]http://ellislab.com/forums/viewthread/110969[/url]
#3

[eluser]thirteen[/eluser]
hi jedd...i have this in a controller

function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');

if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'No access';
die();
}
}

thanks
#4

[eluser]jedd[/eluser]
[quote author="thirteen" date="1262548942"]hi jedd...i have this in a controller
[/quote]

How many controllers do you have? If more than one, you should relocate this to MY_Controller

I think there are two broad ways to approach the problem.

A) Controller-based - so you determine which view partials to generate based on the result of is_logged_in() - and these view partials are then bundled together to produce your final full view

B) View-based - you pass the result of is_logged_in() to your view(s) and let it decide about the view output it generates.

My preference is (A) except for near-trivial 'display logic' decisions, which I'm happy to do in view files.

If you choose (B), you just need to do something like this in MY_Controller or your constructor(s):
Code:
$this->data['is_logged_in'] = is_logged_in();
...
$this->load->view ('foo', $this->data);




Theme © iAndrew 2016 - Forum software by © MyBB