Welcome Guest, Not a member yet? Register   Sign In
page viewable only if logged in
#1

[eluser]josh48202[/eluser]
how would a make a page viewable if your logged in. i didnt have any luck looking for another topic maybe someone could point me in the right direction. thank you.
#2

[eluser]Craig A Rodway[/eluser]
Search the forum for user authentication, login, authentication etc or have a look on the Wiki. There are already-made libraries that allow you to do this with varying levels of complexity or you could do it yourself using sessions.
#3

[eluser]josh48202[/eluser]
thx i just found them i guess i should have kept looking. can someone please delete this topic.
#4

[eluser]Pascal Kriete[/eluser]
Well there are lots of solutions to this. And there are plenty of libraries and such that people have written. If you do a search you'll likely find a few of them. This is what I usually do:

I have a login controller that checks the login data in a model. If it matches I set a variable called logged_in in my session:
Code:
$this->session->set_userdata('logged_in', TRUE);

Then I have a little helper that I wrote which contains the following:
Code:
// Checks if logged in and redirects to $redirect if false
function checkLogin($redirect = '') {
    $CI =& get_instance();
    $logged_in = $CI->session->userdata('logged_in');
    
    if ($redirect != '')
    {
        if ($logged_in == FALSE)
            redirect($redirect);
    }
    
    else
    {
        if ($logged_in == FALSE)
            redirect('login/');
    }
}

// Returns true if logged in - false otherwise
function isLoggedIn() {
    $CI =& get_instance();
    
    $logged_in = $CI->session->userdata('logged_in');
    return $logged_in ? TRUE : FALSE;
}

So now, in the constructor for my login controller (called login) I put:
Code:
$this->load->helper('login');
        if (isLoggedIn())
            redirect('user/');

And on any controller/function I want to protect I put:
Code:
$this->load->helper('login');
        checkLogin();

And there you go.

EDIT: Seems I was a little too slow Wink
#5

[eluser]josh48202[/eluser]
maybe i should explain this better. i want to add a tab to the navigation that can only be seen if your logged in.
#6

[eluser]Pascal Kriete[/eluser]
Ok, well assuming you're using the code I posted you would put this in your controller:
Code:
$this->load->helper('login');

And then in your view you would do:
Code:
<?php if (isLoggedIn()) : ?>
stuff you only want to show when logged in
<?php endif; ?>

Hope that helps,
#7

[eluser]josh48202[/eluser]
well im using FAL and i got it to where you have to be logged in to see the page so i have this
Code:
$this->freakauth_light->check('user');
what would i use to hide the tab?
#8

[eluser]Pascal Kriete[/eluser]
I've never used FAL, but according to their documentation, you could use this:
Code:
$this->freakauth_light->isValidUser();

Which will return true if the user is logged in. So basically do this in your controller:
Code:
$data['logged_in'] = $this->freakauth_light->isValidUser();

$this->load->view('yourview', $data);

And in your view you put:
Code:
<?php if ($logged_in): ?>
whatever
<?php endif; ?>
#9

[eluser]josh48202[/eluser]
well im trying to do this with the tab. i added the code to the controller and i added the rest of the code to the view file and im getting an error. this is what my view file looks like. its for the menu tabs.
Code:
<?php $ci_uri = trim($this->uri->uri_string(), '/'); $att = ' id="active"';?>
    <ul id="navlist">
        <li&lt;?= $ci_uri == ''? $att: ''?&gt;>&lt;?=anchor('', 'home')?&gt;</li>
        <li&lt;?= $ci_uri == $this->config->item('Hotel_uri')? $att: ''?&gt;>&lt;?=anchor($this->config->item('Hotel_uri'), 'hotels')?&gt;</li>
        <li&lt;?= $ci_uri == $this->config->item('Info_uri')? $att: ''?&gt;>&lt;?=anchor($this->config->item('Info_uri'), 'info')?&gt;</li>
        <li&lt;?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?&gt;>&lt;?=anchor($this->config->item('FAL_login_uri'), 'login')?&gt;</li>
        &lt;?php if ($logged_in): ?&gt;<li&lt;?= $ci_uri == $this->config->item('profile_uri')? $att: ''?&gt;>&lt;?=anchor($this->config->item('profile_uri'), 'Profile')?&gt;</li>&lt;?php endif; ?&gt;
        <li&lt;?= $ci_uri == $this->config->item('FAL_changePassword_uri')? $att: ''?&gt;>&lt;?=anchor($this->config->item('FAL_changePassword_uri'), 'change password')?&gt;</li>
        <li&lt;?= substr($ci_uri, 0, 5) == 'admin'? $att: ''?&gt;>&lt;?=anchor('admin', 'admin')?&gt;</li>
    </ul>
#10

[eluser]Pascal Kriete[/eluser]
Hmm, I don't see any mistakes in your code.
Is it a specific error?
Does it work without the if statement?

Otherwise I must say I'm out of ideas Sad.




Theme © iAndrew 2016 - Forum software by © MyBB