• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
troubles with flashdata

#1
[eluser]Fenix[/eluser]
I am having trouble understanding how I can call the flashdata from this particular function (the login function from the ReduxAuth library) in my view. Please take a look at how I'm doing this... what I don't understand is why in the library it's using $this->ci->session-> but when I try to call it with that ci in there, it gives errors.


In the Library:
Code:
public function login ($email, $password)
        {            
            # Grab hash, password, id, activation_code and banned_id from database.
            $result = $this->_login($this->users_table, $this->banned_table, $email);
    
            if ($result)
            {
    
                if(!empty($result->activation_code))
                {
                    $this->ci->session->set_flashdata('login', 'Your Account is Not Activated.');
                    return 'NOT_ACTIVATED';
                }
                elseif(!empty($result->banned_id))
                {
                    $this->ci->session->set_flashdata('login', 'Your Account has been banned for the following reason : '.$result->reason);
                    return 'BANNED';
                }
                else
                {
                    $password = sha1($this->salt.$result->hash.$password);
    
                    if ($password === $result->password)
                    {
                        $this->ci->session->set_userdata(array('id'=> $result->id));
                        return true;
                    }
                }
            }
            return false;
        }

In my controller:
Code:
switch ($redux)
            {
                case 'NOT_ACTIVATED':
                    // Page Data
                    $data['page_title'] = 'Login - Not Activated';
                    $data['center_col'] = 'login_view';
                    $data['login_message'] = $this->session->flashdata('login');
                    $this->load->view('template_view',$data);
                    break;
                    
                case 'BANNED':
                    // Page Data
                    $data['page_title'] = 'Login - Banned';
                    $data['center_col'] = 'login_view';
                    $data['login_message'] = $this->session->flashdata('login');
                    $this->load->view('template_view',$data);
                    break;
                    
                case false:
                    // Page Data
                    $data['page_title'] = 'Login - Invalid Credentials';
                    $data['center_col'] = 'login_view';
                    $data['login_message'] = $this->session->flashdata('login');
                    $this->load->view('template_view',$data);
                    break;
                    
                case true:
                    redirect('admin/dashboard');
                    break;
            }

In my view:
Code:
if(isset($login_message))
    {
        echo '<p>'.$login_message.'</p>';
    }

I've also tried using just the $this->session->flashdata('login') in my view, that didnt work. Then I tried redirecting to users/login instead of just loading the views in each switch case, still no luck.

Please help me out! THanks!

#2
[eluser]JGarrido[/eluser]
So what type of errors/output are you getting? I've been attempting to access the same object with no success.

This isn't really relevant to the flashdata, but should help you with troubleshooting/debugging this particular section of code: One thing I noticed that could be problematic is the usage of that switch statement which includes both boolean and string evaluations. I've been evaluating this ReduxAuth library for a couple days, and that specific section gave me tons of trouble due to the fact that the 'switch' statement in PHP does a loose comparison, as opposed to strict (more info here); so when $redux contained a value of 'true', it still fired off the code in the first case block ('NOT_ACTIVATED'). I wound up wrapping it with an additional elseif to determine the return value type and then evaluate accordingly:

Code:
if(is_string($redux)) {
    switch ($redux) {
        case 'NOT_ACTIVATED':
            redirect('user/activate/');
            break;
        case 'BANNED':
            echo "<br />You have been bad and so have been BANNED!<br />";
            break;
        case 'WRONG_PASSWORD':
            echo "<br />Wrong password bub, press the back button on your browser and try again<br />";
            break;
    }
} elseif (is_bool($redux)) {
    switch ($redux) {
        case false:
            echo "<br />We do not seem to have a user registered with that email address.<br />";
            break;
        case true:
            echo "<br /><h4 style=\"color:green;font-weight:bold;\">LOGIN SUCCESSFUL!</h4><br />";
            break;
    }
} else {
    echo "error, big time!";
}

Another option would be to convert the switch to an elseif, would work just as well (and might actually be more concise and efficient, depending on your situation).

Aside from that, I've been having issues with the flashdata myself, but I'm new to both CI and ReduxAuth (and PHP in-general, for that matter), so hopefully someone else will be able to provide more insight. A deeper elaboration of the session handling in CI/ReduxAuth in the broader sense would be extremely helpful.

#3
[eluser]Fenix[/eluser]
[quote author="JGarrido" date="1219364627"]when $redux contained a value of 'true', it still fired off the code in the first case block ('NOT_ACTIVATED').[/quote]

This was one thing that was happening I'm pretty sure... I would log in with valid credentials but it would tell me NOT_ACCTIVATED... that isn't what it is doing anymore, I've been trying one thing after another so that is behind me for now.

I'll give you're method a try and see how it goes. I'll post back in 10

#4
[eluser]Fenix[/eluser]
Now, for some reason, everything works except it is letting me login even if the credentials are flagged as banned... are you having this problem at all?

#5
[eluser]JGarrido[/eluser]
I haven't actually tried the banned scenario yet, as it won't be a feature I'll need for this application. Have you made any changes yet to the switch statement that evaluates $redux?

#6
[eluser]Fenix[/eluser]
Yes, now it looks like this (works too, for what I can tell):
Code:
if(is_string($redux))
            {
                switch($redux)
                {
                    case 'NOT_ACTIVATED':
                        $this->session->set_flashdata('login', 'Your account is not activated.');
                        redirect('users/login');
                        break;
                    case 'BANNED':
                        $this->session->set_flashdata('login', 'Your account has been banned.');
                        redirect('users/login');
                        break;
                }
            }
            elseif(is_bool($redux))
            {
                switch($redux)
                {
                    case true:
                        redirect('admin/dashboard');
                        break;
                    case false:
                        $this->session->set_flashdata('login', 'The credentials you provided are invalid.');
                        redirect('users/login');
                        break;
                }
            }
            else
            {
                $this->session->set_flashdata('login', 'An unknown error ocurred.');
                redirect('users/login');
            }

And my logout function is in that same controller:
Code:
function logout ()
    {
        $this->redux_auth->logout();
        $this->session->set_flashdata('login', 'Logout successful!');
        redirect('users/login');
    }

And here is my view:
Code:
<h1>Login</h1>
&lt;?=$this->validation->error_string?&gt;
&lt;?php
    echo $this->session->flashdata('login');
?&gt;

&lt;?=form_open('users/login')?&gt;

<label for="username">Email : </label>
&lt;?=form_input('email')?&gt;<br/>

<label for="password">Password : </label>
&lt;?=form_password('password')?&gt;<br/>

<label for="submit"> </label>
&lt;?=form_submit('submit', 'Login')?&gt;

&lt;?=form_close()?&gt;

#7
[eluser]Popcorn[/eluser]
This is a bug with Redux, mixed returns and using them in conjunction with a php switch statement doesn't work too well.

Glad you got it sorted, this will fixed for version 1.5.

#8
[eluser]JGarrido[/eluser]
That's great to hear, thanks Matthew.

#9
[eluser]steelaz[/eluser]
If I'm not mistaken, you can only read session flashdata (or userdata) when a new page is loaded.

#10
[eluser]Unknown[/eluser]
Try this:

switch ((string)$redux)
{
case 'NOT_ACTIVATED':
# code...
break;
case 'BANNED':
# code...
break;
case false:
# code...
break;
case true:
# code...
break;
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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