Topic: Problem with sessions

Hi, firstly sorry about my english, it's  little bad (i'm spanish).

I'm using the xajax's previous version (it's a project from another company and now my comany it's the developer).

Well, my problem it's this. There is a page with a loggin so we make a session variable with session_start. What happend? well, when the session expired last one hour we do an "header('location: out.php')"  but if we press on a button the response of xajax it's "the xml response it's no...." adding the content of the page that we are redirecting.

How I can do the redirect? why xajax it catching the content of the page?

Thanks and sorry about my english again.

Re: Problem with sessions

$objResponse->redirect($url); or in 0.2.x ->addRedirect($url): that's all smile

why xajax it catching the content of the page?

the header() redirect statement forces the browser to load another page. During an ajax-call (XHR) the browser expects a valid reponse either as text/xml or as text/plain and does not recognize any redirect statements. That's why you receive an errormessage.

3 (edited by j.avi 2008-01-10 6:47:19 PM)

Re: Problem with sessions

I have tried this codes:

First code:

if ($_SESSION['log']==NULL)
{
               header('Content-type: text/html ');
               header('Location: index.php?err=login');
}

The response it's with the content of the page

Second code:

if ($_SESSION['log']==NULL)
{
    $objResponse = new xajaxResponse();
    $objResponse->addRedirect('index.php?err=login'):
    return $objResponse;
}

Here, the response it's blank, show the alert with error and I recibe it in blank. I don't understand why whit the second code have this problem because i think it's right

I tried too to write the xml that do the response (i copied from the debuger) but doesn't work. I do it like this:

    $objResponse = new xajaxResponse();
    $xml='<?xml version="1.0" encoding="iso-8859-15" ?><xjx><cmd n="js"><![CDATA[window.location = "xx.php";]]></cmd></xjx>';
    $objResponse->loadXML($xml);
    //return $objResponse->getXML();
                return $objResponse;

Maybe i do it wrong

Finally, i tried this one, but doesn't work too XD

    header('Content-type: text/xml ');
    $xml='<?xml version="1.0" encoding="iso-8859-15" ?><xjx><cmd n="js"><![CDATA[window.location = "xx.php";]]></cmd></xjx>';
    echo $xml;

Thanks smile

Re: Problem with sessions

Ah, I see smile The problem is, that $xajax->processRequest(); is processing the current xajax request - so you can't return any other responses - xajax will try to process the request anyway.

To catch expired sessions I'm using a 'small hack'.

if (
        (
            ($_SESSION['CMS']['user_id'] == "")
            OR
            ($_SESSION['CMS']['user_id'] == 0)
        )
        AND
            ($_REQUEST['xajax'] != "openLogin")
        AND
            ($_REQUEST['xajax'] != "initApp")
        AND
            ($_REQUEST['xajax'] != "loginUser")
        ) {
            $_POST['xajax'] = "SessionFailed";
        }


function SessionFailed($a=false,$b=false,$c=false,$d=false) {

    $objResponse = new xajaxResponse();
        // redirect, whatever
    return $objResponse;
}

This small snipped replaces the functionname when the session is expired. This way $xajax->processRequest() calls the function SessionFailed. You might also want to override the function arguments, since the number of given arguments may differ from function to function.
I've set a few "dummy vars" that catch the varying number of passed arguments. It's not cleanest solution, but it does the job smile

5 (edited by j.avi 2008-01-11 10:56:07 AM)

Re: Problem with sessions

Thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks,!!!!!!!! I do it!!!!!!!!!!! big_smile

I have to do this:

if ($_SESSION['nick']==NULL)
{
    $_POST['xajax'] = "finalize";
}

function finalize()
{
    $objResponse = new xajaxResponse();
    //$objResponse->addScript("alert('".$_POST['xajax']."');");
    $objResponse->addRedirect('../profesionales.php?err=login');
    return $objResponse;
}


But I have to put this before:

require ('../xajax/xajax.inc.php');
$xajax = new xajax();
$xajax->setCharEncoding("iso-8859-15");
$xajax->registerFunction("finalize");
$xajax->registerFunction("out");
.
.
.
$xajax->processRequests();

This it's very important because if I put the code after this don't work big_smile


Thanks a lot for tell me this line $_POST['xajax'] big_smile:D:D

Re: Problem with sessions

This is an interesting solution... so I've made the topic sticky.  However, I wanted to point out that this will not work as expected when you upgrade to xajax 0.5

With xajax 0.5, it is possible to have any number of plugin's which will use their own POST key... for example:

$_POST['xjxfun']

So, it is not possible to know what the post key value will be.  I am sure there will be some creative solution; but I don't think this has been solved yet.

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: Problem with sessions

Hi,
I am right now working on the upgrade from 0.2.6 to 0.5. And ran exactly into the problem described above. Luckily the last reply by "Joe" on 08-01-12 helped me to find the solution. I basically do the same thing as "j.avi":
When handling ANY call (normal page load as well as "AJAX" call), I first check the session, before and further processing is done. When I detect no or an invalid/timed-out session, I redirect to the login page, see code:
----------------------------------------------------------------
function sessionXAJAXRedirect($arg1=0, $arg2=0, $arg3=0, $arg4=0, $arg5=0, $arg6=0, $arg7=0, $arg8=0) {
    $objResponse = new xajaxResponse();
    $objResponse->redirect($arg1);
    return $objResponse;
}

function sessionDoRedirect($redirectTo) {
    // Fix for xajax 0.5: renamed "xajax" to "xjxfun"
    //               and "xajaxargs" to "xjxargs"
    if (isset($_REQUEST["xjxfun"])) {
    $_POST["xjxfun"] = "sessionXAJAXRedirect";
    $_POST["xjxargs"][0] = $redirectTo;
    require_once("../xajax/xajax_core/xajax.inc.php");
    $xajax = new xajax();
    $xajax->register(XAJAX_FUNCTION, 'sessionXAJAXRedirect');
    $xajax->processRequest();
    exit();
    } else {
    header('Location: '.$redirectTo, true, 302);
    exit();
    }
}
----------------------------------------------------------------
So if the module is not a "XAJAX" function-call, I use the php "header()" command, otherwise I have to use the XAJAX redirect function, for which I have to "hack" the POST variables. I delete the original function call and replace it with my sessionXAJAXRedirect function.

Not sure if there is a more elegant possibility, but this works well. For the migration from 0.2.6 however it was first not working, until I found this post that told me that the POST-Variable name changed from xajax to xjxfun (see comment in code).

Now I wanted to ask for more clarification and maybe new information on the last comment:
Quote: So, it is not possible to know what the post key value will be.  I am sure there will be some creative solution; but I don't think this has been solved yet.

That means that with plugins, the POST variables can have different names? I'm not using any plugins, so I'm not concerned for my code at the moment, but I would be curious if there is a solution to this issue. Any ideas? Should I further investigate or ignore, as I'm not using any plugins right now?

  Thanks, Herbert

Re: Problem with sessions

Well, what I can say is that this solution change my mind!
I was so upset without this solution that I was changing my colegge project to Flex!
Thanks!

And many thanks to xAjax developemnt team!!!

Regards,

Marcello

Re: Problem with sessions

I'm glad you have been helpful, a greeting.