ajax logout redirect - php

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

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.

Avoid "resend form confirmation after login

I'm using this script for login on a site in development: http://www.php-login.net/
I'm using the simple login, like so:
require_once("config/db.php");
// load the login class
require_once("classes/Login.php");
// create a login object.
$login = new Login();
// ... ask if we are logged in here:
if ($login->isUserLoggedIn() == true) {
$userid = $_SESSION['user_id'];
$time = Users::getData($userid);
include("views/logged_in.php");
} else {
// the user is not logged in...
include("views/not_logged_in.php");
}
The script saves the login id in a session which I then use as a parameter in my function getData. This works fine, the problem is when I
a) refressh the page
b) navigate with whe forward/backward buttons in the browser.
I keep getting the "confirm resending form..." message pop up, which of course is not very user friendly.
How can I avoid this?
You have to use header and redirect site after posting form.
This is happening if user log in, and right after logging in he clicks to logout. Then when you press back button in the browser, there is sending form with $_POST data of his logging and the browser asks you if you want to send that form again.
Use this solution to avoid sending form again (when back button is clicked or F5 (refresh) is pressed):
When you process form with login data, do redirect to the same page, or to home page (its on you). Use
header("HTTP/1.1 302 Found");
header('Location: '.$url_to_redirect);
Then, when he clicks back button, or refresh page, it will not send again login data, so it will not process login again.

php navigate to previous page after login workaround

So for some reason, I tried http_referer and it isn't working for me...or maybe it is but this is the reason why I'm trying a workaround. I'm testing a login script in my browser and I want the user to go back to the previous page which they were visiting.
The issue I ran into is when I put in my login credentials, Firefox and Chrome pop out a "Save Password" option and I THINK it acts like a page. So when http_referer is called or when
echo '<script type="text/javascript">javascript:history.go(-1);</script>';
The page is still on the login page! Well what if the user clicks once "Don't Save" and that option is saved...then using history.go(-2) wouldn't work. Here's the workaround I tried but I can't get it to work.
Initially if the login info is correct, I call the first history.go(-1). Then I'm using this function to get the current page and if it matches login.php, I want to go back one more page! But it's not working. Please advise me
if($login_check > 0){
echo '<script type="text/javascript">javascript:history.go(-1);</script>';
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curpage = curPageName();
if ($curpage = "login.php"){
echo '<script type="text/javascript">javascript:history.go(-1);</script>';
}
}
I've also read to use sessions but my session array saves data for the shopping cart so I'm not sure how I would use sessions for navigation.
Why not store the URL of the page that redirects to the login page in a session and then redirect to it on a successful login? That way even if the user has a few failed login attempts they'll still get to the right page.
Another benefit for this method over javascript is that the redirect is sent in the header on page request, no need to send the user js.
Save the URL as a session variable when the user is not logged in.
// not logged in
$_SESSION['redirect_url'] = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
//now redirect to login page, something like
header("Location: http://www.site.com/login");
Nb. The URL above is crudely constructed, if you are using SSL or a different port (not 80) you'll need to change it accordingly.
Retrieve it on successful login
//login successful
header("Location: ".$_SESSION['redirect_url']);
exit;

Session expired during the ajax request in 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.

jQuery AJAX and PHP session issue

Really hope this makes sense and someone can point me in right direction. On a registration page (parent page), when you enter a license code a jQuery ajax success function loads content from another page with an appended url to include a session id:
success: function(data) {
if (data=="RegisterError") {
$("#error").show();
$("#processing").hide();
} else {
$("#contain").load("download-software.php?sessionid=xXXX #req");
}
}
The page "download-software" has the following PHP referrer check to make sure the content is being requested from the registration page via the session ID and redirects you if it's not:
<?php
$code = $_GET['sessionid'];
if(strcmp( $code , 'xXXX' ) != 0) {
header("Location: http://www.someotherpage.com");
}
?>
That works fine. Now what I need to do is in the "download-software #req" content that is loaded into the parent page, have a link that when clicked replaces the "download-software #req" content which has been loaded inside the parent page with content from another page and do the same type of session id check.
I cannot get it to work. I place the following code on the "download-software #req" content for the
<a id="beta">beta notes
$("#beta").click(function() {
$("#req").load("NT7-SP1-Download.php #betaNotes");
});`
I've also tried using the .live function. How do I start a new session and make this work?
*answer**
I used the live function on the parent page and it works fine. Making it too hard i guess.
I would refrain from using GET query string parameters and use the included PHP session functions.
use:
<?php
// initialize the session
session_start();
// assignment call
$_SESSION['key'] = 'value';
?>
You can retrieve data from the session on the same server in subsequent page requests.
When you are done with the session, use: session_destroy()

Categories