/    Sign up×
Community /Pin to ProfileBookmark

Problem redirecting to login page if user session has expired while making ajax calls

Hi, my website checks if the user is logged in each time a page is loaded by checking a cookie which is placed.

My problem occurs when i make ajax calls to functions in the framework when the cookie has expired. The code that would usually redirect to the login page:

header(‘Location: /login’);

won’t work, i think because there is already html written to the page (i get a 302 error).

Is there anyway to be able to

try{header(‘Location: /login’);}

and then if that fails, just write a js <script>window.location=’/login’;</script> ??

Any help would be much appreciated.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@coldscooterauthorDec 31.2010 — If anyone is interested, i solved it by simply checking if the request was made via an ajax call in my check_logged_in function:

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{echo("<script>window.location='/login'</script>");die;}
else
header('Location: /login');


This seems to work nicely and is a very lightweight fix.
×

Success!

Help @coldscooter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...