The importance of managing session data

Here is my issue: I want only one session to run at a time. Consider the following query:

$get_crs_mysqli .= (!empty($_SESSION['userSearch']))?(" AND (course_title like '%".$_SESSION['userSearch']."%') OR (course_title like '%" . $_SESSION['userCategory'] . "%')  ") : '';

and

//getting the providers
function getProviders(){

            if(isset($_GET['user_query']))
    {
                  $search_query = $_GET['user_query'];
    global $con;
    $get_providers = "select DISTINCT course_provider from courses where (course_title like '%$search_query%' ) AND course_date1 >= CURRENT_DATE() LIMIT 5";
    $run_providers = mysqli_query($con, $get_providers);

    while ($row_providers=mysqli_fetch_array($run_providers)){
        $provider_title = $row_providers['course_provider'];

        $numberIncrease = 0;

echo '<a href="" id="liSpacing"><label id="labelSearch"><input class="filter" name="provider' . ++$numberIncrease . '" type="checkbox" value="' . $provider_title . '">&nbsp;' . $provider_title . '</label></a> <br />';

}

    }

     else if(isset( $_GET['crs_category']))
    {
                  $search_query2 = $_GET['crs_category'];

    global $con;
    $get_providers = "select DISTINCT course_provider from courses where (course_title like '%$search_query2%' ) AND course_date1 >= CURRENT_DATE() LIMIT 5";
    $run_providers = mysqli_query($con, $get_providers);

    while ($row_providers=mysqli_fetch_array($run_providers)){
        $provider_title = $row_providers['course_provider'];

        $numberIncrease = 0;
}
}
}

Below is the section that deals with session variables:

 <?php 
 if(isset($_GET['user_query']))

    {
       $search_query = $_GET['user_query'];

 $_SESSION['userSearch'] = $search_query;


  $paginationresults = mysqli_query($con,"SELECT COUNT(*) FROM courses where course_title like '%$search_query%'  AND course_date1 >= CURRENT_DATE() ORDER BY course_date1 ASC");
$get_total_rows = mysqli_fetch_array($paginationresults); //total records
$item_per_page = 10;
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page); 

}

if(isset($_GET['crs_category']))
    {
       $search_query_category = $_GET['crs_category'];

 $_SESSION['userCategory'] = $search_query_category;


  $paginationresults = mysqli_query($con,"SELECT COUNT(*) FROM courses where course_title like '%$search_query_category%'  AND course_date1 >= CURRENT_DATE() ORDER BY course_date1 ASC");
$get_total_rows = mysqli_fetch_array($paginationresults); //total records
$item_per_page = 10;
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page); 

}


?>

The challenge arises when trying to have two sessions running simultaneously, causing conflicts and errors. I need to modify it so that only one active session is accessed. For example, if a user searches for a category, the URL appears as searchPage.php?crs_category, and if they look for a specific item, it shows as searchPage.php?user_query=html. The problem occurs because the previous session remains active and interferes with the search results.

I hope this explanation is clear. Please let me know if you need further clarification.

Answer №1

Make sure to effectively clear the previous session values before setting new ones.

$_SESSION['userInput'] = $new_input;

unset($_SESSION['oldInput']);

also,

$_SESSION['userPreference'] = $new_preference;

unset($_SESSION['oldPreference']);

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

Retrieving attribute values in PHP using XPath

Similar Question: How can I extract an attribute from an XML node using PHP's DOM Parser? Can someone help me with extracting the value of an HTML tag? Here is the HTML code snippet: <input type="hidden" name="text1" id="text1" value="need ...

simplify sending emails through WAMP and a proxy server

For the past couple of days, I've been attempting to send emails from my PHP application that's running on my PC with WAMP. I made adjustments to the php.ini file as follows: [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = mail.my ...

Leveraging NPM and MYSQL to transfer query outcomes to a different variable

I'm in need of assistance with a database package I'm developing for a larger application. The goal is to store specific data in a database using the mysql package in npm. However, I'm encountering an issue where the results variable always ...

Unexpected results obtained from database with PHP fetch_assoc() function, loop malfunctioning due to potential integer conversion issue?

Here is the data retrieved from my query: T t0 t1 t2 t3 1390716665000 137 47 82 1390717359000 105 47 79 1390718399000 126 46 79 This is what my PHP code looks like: $i=0; while( $row = $result->fetch_assoc() ){ $RowTime = ( ...

Include a new XML child element using PHP

I've encountered an issue with my PHP code while trying to add multiple nodes from an XML file. Currently, the code only adds the first node, but I need it to include all child nodes under the name 'node'. XML Structure: <root> <re ...

Optimizing PHP code by elimiating unnecessary queries in generated URL strings

Currently, I am dealing with a situation where I have a string that generates mp3 URLs for a music player on my website. <?php echo $song->getTitle() ?> After executing this code, the result is /public/music_song/df/74/746b_2112.mp3?c=ec1e My g ...

Query to retrieve the most recent chat message between two users in a private chat system using MYSQL

Continuing from my previous query. Private chat system MYSQL query ORDERBY and GROUPBY In the meantime, I have introduced a new table named users where user information will be retrieved. Here is the structure of the messages table: message_id|sender ...

What is the best way to organize a list of dates in PHP?

I've encountered an issue with the array_push and array_unshift methods. When I try to order the elements, it mistakenly combines dates from December 2020 with those of January 2021. Let me illustrate this problem with an example. foreach ($lo ...

Is there a way to showcase pre-existing php variables on a newly loaded view through an AJAX call within the context of an existing page?

Using CodeIgniter framework, I have implemented a web page that displays clients. The view called "clients.php" lists the clients in a table format. It presents the client information like this: <?php foreach ($clients as $client) { ?> <tr> ...

Ajax is known for its uncanny ability to consistently encounter errors

Having trouble with my ajax call, it keeps ending up in the error part. Can someone please assist me and point out where I may be going wrong? I even checked by writing the final value to a text file and everything seemed fine. The URL is correct as well. ...

How do I switch between liking and unliking a post using Ajax's success response?

I have successfully implemented code to insert or delete likes from the database, and it functions correctly upon page refresh. However, I am struggling to figure out how to make it toggle upon clicking. I have attempted various online solutions without su ...

Use a dropdown menu to update the selected value

Issue with displaying drop down values in the second list, despite trying various solutions. When a user selects a country, the corresponding state should be populated from the database into the second drop-down. Any assistance would be greatly appreciated ...

Combining rewrites with various origins within location blocks

I'm finding it difficult to figure out how to manage rewrites for PHP scripts when the location blocks that handle the rewrites have different root directories. Let me give you a simple example. My catch-all front controller needs to be in the web ro ...

What steps can be taken to resolve an HTTP Error 500 when encountering it in a PDO execute

I've been attempting to run a query using php PDO, but the execute method is triggering an HTTP ERROR 500. $query = "select * from job_t where title like '%:title%' and salary>=:salary"; $st = $conn->prepare($query); $st->bindParam ...

Retrieve information from a remote, dynamically generated text file using PHP

I'm currently attempting to retrieve data (using PHP) from a remote text file. The URL of the file is website.com/page.php?info=someinfo. Although it's not a .txt file, as the content consists entirely of plain text. When inspecting the source, t ...

Tips for Avoiding Line Breaks in CSS Divs

I am currently designing a website menu with items such as Home, Contact us, and About us. Each item is styled with a background color and text size of 125X30. I initially used the float property in CSS to align them correctly in a single line from left ...

PHP Elasticsearch Clustering for Optimized Data Searching

I currently have a PHP application that relies on ElasticSearch for search and insert operations. To improve the speed of my application, I recently implemented clustering in ElasticSearch with a total of 3 nodes (including data nodes). The initial node ...

Receiving an UNDEFINED INDEX error when trying to send data from an HTML form to a

I've created an HTML form that is styled using CSS for weekly work data entry. <form action="wwinsert.php" method="post"> <fieldset> <legend>Weekly Work</legend> <p><label class="lab1" for="StoreNumber">Store:</ ...

What is the process for exporting a chart into Excel?

My current challenge involves displaying data extracted from a database in the form of a bar chart and then exporting both the data and the chart as an image into an Excel file. While I have successfully displayed the bar chart, I am facing difficulties in ...

Half of the data is missing when passing data through Laravel AJAX

My attempt to store data in a database using ajax isn't completely successful. While one ajax request works fine, the other one doesn't. Below is the ajax script that is causing issues: <script> $( document ).ready( function() { $(" ...