1

Scenario:

I have a link that is

example/directory/go.php

I am 301 redirecting that link to

example/go.php

I use a session variable to track the path of the user. Without putting any variables in the url, is there a way I can still track in the session that the user went to

example/directory/go.php 

or rather intended to go there but obviously was 301'd to

example/go.php?

right now when I var_dump my session variable, it always shows the last page they visited as

example/go.php. 

which I guess make sense since it is being redirected with mod_rewrite, but I am just trying to see if there is anyway I could still see the redirected link in my session variable or through some other means.

absentx
  • 1,397
  • 4
  • 17
  • 31

1 Answers1

1

Using mod_rewrite you can set a cookie with the previous URL as a value and then get the cookie value in your final PHP script.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • That sounds reasonable, but how would I do it? Is there someway for php to tell that the link is being rewritten and then write that to the session/cookie? Or is this something I have to do in htaccess? Thanks for the help. – absentx Apr 11 '12 at 19:44
  • Since 301 redirection is happening in .htaccess so its got to be in .htaccess only. [Refer official mod_rewrite guide here and search for cookie](http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html) – anubhava Apr 11 '12 at 19:49
  • okay so I have the cookie created like so in my rewrite rule[CO=tracker:yes:example.com:1000:/]. Now, how do i access that cookie with my php script? – absentx Apr 11 '12 at 20:04
  • Just `echo $_COOKIE["tracker"];` should be good in PHP code. Check this doc also: http://www.w3schools.com/php/php_cookies.asp – anubhava Apr 11 '12 at 20:10
  • hmmm...I must not be setting the cookie right: RewriteRule ^example/directory/go\.php$ http://www.example.com/go.php [R=301,L] - [CO=tracker:yes:.example.com:1440:/] Do I need a separate rule first: RewriteRule ^example/directory/go\.php$ - [CO=tracker:yes:.example.com:1440:/] although neither seem to work. thanks again. – absentx Apr 11 '12 at 20:24
  • Can you try: `RewriteRule ^example/directory/go\.php$ example.com/go.php [L,CO=tracker:yes:.example.com,R=301]` – anubhava Apr 11 '12 at 20:32
  • oops I had another syntax error, both work now. Excellent thanks again! – absentx Apr 11 '12 at 20:36
  • One more quick question. If I want to destroy this cookie right away, or at the end of the script, can php do this? I am trying setcookie ("tracker", "", time() - 3600); unset($_COOKIE['tracker']); but the cookie ultimately still shows up. I need the cookie to be destroyed right away. – absentx Apr 11 '12 at 22:33
  • Please check: http://stackoverflow.com/questions/2310558/how-to-delete-all-cookies-of-my-website-in-php for deleting cookies from PHP code. – anubhava Apr 12 '12 at 04:51