There is a php file to which data is sent via ajax. In the php file, the validation and processing of this data takes place, but after that I need to redirect to the page where I will need this data in the future. Where is the best place for me to store this data during a redirect?

  • And what kind of data? The options are different, for example, cookies - Stanislav Belichenko
  • @ Stanislav is a simple json array. I heard something about the fact that this data can be stored in php-session, but I did not find an example that clearly demonstrates this. - JamesJGoodwin
  • well, there are just different arrays, for example, a basket in an online store, they also appear in cookies. In general, here: stackoverflow.com/questions/17242346/… - if it helps, I can repeat it in the answer - Stanislav Belichenko
  • @ Stanislav thanks, I will try. Such a question yet. And if I make an ajax request to a php-file in which there is a redirect through the header('location') , then the redirect will work? Or is it better to wait for the script to finish working and redirect it via javascript via window.location ? - JamesJGoodwin
  • Regarding the link above - I forgot to clarify that there is more likely a set of tips, as the real example strongly depends on the code that you write. Regarding the question in the previous comment - yes, it will work, if you make it right - first of all, not to display anything in any way before sending the header - Stanislav Belichenko

2 answers 2

In the session they belong

  1. Start a session by calling session_start()
  2. Fill an array of $_SESSION[]
  3. Redirect
  4. Start a session by calling session_start()
  5. Read data from the $_SESSION[]
  6. If the data is no longer needed, remove it from the array.

hello.php

 session_start(); $_SESSION['hello'] = 'World!'; header('Location: world.php'); 

world.php

 session_start(); echo $_SESSION['hello']; unset($_SESSION['hello']); 
  • And how do I properly remove the contents of the $_SESSION[] using JS? You can, of course, create an invisible <div style="display: none"> , stick data into it, parse it with JS and delete it, but you want to implement it all culturally, without crutches. - JamesJGoodwin
  • @JamesJGoodwin None. This is a server object. But nothing prevents it from displaying its contents on the page and using JS to parse the output - Anton Shchyrov

Your localStorage is fine for you. Variables of this object are stored at the browser level.

 localStorage.setItem('ключ', 'значение'); localStorage.getItem('ключ'); localStorage.removeItem("Ключ"); localStorage.clear(); 

Everything is intuitive. And here is a good, easy article:

tproger.ru/articles/localstorage/

  • will not work, there is no support by all browsers - Stanislav Belichenko