• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with boolean

#1
[eluser]KrizzAngel[/eluser]
im really confused. i know im a noob XD anyways can u guys help me with this:
Quote:function checker()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
return true;
}

}
i made that function but i dont know how to use it to my index() and other page lol.. what i want to happen is if a user try to access certain page it will redirect to login(home page) if user is not logged in and on the other hand i dont want to access home page if the user is still logged in.

#2
[eluser]bretticus[/eluser]
[quote author="KrizzAngel" date="1264151612"]
Code:
function checker()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            return true;
        }
        
    }
[/quote]

Code:
$this->session->userdata('is_logged_in')

...according to the manual this call will either return the value you set in it (boolean perhaps?) or FALSE if that session key (is_logged_in) is not found.

isset() simply checks to see if the variable is set. Since you set it before your if statement,
Code:
!isset($is_logged_in)
will never be true because
Code:
isset($is_logged_in)
is always true.

In fact, as long as you are setting boolean values to the session item, you can probably make this function a one-liner:

Code:
function checker(){
     return $this->session->userdata('is_logged_in');
}

#3
[eluser]Eric Cope[/eluser]
if this is always checked, consider placing it in the constructor.

#4
[eluser]KrizzAngel[/eluser]
constructor?? i dont know if u guys gets what i mean hehe..
i just dont know how "return" value works..
for example i was coding my index() function.
i wrote
Quote:function index()
{
$this->checker();
$this->load->view('home.php');
}

does this mean that if the checker returns true it will continue to load the home page?? what if its false..
how can i express the other code so that it will load another page if its false/

#5
[eluser]n0xie[/eluser]
[quote author="KrizzAngel" date="1264174356"]
does this mean that if the checker returns true it will continue to load the home page?? what if its false..
[/quote]
No it will just return TRUE or FALSE. Since you don't actually do anything with the function, the return value gets lost.

Quote:how can i express the other code so that it will load another page if its false/

Try this:
Code:
function index(){
  if($this->checker()) {
    $this->load->view('someview');
  }
  else
  {
    $this->load->view('someotherview');
  }
}

function checker(){
     return (bool) $this->session->userdata('is_logged_in');
}

#6
[eluser]KrizzAngel[/eluser]
ok ty.. ill try that..

#7
[eluser]Yorick Peterse[/eluser]
[quote author="KrizzAngel" date="1264174356"]constructor?? i dont know if u guys gets what i mean hehe..
i just dont know how "return" value works..
for example i was coding my index() function.
i wrote
Quote:function index()
{
$this->checker();
$this->load->view('home.php');
}

does this mean that if the checker returns true it will continue to load the home page?? what if its false..
how can i express the other code so that it will load another page if its false/[/quote]

The return statement, as the name might suggest, does nothing more than returning a certain value (or whatever it's supposed to return). If you place this statement in a function (as shown below), it will skip everything after the return statement.

Code:
<?php
function cookies()
{
    // This gets executed
    $var = 'hello world';
    return $var;
    // This is ignored
    echo 'Hello planet';
}
?>

The difference between return and echo is that echo outputs it to the screen (or console) whereas return does nothing more than returning it. You can use the return statement as following:

Code:
<?php
    // The function
    function hello_world()
    {
        $message = 'Hello world!';
        return $message;
    }
    // How to use it, method 1
    echo hello_world(); // Outputs: Hello world!

    // Or you can do this
    $some_var = hello_world()
    echo $some_var; // Outputs: Hello world!
?>

#8
[eluser]KrizzAngel[/eluser]
ok that was clear.. i got it now.. but another problem i got here.. can u guys assess my code..
Quote: function index()
{
if($this->checker())
{
$this->load->view('home');
}
else
{
$this->upanel();
}
}

function checker()
{
return ($this->session->userdata('is_logged_in')) ;
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
*edited
ok i thought its my logout that had a problem but its not.. i closed my browser but it seems that index always redirect me to upanel XD

#9
[eluser]KrizzAngel[/eluser]
*bump*


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.