Welcome Guest, Not a member yet? Register   Sign In
Problem with session flashdata
#1

[eluser]kevinh21[/eluser]
Hi everyone
I'm programming an administrator-backend for my website. I use the erkanaauth library to make this easier.

Now i have some problems with flashdata to show a message when the access is denied.

admin controller:

Code:
function settings()
{
   if($this->erkanaauth->try_session_login())
   {
      $this->_showSite('admin/settings', 'left_settings');
   }else{
      $this->session->set_flashdata('status', 'You must be logged in to view this page');
      $this->_showSite('admin/login');
   }
}
...
function _showSite($module, $leftmodule = false)
{
   $data['module'] = $module;
   if($leftmodule <> false) $data['left'] = $leftmodule;
   $this->load->view('admin_layout', $data);
}

admin/login.php (view):
Code:
<div class="content wide">
<h1 id="login">Login</h1>
<hr />

&lt;?=$this->validation->error_string;?&gt;
&lt;?php echo '<p class="notice">'.$this->session->flashdata('status').'</p>';?&gt;

&lt;form class="login" action="&lt;?=site_url('admin/login')?&gt;" method="post"&gt;
.....
&lt;/form&gt;

</div>

The Problem:
If i call admin/settings, it should check if i'm logged in. If not, it should set a flashdata message (status) and show the admin/login.php view with my function showSite().
In this view, it should display the flashmessage i set.

But he is not showing the message when i'm cliking on a link that refers to admin/settings.
I need to click at least twice, then it will show me the flashmessage.

What i'm doing wrong?
#2

[eluser]kevinh21[/eluser]
Hi again

The problem must be here:

Code:
function settings()
    {
        if($this->erkanaauth->try_session_login())
        {
            $this->_showSite('admin/settings', 'left_settings');
        }else{
            $this->session->set_flashdata('status', 'You must be logged in to view this page');
             echo 'TEST'.$this->session->flashdata('status');
            $this->_showSite('admin/login');
        }
    }
The first time i execute it, it just echoes "TEST".
if i then do it again and from this point it will write "TESTYou must be logged...."

I don't understand why Sad
#3

[eluser]Yash[/eluser]
do something like this
Code:
$login = $this->session->userdata('logged_in');
if($login)
{        
//send to control panel
redirect('admin/controlpanel/','refresh'); }
else
{
//send in login form
}
I don't which session is set by euth lib for admin login.
Just check that and use this code.
#4

[eluser]kevinh21[/eluser]
Hi Yash
Thanks for your answer but i think you misunderstood me.
The if-code is working fine, i think i should make it a bit more understandable by reducing the code:

function settings()
{
$this->session->set_flashdata('status', 'You must be logged in to view this page');
echo 'TEST'.$this->session->flashdata('status');
}

This part is making the problem.
Sometimes when i execute this function by browsing to localhost/admin/settings or clicking a link that refers to this, it will only write "TEST" and the session flashdata in 'status' seems not to be set.
When i reload the page, it will write "TESTYou must be logged in to view this site".

I don't understand why it sometimes doesn't have set this flashdata.
Most of the times this is the case after i executed other functions.
#5

[eluser]makedjakila[/eluser]
Hi Kevinh21 !

You need to understand how Session works in PHP : You need to load another page to see the session data. It means that you have to set a redirection if you want to see your flashdata at the first time.

Regards,
Makedjakilabee




Theme © iAndrew 2016 - Forum software by © MyBB