Welcome Guest, Not a member yet? Register   Sign In
Nested CI modules problem
#1

[eluser]DougW[/eluser]
I have a main module, let's call it 'home.' It is a controller and a view. On this view I am using a jquery menu that loads sub-modules with ajax. Each sub-module is also a controller/view combination, such as 'dashboard.'

I am trying to find a way to detect whether or not the sub-module was in fact loaded by the ajax call and thereby, valid. For example, I have a sub-module called 'dashboard' but I don't want users to be able to load the dashboard with just /dashboard. it should only be valid if called from the home module.

There are probably a million ways to do this but I was wondering if there was a CI- tool or trick for detecting whether a CV construct was at the 'top' level or not besides looking at the url, as that clearly won't work in this case.

Any clever ideas are appreciated.
#2

[eluser]cahva[/eluser]
Put this line to application/config/constants.php
Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

With that you can check if the request was an ajax request or not:
Code:
if (!IS_AJAX)
{
    // Not an ajax call
}
#3

[eluser]DougW[/eluser]
That did it. Thanks! Brilliantly simple. Just put the validation in any controller ad voila!
#4

[eluser]DougW[/eluser]
On the next phase of this, the session ID times out and I am prompted to login again which is expected. The problem is that even though I put:

if (IS_AJAX) redirect("/");

in my login controller, the login screen is drawn in the div that was loaded by ajax. I want it to break out and go back to top level if a user's session times out. How can I do that?
#5

[eluser]InsiteFX[/eluser]
You need to extend the Session Class.

Create a new file called MY_Session.php and add the below code to it.
Then place the MY_Session.php file in application/libraries

Code:
class MY_Session extends CI_Session
{
   /*
    * Do not update an existing session on ajax calls
    *
    * @access    public
    * @return    void
    */
    public function sess_update()
    {
        if ( ! IS_AJAX())
        {
            parent::sess_update();
        }
    }

}

InsiteFX
#6

[eluser]DougW[/eluser]
Thanks. I tried this, put it in autoload and same result. Is there a particular way I need to call it?




Theme © iAndrew 2016 - Forum software by © MyBB