Concluding the session upon exiting a directory or specific web addresses

Is there any way to terminate or cancel a session when I depart from a specific folder on the website, like /maps/, or simply end it when I'm not certain URLs?

At present, I am utilizing:

<script>
$(window).on('unload', function() {

    var fd = new FormData();
    fd.append('ajax_data', 22);

    navigator.sendBeacon('resources/ajax/unload.php', fd);
});

</script>

This invokes the following code:

<?php require_once("../config.php");
 ignore_user_abort(true);
 date_default_timezone_set("Asia/Tokyo");
 $time = date("H:i:s");
 $date = date("Y/m/d");
 $query = "uknown";
 $session_id = session_id();
 $session_prev = $_SESSION['prev'];
 $ip = $_SESSION['ip'];

 $sql = query("INSERT INTO navigated (navigated_from, navigated_to, 
 navigated_date, navigated_time, session_id, session_ip) VALUES ( 
 '$session_prev', '$query',  '$date', '$time', '$session_id', '$ip' 
  )");


 if (!isset($_SESSION['ended'])) {
 date_default_timezone_set("Asia/Tokyo");
 $_SESSION['ended'] = date("H:i:s");

 }

 $session_end = $_SESSION['ended'];
 $session_start = $_SESSION['started'];
 $session_date = $_SESSION['date'];
 $session_clicks = $_SESSION['clicks'];
 $sql2 = query("INSERT INTO session_time (session_id, 
 session_start, session_end, session_date, session_clicks, 
 session_navigated, session_ip) VALUES ('$session_id', '$session_start', '$session_end', '$session_date', '$session_clicks',  '$query', '$ip')");

unset($_SESSION['ended']);
unset($_SESSION['date']);
unset($_SESSION['started']);
unset($_SESSION['clicks']);
session_regenerate_id();


?>

Terminates sessions immediately when I leave the page or trigger an input.

Answer №1

Perhaps, based on your specific requirements, an option could be to create an unset_maps_session() function that incorporates all the necessary steps to unset the session for the /maps/ folder.

You can establish a common function named common_tasks(), which will be utilized in every file within your project. The purpose of this common_tasks() function is to identify tasks that need to be performed on a given URL. By employing regular expression checks on the URL, you can determine what actions need to be carried out.

function common_tasks() {
    // Utilize regular expression checks on the $_SERVER['URI'] parameter
    // Suppose there is a boolean variable called matched_maps_url_not_found that indicates whether the current URL
    // is not a maps URL.
    
    if(matched_maps_url_not_found) {
        unset_maps_session();
    }

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can a LiveCode translator be developed for my app that can translate all text within the application?

I'm interested in developing a translator within my LiveCode app that can translate any text. I attempted to integrate Google Translate, but it didn't work as expected. Thank you! ...

Sending data using jQuery's AJAX feature

Here is an example of the code I have: var loadUrl = 'test.php'; var dataObject = { category_id: category_id, grade_val: grade }; jQuery.ajax({ type: 'POST', url: loadUrl, data: dataObject, dataType: 'html', ...

How to Invoke JavaScript Functions within jQuery Functions?

I'm working on a jQuery function that involves dynamically loading images and checking their width. I have two JavaScript functions, func1() and func2(), that I want to call from within the jQuery function in order to check the width of each image. T ...

Building interactive form elements using jQuery Mobile

Hello! I have a question that I couldn't find an answer to through search... I'm currently exploring jQuery Mobile and trying to dynamically add radio buttons to a <fieldset> container within a page. You can view my code in this fiddle: ht ...

Troubleshooting issues with sending email with attachments in PHP and HTML

I've been having trouble sending an email with an attachment file; it just doesn't seem to work and I can't seem to receive any files. Although my HTML syntax appears correct, as I haven't received any bounce-back emails, the issue per ...

Tips for identifying the clicked location inside an element using JavaScript?

Is there a way in JavaScript to find out the exact position of a click within an element, like its distance from the left, right, or center? I'm struggling to determine whether the clicked area is on the left, center, or right side. https://i.stack.i ...

Enable the button if at least one checkbox has been selected

I've written some code similar to this: $('input[type=checkbox]').click(function(event) { $('.chuis').each(function() { if(this.checked) { $('#delete_all_vm').prop("disabled",false); } ...

Steps to record and upload a video directly to the server using getusermedia

For my application, I need the user to record a video and have it saved on the server disk instead of downloading it to the client browser. I am currently using getusermedia for this purpose, with the following code: (function(exports) { exports.URL = exp ...

Save all dynamically received data from a form in PHP as an array

I have developed a PHP form that dynamically adds new text variables in this manner: <form action="" enctype=”multipart/form-data” method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>"> <div id="div"> va ...

Sending emails with SMTP in JavaScript using the mailto form

I'm facing a challenge with my form. I am looking for a way to have the Send-email button trigger mailto without opening an email client, instead automatically sending via JavaScript (smtp). I'm not sure if this is achievable or if I'm askin ...

Grab the ID parameter from the URL using PHP by accessing the `id` key in the `$_GET` superglobal

Is there a way to utilize the .$_GET['id']; After Domain.com in this manner? Domain.com/.$_GET['id']; ($curl = curl_init('https://nombre.is/stream.php?mid=.$_GET['id']');) Whenever I attempt it, it seems to conf ...

html Tips for referencing a file in an input tag

Imagine you have a basic form with an <input type="file"> tag. What I aim to achieve is, once the user selects a file (which is an image), I want to display that file on the same page for viewing - providing a better user experience. I attempted to ...

Dynamic content loading using AJAX to selectively update a designated section

I am having trouble displaying the error message in my form. Currently, it is causing my form page to load four times. Can someone help me identify what I did wrong? I only want to display that span: controllers: <?php /* * File Name: user.php */ ...

Implementing a keypress function that can handle duplicate names

HTML <input name="pm" type="text" value="0"/> <input name="pm" type="text" value="0"/> <input name="pm" type="text" value="0"/> <input name="total" type="text" value="0" disabled="disabled"/> Javascript $('[name="pm"]' ...

filling a dynamic selection menu with a pair of parameters

I am facing a challenge with two variables set up to populate a dynamic dropdown. Both variables are arrays and run through for each. The first variable queries the database to determine the currently assigned staff member's name and displays it in th ...

Can an onload function be triggered within the location.href command?

Can a function be called onload in the location.href using jQuery? location.href = getContextPath() + "/home/returnSeachResult?search=" + $('#id-search-text-box').val() + "&category=" + $('#search_concept').text() + "onload='j ...

What is the best way to eliminate duplicate values from a column in a data set

Time To Status Off-Track On-Track 10:28 AM gf 6233 4 4 10:32 AM dd 3835 7 10:40 AM ss 3235 4 10:43 AM ww 6621 5 11:06 AM zz 3837 ...

Is it possible to transfer data between the beforeSend and success functions within the jQuery .ajax method?

The Issue When utilizing AJAX to request data from a remote API, the asynchronous nature of the request means that responses come back whenever they are ready. The challenge arises when making multiple calls to the same API with different parameters, as i ...

Unable to display ChartJs within the same DIV on the webpage

I'm struggling to display the Pie Chart from ChartJs in the correct DIV when I click from the left DIV. Running the chart directly works fine, but I want them both on the same page in separate DIVs. I have separate HTML files for each link and they wo ...

Control the HTML button's state according to the information received from the server

I am currently working with datatable Jquery and using an ajax call to retrieve data from the server. Let's assume that the database consists of three attributes: "Attribute1, Attribute2, Status". Depending on the Status attribute, I need to enable or ...