Welcome Guest, Not a member yet? Register   Sign In
Login Session Problems
#1

[eluser]datguru[/eluser]
Hey there, well what I am trying to do is make an if statement that repoints the user to the login page if they havnt got the specified 2 sessions

Code:
if($this->session->userdata('loggedin') != TRUE && $this->session->userdata('admin') != TRUE)
{
    $this->load->view('login')
}
else
{
//other stuff here
}

However if the sessions havnt been created then I get a severity notice, It still repoints the user because obviously the values are not true, but it leaves two big red boxes above the page. Is there anyway to stop these boxes (just for that page) from appearing? Or is there a better way to test to see if a user is logged in or not?

Thanks for your time

Regards

Chris.
#2

[eluser]mironcho[/eluser]
Hi,
you can use the @ sign (php error control operator):
Code:
if(@$this->session->userdata('loggedin') != TRUE && @$this->session->userdata('admin') != TRUE)
{
    $this->load->view('login')
}

For more info: http://php.net/@
#3

[eluser]datguru[/eluser]
Hey thanks for the reply, it stops the errors now, but unfortunately it seems to stop everything. As in it now doesn't go into the if statement it stops at the errors (or doesn't seem to output anything).
#4

[eluser]mironcho[/eluser]
The @ sign simply says - ignore all errors from this expression. Probably you have some other problem - can you post here your login controller? Probably something with loggedin and admin values? Can you echo or var_dump them.
Other thing - you are using not equal operator (!=) which means that if, for instance loggedin value is 'some text' or number different form zero, then the result from this expression will be TRUE. So you can avoid this by using not identical operator (!==).

Hope this helps!
#5

[eluser]datguru[/eluser]
Hey they are boolean values so will either be 1 or 0. I haven't made the login controller yet because I am just making the back-end at the moment so was not worried about protecting it until I had made it all. My problem is that I cant seem to say don't let anyone view this page unless they have these session variables (Logged in = 1 and admin = 1). The reason it says I cant have them is because the object doesn't exist (which is true because they need to go to the login page for these to initialise). So the main problem is that it doesn't seem to like evaluating the truth of an object which doesn't exist, any easy way around this?
#6

[eluser]Pascal Kriete[/eluser]
It should still work since $this->session->userdata('value') return FALSE if the value doesn't exist. Are you loading the session library?
#7

[eluser]datguru[/eluser]
I'm sorry guys that was the problem :S. I said to myself I wont put it in the auto-load yet to see what performance difference it makes, and I forgot to call it! Sorry guys! Thanks for your help though Smile.
#8

[eluser]mironcho[/eluser]
Hey, no worries Smile




Theme © iAndrew 2016 - Forum software by © MyBB