2

I have two pages. The page 2 is accessible by the page 1.

The thing is I want to detect if someone came back to page 1 by the page 2 with a return button in order to display an other thing.

      PAGE 1 -------------------------> PAGE 
 Normale Display             Create SESSION or COOKIE
                                         |
                                         |    
If SESSION or COOKIE  <--------- Pressed back button
-> Secondary Display

So I tried to :

  • Create a cookie but it wasn't read
  • Create a Session variable but it wasn't read neither
  • Empty the cache but I ended up with a warning message
  • Local Storage / Session Storage still react like a Cookie

Does anyone see a solution for me, either to force the read of one of those thing or force the page to acte as if it was a normale way of going to it when back button pressed?

Im open to any solution It's been two long days going around in circle.

EDIT :

Let change a bit my question by : How to detect if a page is reach using back button?

So I've seen this question but its an old one (2009) and an answer using Iframe which if I could avoid I'd like too.

Baldráni
  • 5,332
  • 7
  • 51
  • 79
  • Why session did not work? – Michal Jul 20 '16 at 13:48
  • I've just edited my question. – Baldráni Jul 20 '16 at 13:51
  • what about some code? probably cookie and session didn't work just for a coding issue? – Lelio Faieta Jul 20 '16 at 14:12
  • They are working perfectly well this is actually the native way of back button working that is problematic. It display back the exact same page you left without refreshing it so it can't reach eaither new Cookies either new $_SESSION variable. Try it yourself if you want, generate a session variable on a page and see if it still exist when back button pressed ;) – Baldráni Jul 20 '16 at 14:14

3 Answers3

0

The easiest way is to check for $_SERVER['HTTP_REFERER'] but if someone has multiple tabs opened while looking at your website it will not work properly.

I do not think that there is any "nice" solution to your problem.

Edit: I guess that better question would be to ask what are you trying to achieve specifically. Why do you need to know if someone came to page 1 by the page 2 with a return button? Is there any other way how to solve your problem?

Michal
  • 4,952
  • 8
  • 30
  • 63
  • Already tried, it depends on browser and a lot of factor and is indeed not really reliable. – Baldráni Jul 20 '16 at 13:42
  • I juste edited my answer. Is there any other solution to your problem? – Michal Jul 20 '16 at 13:42
  • Its complicated but it is due to modification of a form which called an Ajax method which I need to remember the value when coming back to the page by the return button – Baldráni Jul 20 '16 at 13:45
0

You can do that by changing the local storage I guess.

When page 2 is initialized, check if the precise storage of your local storage is empty or not. If not, display what you want in addition.

To do that you must put a controller which does that when he is called and don't forget to call it when the page 2 is loaded.

But you need to empty that local storage. Consequently you may want to reset it each time you go on another page except the seconde one.

It's kind of dirty, but it should work.

NB : It's pretty easy to do that with Angular JS. With pure Javascript it may be not that easy to do, but I'm pretty sure it is possible.

Hope that could help.

Alburkerk
  • 1,564
  • 13
  • 19
  • What if you open multiple tabs? You delete local storage when using page 3. What happens when you hit back button on page 2? – Michal Jul 20 '16 at 14:00
  • Sadly the localStorage react as a Cookie and the variable is not get instead there is a refresh on come back. – Baldráni Jul 20 '16 at 14:41
0

Ok so the solution was to send variables via history of url's.

Since HTML5 you can modify the history of navigation.

So basically the idea in my case was to send values in the url as in a get request :

yoursite.com/where_you_are?something=value

And for doing that you can modify url history with : history.replaceState({},'',newURL)

Eventually if you want to send object to your page you can use {} like this {myobject: value, ...}.

Hope this could help and have a look at the great documentation by mozilla linked above.

And eventually have a look there too

Baldráni
  • 5,332
  • 7
  • 51
  • 79