My function like this :
public function index()
{
$stores = $this->store_service->getListStore();
return view('store.index', compact('stores '));
}
If the method called, it will call store page
I want to add validation server side. So if user open the store page, user can not back to previous page
How can I do it?
Include a middleware in the routes and check the request in the middleware. If the request is coming from stores (or whatever page you want to prevent the user to go) redirect the user to the previous page. This way even if a user click the back button, the user will be presented to the same page.
You can check for path like this: $request->path() and if the path is not what you want your user to go then redirect to the previous page
You cannot handle browser events using server side code. According to me you will have to push your URL in push state and clean the browser history. You can use below code.
$(document).ready(function(){
window.history.pushState(null, "", window.location.href);
window.onpopstate = function() {
window.history.pushState(null, "", window.location.href);
};
});
I want return response that can download and a redirect that can redirect to other page on the same controller function is it possible if so how can I do it if you could show me like this code if i want to download and then redirect to other page. on one controller.
public function handledown()
{
return Response::download(Input::get('book')) & Redirect::action('ViewController#book');
}
You can't return multiple responses like you are trying to do there.
My advice to you would be to return the response download and then redirect to a route you define within a data-redirect attribute on your button etc what ever you use, with jQuery etc.
Another solution can be found here.
How can I do PHP redirect header in Laravel 4?
In original PHP:
header('Location: http://www.example.com/');
I don't want to do with return Redirect::route('loginpage');. Because it does not work well with ajax. So, how can I do the PHP redirect with Laravel?
Thanks.
If you're wanting to get the url and refresh the page with an ajax function you could simply return the laravel route url and refresh the page with javascript:
// Use the route helper function
return route('loginpage');
// Javascript page redirect
var url = 'Your ajax response here';
window.location.href = url;
return Redirect::to('loginpage')
Is the most common way. Why doesn't that work well with ajax?
When using AJAX, your request isn't handled the way you're expecting it to be. This isn't a laravel-specific issue, as your header() location example would not work either.
If you want to make an ajax request that redirects the user, you'll need to have the response from ajax return something that you can key off of in javascript that executes a window.location change based on the results.
I have a website build in Code Igniter with a commenting system that loads a controller function with jQuery for each page.
I'm also passing the article ID as URI segment to the controller while loading.
Lets say the controller name is "comments" and the function is:
function get_comments(){
$article_id = $this->uri->segment(3);
echo 'the uri segment is '.$article_id;
}
I call them to my page with the following jQuery:
$(document).ready(function(){
$('.comments-holder').load('<?php echo base_url();?>comments/get_comments/'<?php echo $article_id;?>);
});
and this is fine.
The thing I want to accomplish is to forbid the user to load the "get_comment" function by typing the http://www.domain.com/comments/get_comments/
I believe that is possible not to have a solution for this problem a workaround will work good as well even if it means more changes to be done.
Code igniter has this by default.
Add this in the ajax action:
if ($this->input->is_ajax_request()) {
// your regular ajax code
} else {
// redirect or show error
}
I have a web page, let's call it main.php which displays an image of football field and some players distributed on the field. However, that page uses list.php as a right side frame that loads a list of players.
What happens is, when the user clicks on a player on the field (main.php), let's say on the image of the Goal Keeper (GK), a list of GKs from world wide teams will load in right list fram (list.php). This is by using ajax.
So far we are good.
The current situation is, when session times out and the user clicks on a player from the field, the list on the right does not load, instead, list of players disappears from the list and a message says "Please login" is displayed on the right side frame (list.php)
The objective is, when session times out I want the whole website to redirect to the main page index.php
The problem is, I already put the redirecting code just before the code that is responsible of displaying the message "Please login". But what happened is, the redirection happens from within the frame, so i ended up having main.php displaying the field, and list.php displaying the main page!
Here's the code I added.
$user_id = NSession::get('user_id');
if (!isset($user_id))
{
NSession::removeall();
General::Redirect('index.php');
}
They are using Smarty. and btw, I added the same code to top of main.php, and now if user tries to access main.php without logging in, it will redirect him to the main page, so the code works!
n.b. The project is not mine, it belongs to the company I work in.
And I don't know which code is checking the session, all what I know is, if the user click on a player from the field after the session timeout, the "Please Login" message will be shown in the frame.
I'm guessing the redirect is essentially the same as using a header() function. It isn't possible to specify a target using a php redirect as it is server-side - specifying the target is client-side.
You would need to print something like this to the screen:
<script type="text/javascript">window.open('index.php','_parent');</script>
And that will redirect the user to the index.
Using frames for such purpose is... well... so 80ish...
Anyway, the frames are probably named in such a scenario. This means you can address them, but also that you have to address them. Just loading an url inside the "current" frame does exactly that, which is why your approach won't work.
If you really have to go with that frame based approach, then you will have to use javascript to address all known frames and redirect them.
Maybe you can use some javascript inside of your frame like so :
<script type="text/javascript">
window.top.location = 'YourPage.html';
</script>
Hope this helps
The issue was that the session expires while I'm on main.php. Therefore, any subsequent Ajax requested will fail since all requests requires session to be active.
the problem was that the Ajax request being sent from the IFrame (the IFrame is inside main.php and points to list.php thru Ajax calls) is failing due to session expiry.
So I've fixed this issue by adding another two session checks, one on main.php, list.php using PHP (check for session, if it's there, redirect). And in the main container, main.php, I check for the session via JS, interval Ajax requests to check the session, if session has ended, then use redirect using JS.
PHP:
$user_id = NSession::get('user_id');
if (isset($_POST["checklogin"]))//check loging
{
die(isset($user_id) ? "true" : "false");
}
if (!isset($user_id) || $user_id == "")
{
NSession::removeall();
General::Redirect('login.php');
}
JavaScript:
jQuery(document).ready(function($) {
$(window).focus(function() {
checkSession();
});
});
function checkSession()
{
$.ajax({
type: "POST",
data: {"checklogin": "cl"},
url: "list_players.php",
success: function(result) {
if (result === "false")
{
if (FIELD.showMessage === false)
{
FIELD.showMessage = true;
alert("Your session has been closed\nYou will be redirected to login page now. ");
window.location.href = ("login.php");//incase user clicks OK
}
}
}
});
}