0

I want to mimic the behavior of elpais.com for my Drupal website. It works as follows:

If you visit elpais.com and go to any section, like brasil.elpais.com, and then you close the tab/window, and then revisit elpais.com, the system automatically redirects to brasil.elpais.com. As you notice, the system saves the user last closed location.

I want to make this for Drupal website; particularly for any page.

What I have done so far is very simple:

  1. Added the cookies.js file to the misc folder. (The code for this JavaScript file was gotten from How do I set/unset cookie with jQuery?.)
  2. In the page.tpl.php file I call the file using this drupal_add_js('misc/cookies.js', 'file'); to embed the JavaScript code for cookies management.

I have the basic idea: Save the current URL to a cookie -using JavaScript-, and then restore it when the user visit any other page.

What do I need to perform that? Ideas, suggestions, etc.?

Community
  • 1
  • 1
InfZero
  • 2,944
  • 4
  • 24
  • 36

1 Answers1

1

First, I don't see why you want to do that from JavaScript. Use the PHP and store current path to cookie or to PHP session every time user opens some page. Then, make some logic that when page is opened you check for that cookie/session value and make redirection to stored path.

Drupal has goto function for making redirections:

https://www.drupal.org/node/2023537

But watch not to make redirection every time, since you'll make it impossible to visit any other page except first one you visited. Add some extra conditions for redirection, i.e. redirection is made only if you are not coming from some other page of your site (check out $_SERVER["HTTP_REFERER"] php variable).

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • This is what I done so far: http://pastebin.com/gnbczNKV in page.tpl.php It does not work correctly. What it does is, apparently, omitting the redirection. What do you suggest me to modify? Thanks – InfZero Sep 19 '16 at 16:40
  • Hmm, not sure that you can execute any other code after calling header function, since it will redirect you. Try moving setcookie() above header(); – MilanG Sep 20 '16 at 09:21