67

I've searched around for use of localStorage in PHP, as I need to get localStorage data as a $var, in PHP and do stuff in PHP with it. Is this possible?

  • 1
    You can read http://diveintohtml5.info/storage.html – yPhil Sep 11 '13 at 13:44
  • 1
    I see that this page has entered the reopen review queue. The thing about reopening isn't just that the question is valid; it is that it must also be a good question. These early SO questions tend to be requirements dumps or essentially ask for help without a shred of posted effort. Compare this page to the more recent: https://stackoverflow.com/q/25191944/2943403 & https://stackoverflow.com/q/21336996/2943403. SO will invariably grow better content if more complete questions are posted. I am not sure if I'm on board with reopening this one because it sets bad precedence regarding how to ask. – mickmackusa Dec 24 '20 at 20:24
  • 1
    Also in this dupe cluster: https://stackoverflow.com/q/22609986/2943403 , https://stackoverflow.com/q/49829703/2943403 , https://stackoverflow.com/q/39038225/2943403 , https://stackoverflow.com/q/44834919/2943403 , https://stackoverflow.com/q/10923813/2943403 , https://stackoverflow.com/q/30163855/2943403 , and many more. – mickmackusa Dec 24 '20 at 20:36
  • 1
    Since this question is seeking a complete tutorial / off-site resource AND the answer shows about as much detail as the question, I am not compelled to reopen this old, low-value page. – mickmackusa Dec 24 '20 at 20:38

1 Answers1

131

localStorage is something that is kept on the client side. There is no data transmitted to the server side.

You can only get the data with JavaScript and you can send it to the server side with Ajax.

John R Perry
  • 3,916
  • 2
  • 38
  • 62
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 53
    If php can access cookies, why the hell can't it access localStorage? – Braden Best May 07 '13 at 15:43
  • 64
    @B1KMusic: Because cookies are sent to the server in the request headers. It's not that PHP can access the client side. The browser sends the cookie data to the server. – Felix Kling May 07 '13 at 22:10
  • I know this is correct, but could you update the answer to show how? – DavidG Apr 21 '18 at 19:09
  • @FelixKling and this is true of **every request**. If you have a large cookie, it will get sent to the server on ever URL that is in the same origin. It can actually generate a lot of extra bandwidth use. – Nelson Dec 18 '19 at 06:47
  • 1
    @FelixKling That explains the server READING cookie data, so how about WRITING it? – Chuck Le Butt Sep 17 '20 at 11:55
  • 6
    @ChuckLeButt: The sever can send `Set-Cookie` headers in the response. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie – Felix Kling Sep 17 '20 at 12:03
  • @FelixKling Exactly. – Chuck Le Butt Sep 17 '20 at 12:21