Session expired during the ajax request in php - php

I have a problem that the session was expired and then I have clicked on a button which makes the ajax request, then on it holds & do nothing, in the console it displays as an error, if it is any other action then it redirects to login page but for the ajax it does nothing. Now what I need is that even on the ajax request it should get redirect to login page if the session gets expired. The main thing is that I have plenty of ajax calls in my application and I need to make a common script for all the ajax requests to get redirect to login page.
Please help me...

Call the server side file through ajax
Check for the session in the server side.
If session expired send response as session expired.
In the ajax success call back check the session expired data always.
If you found session expired value redirect to login using location.href.
Just a skeleton code below.
$.post('URL',{},
function (data) {
if (data.sessionexpired) {
location.href = 'login.php';
} else {
//Normal process
}
}
);

have the ajax request return an error code or the string error_go_home or something.
if the javascript that recieves that message gets that string, have the javascript redirect them home.

Related

Laravel 5 Session Doesn't Work on Redirect

I am having trouble with developing a Laravel application. When I set a session from a function and loaded the view the session will show:
Session::flash("test","ABC");
return view('layout.customer');
But when I set a session and redirect it to a URL in that page, the session will not work. I am currently using following code:
Session::flash("test","ABC");
return redirect()->route('customer.details');
Variables that are flashed only last one request.
When you send a redirect, the redirect itself is the first request sent to the visitor. On this request the existing flash session is cleared.
Now a second requests starts for the page that is being redirected to and the session flashes no longer exist.
Update:
You can use the with function to add flashed data to a redirect.
return redirect()->route('customer.details')->with("test","ABC");
More info: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
You can use return redirect()->route('customer.details')->with("test","ABC");to flash the data when redirecting. Here's the documentation for this: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
In customer.details controller add line use Session; at top of the page.

Call ajax to clear session after user has left page

I want to clear the php session array every time a user leaves my page, but my page has links with query strings. I don't want to clear the session array, when a user clicks on a link with a query string. I have tried the following javascript code but it does not work when the user leaves the page.
somepage.php
var url = new RegExp(/somepage.php\?sort=.*/);
if (url.test(document.location.href)){
//do nothing
}
else {
$(window).unload(function(){
$.ajax({
url: 'clear_session.php'
});
});
}
Calling a webserivce onunload is very unrealiable. Why not just unset the PHPSESSID cookie? This wont clean up the session on the server, but it will give the user a new empty session when he visits again.

ajax logout redirect

public static function logout(){
DB::query("DELETE FROM webchat_users WHERE name = '".DB::esc($_SESSION['user']['name'])."'");
$_SESSION = array();
unset($_SESSION);
return array('status' => 1);
window.location.replace("http://domain.com/index.php");
}
This is the code I use to logout of a chat window, that is ran using AJAX. I'm just wondering if there is a way so it will do a redirect when the button is pressed. This is the procedure that is ran on.click. I've currently experimented with window.location. but that doesn't seem to do the trick.
How would I do this?
If it's an Ajax call you cannot redirect the user. If you use header('Location: ...') you will redirect the Ajax requestion, which will have no effect on the user.
You can either:
redirect the user to the logout page, instead of sending an Ajax request and use header('Location: ...') to send him back to your home page
Use an ajax call, but the window.location must be done in the javascript par after the ajax call. In the onSuccess event for instance

Login loading page

I have a php site where a user has the ability to login. Once they submit the POST data with their credentials they are taken to a page that initiates a session and checks the database. This process takes a long time before the user is ultimately redirected to the backend of the site. Durring this time the user stairs at a blank white page. I would like to show some kind of loading page, but when anything printable is on the page I get a header error due to the redirect.
How can I create a loading page? I understand that ajax is an option but is it my only option?
Update:
The PHP application is mainly taking a long time because each login is dependent on a foreign API that needs to be authorized as well as charging data that needs to be queried. I am not trying to mask something I can easily fix, I am trying to create a better user interface.
The login process takes ~10 seconds.
Well, the easy way to handle this is as follows:
Once a user pressed loging, using javascript, hide the login button, inject some ajaxloader gif, than send an AJAX request with the login credentials, salt it properly, log the user in, post the response as JSON, and intercept that response, and in case of success - do redirect, incase of failure - print appropriate message.
some code:
login-handler.php
$user = new user();
if ($user -> login($_POST['uid'], $_POST['password'], $_POST['whatever']){
$response['type'] = 'success';
}
else{
$response['type'] = 'failure';
}
echo json_encode($response);
js-login.js
$("#login").click(function(){
// verify that user put user name, password and whatever, hide button and show ajax loader
$.get("login-handler.php", {user: user, password: password}, function(data){
if (data.type == "success"){
window.location = "http://example.com/new/location/whatever";
}
else{
//print message
}
}, "JSON");
});
EDIT: but I'd like to join the other folks and consider using a faster/different API, as 10 secs loading time is utterly ridiculous, unless it is for yourself, in which case - knock yourself out.
There is a classic trick around this:
Set the target of the login form to a (invisible) iframe, but in it just set a variable to a redirection URL
On submit set the div carrying the username/password to .style.display='none', then set a already loaded "please wait" div to .style.display='block'
When the iframe finally loads use document.getElementById('myiframe').contentWindow.VarName to get the URL and write it to location.href

How to do a login in flash, and then redirect to php?

As the title said, login page should be in flash(login.swf), and the redirect to a php page (account.php). In account.php, I will need to check session, for example:
isset($_SESSION['loggedin']){
echo "Welcome back, $user";
} else {
echo "You need to login and main site";
}
Where do I generate the sessions, and where do I store it, and how do I do the session check??
EDIT: About my current problems
Login in swf page
user type username and password and send to authenticate.php
if its valid user, i send variables back to flash: echo "login=true" and then start and create session e.g $_SESSION['username']
Back to flash, after some animation going, I click a button that link to profile.php page
In profile.php, i do a check if(isset($_SESSION['username']){ echo "welcome back user";
But the problem is, after i login in flash/swf page, i click the button to the profile.php page, I still need to login again, means, there is no $_SESSION['username'].
So, my questions: Where do I generate the sessions, and where do I store it, and how do I do the session check??
In the login.swf, on login button click, you need to do a http request to the server (say to validate.php page)with the credentials that user entered. In the validate.php you need to check for whether credentials are valid and need to create session if its valid and send back the response to the login.swf(in some xml format telling user is authenticated/valid or not).
On response handler of http request, get the result xml check whether user valid or not, if not valid show error message, if valid, call a javascript function(basically window.location.href='/account.php') which will redirect the entire page to account.php. Once the page request for account.php comes, you can use the session that you created and the user details from session.

Categories