Help with PHP session

Asked

Viewed 57 times

1

Good morning!

I have a php system where I use a webservice to perform queries, and save your return in the session to use via ajax.

So far so good, the problem is when I need to make two or more simultaneous consultations, example:

I open two tabs in the browser is performed two different query, after the return of the webservice saved your data in the session:

Consulta 1: Session::set('consulta', $consulta);
Consulta 2: Session::set('consulta', $consulta);

When query 2 is saved in session it overwrites query 1, because the name I am passing to session is the same.

I could change the session name by generating a Random number or passing some parameter that corresponds with this query, example:

Consulta 1: Session::set('consulta1', $consulta);
Consulta 2: Session::set('consulta2', $consulta);

But the real problem is in the ajax calls, today I perform ajax calls to my php file that reads the session Session::get('consulta'), how do I get ajax to access this session if I change the session name for each query?

You would be able to log in per browser tab, like the Java JSF @Viewscoped. Or if you have some other form with no session usage?

Obs: I can have up to 10 simultaneous searches, which will serve 10 saved searches in the session.

1 answer

1

I think you can do as follows, in your query session you will store an array of queries and not just a query. Whenever you make a new query, you take the value of your session, increment the new query in this value and arrow to session the array updated, so you won’t overwrite the current value. With this your ajax query need not change the name of the query, you will only need to treat to get the last position of the array consultation.

Abstraction of the code.

// Aqui é sua sessão com a primeira consulta
$_SESSION['consulta'] = [
    [
        'id' => 1,
        'nome' => 'Teste'
    ]
];

// Aqui é o que você vai fazer quando a 2 consulta for executada
$consultas_antigas = $_SESSION['consulta'];
$nova_consulta = $consulta; // Aqui você executa sua query ou que for necessário para gerar sua consulta

// Agora você atribui para a sessão o array das consultas antigas mesclado com a nova consulta
$_SESSION['consulta'] = array_push( $consultas_antigas, $nova_consulta );
  • I understood your logic, but I got a point in question that is the following: you mentioned to treat ajax to take the last position of the array, but assuming I have two searches open, the second search will work perfectly by taking the last position of the array, but how would ajax get its query inside the array if it is not in the last position? Like, for example, old research.

  • I couldn’t quite understand your question, you mean like this, you made 2 queries and it worked blz, so now you want to take the number 1 query again, which in the case it is already old. Would that be?

  • Yeah, that’s right.

  • Here I thought of 2 possibilities, the first you add a parameter to your ajax, for example the query ID, and so you can locate it inside the array. The second is whenever you make a query, before you merge what already exists with the new query, you check if it already exists in your session, if it exists you remove it and put it as the last item in the session array. So your ajax will keep picking up the last item and will work in any situation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.