Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to automatically create session to match the page?

Hello,

I have a simple JavaScript script that filters data and sends it to the backend via AJAX, then saves the data in a session.

I then use this data in the session to display results in the same page it was used (So that after a page refresh that data that was filtered will be saved)

Currently this script is on 1 page, so I manually added AJAX route in web.php and another method in the controller to save the session. But I manually created the session with a name I gave it.

Now I want to use this script in many more pages. How can I make the script work "globally", so that I won't need many routes for the AJAX to save the session and manually create session per page, yet keep the sessions match the page the filters were used in?

I was thinking to use the URL as a reference and have single AJAX route, send the page URL the script worked from, and have the data saved in a session with a name that matches the URL.

Is it OK? Or not? And are there better ways to do it?

Thanks

0 likes
3 replies
LaryAI's avatar
Level 58

One solution to this problem is to use the URL as a reference and create a session with a name that matches the URL. This can be done by passing the URL as a parameter in the AJAX request and using it to create the session name in the controller.

Here's an example of how this can be done:

  1. In your JavaScript code, add the current URL as a parameter in the AJAX request:
var url = window.location.href;
$.ajax({
    url: '/save-session',
    type: 'POST',
    data: {
        filters: filters,
        url: url
    },
    success: function(response) {
        // Handle success
    },
    error: function(xhr) {
        // Handle error
    }
});
  1. In your controller, use the URL parameter to create the session name and save the data:
public function saveSession(Request $request)
{
    $filters = $request->input('filters');
    $url = $request->input('url');
    $sessionName = str_replace('/', '_', $url); // Replace slashes with underscores
    session([$sessionName => $filters]);
    return response()->json(['success' => true]);
}
  1. To retrieve the session data on a specific page, use the current URL to get the session name and retrieve the data:
$url = window.location.href;
$sessionName = str_replace('/', '_', $url);
var filters = {!! json_encode(session($sessionName)) !!};

This way, you can use the same AJAX route and controller method for all pages, and the session data will be saved and retrieved based on the URL of the page.

Snapey's avatar

Send a session key with the ajax request to s single endpoint. Then save the session data as an array under the provided key.

1 like
Ligonsker's avatar

@Snapey I just wanted to skip the part where I need to manually insert a session key because if I have many pages that use this script it's cumbersome to manually create sessions keys. So I thought about automatically sending the key as the current URL from JS (window.location.href), and then in the controller methods that are related to that page, use Laravel's $url = $request->url();. This way I won't need to worry about adding session keys myself everywhere - Is it OK, or not a good idea?

also, do you know why LaryAI suggested to replace slashed with underscores for the session keys?

Please or to participate in this conversation.