Welcome Guest, Not a member yet? Register   Sign In
Redirecting To Login Page If Session Expired during or before Ajax request is made
#1

[eluser]arian1[/eluser]
I tried redirecting to login page if the session expired during ajax request is made
i used redirect('login','location') but it loads the login page in the div of current page
i tired echo html meta tag and javascript location.href but it didn't work
Code:
function session_check()
{
//code to check session
if(session is empty) {

redirect('login','location');  //1. loads the login page in current page div

echo 'location.href='<?=base_url();?>login';';  // 2.doesn't work

echo '<meta http-equiv=refresh content="0;url=<?=base_url();?>login">'; //3 doesn't work

}

}

i can use that session_check function for all my controller functions which are used for processing ajax request ...

thanks for reply in advance and hope you can understand my english
#2

[eluser]Sarfaraz Momin[/eluser]
try

Code:
echo 'top.location='.base_url().'login';';

echo the code in script tag. I suppose it should work.

lemme know the outcome...

Good Day !!!
#3

[eluser]arian1[/eluser]
thank for reply
it doesn't work
Code:
echo '[script]top.location='.base_url().'login;[/script]';
i used [] instead of <> for script tag here otherwise it disappears

when i checked the html it's shows the code but no effect i have tried using this outside of codeigniter and it works fine also if i call this funtion without ajax the redirection works but not with ajax ..
sorry i made the mistake in echo base_url on my post thanks for pointing it out
#4

[eluser]arian1[/eluser]
Hmm still searching for the way to refresh the page hope somebody can write the solution

thanks
#5

[eluser]xwero[/eluser]
a script doesn't get executed when it's inserted in the page, you need a trigger to execute it.
what are you using for you ajax calls?
#6

[eluser]arian1[/eluser]
i am using prototype Ajax.Updater function ..

if i trigger redirection using the response i have to add code to my every ajax function
//0 if session expired echo '0'; in php session_check_ajax function
javascript
after recieveing response

if(response==0) location.href='/login';
else { response goes to div }

or i can add those lines in one javascript function to put before the response goes to div

but still i have to add that line to all my ajax function ..

what i was hoping for that if i can somehow redirect without adding anything to my javascript ajax functions

jsut puting eveyrthing in my php session_check_ajax function.

hope you can understand my english .

and thanks a lot for your previous reply
#7

[eluser]arian1[/eluser]
Hmm if anyone knows what is the best way to deal with ajax and session in codeigniter please reply

thanks
#8

[eluser]Vik[/eluser]
I looked into this recently. The way I addressed it was to have my Ajax Javascript add a variable to the $_Post array, called, "an_ajax_call_is_active." It's a boolean variable set to true. My PHP code checks the $_Post array, and if it is present, and the session is no longer active, the PHP code returns a message saying "The session has timed out" via Ajax, rather than the regular data.
#9

[eluser]arian1[/eluser]
thanks for reply vik ..

if i cannot refresh the page ... i think your solution is the only way

i will make function which will check if $this->session->userdata('session_data') has
expired and if it has expired .. printing message linked to login page which will on

onclick simply refresh the page or redirect to login page and include that function to all my ajax php functions.

I still wish we could refresh the page..but i think for now its not possible ..

this is my first experince with any framework and i am learning everyday something new

thanks for you reply again
#10

[eluser]Nick Husher[/eluser]
Hey, I've done a little bit of what you're talking about. I was using the YUI connection object and was transferring data over JSON. JSON is nice because you're handing a Javascript-compatable data structure across the connection that you can run an eval() on. Basically, the code looked like this:
Code:
var callbackObject = {
    success: function(o) {
        var response = eval('('+o.responseText+')');
        if(response.serverResponse == 'session-timeout-error') {
            [removed] = LOGIN_PAGE;
        } else {
            // process AJAX call
        }
    },
    failure: function() {},
    timeout: 3000
}
YAHOO.util.Connect.asyncRequest('POST', url, callbackObject);
Basically, if my session had expired when the ajax function is called, it would return an object that would look like this:
Code:
{ 'serverResponse': 'session-timeout-error' }

Note: Eval should never, ever, ever (ever) be used if you don't have 100% control over the content passing through it. If it's content that's being generated by someone that's not you, and you aren't extremely adept at ripping the potential javascript out of it, don't use eval. Eval is evil.

Further Note: YUI 2.4 comes with an excellent SafeMode JSON parser. Use that instead.




Theme © iAndrew 2016 - Forum software by © MyBB