Marko Cakic
Marko Cakic

Reputation: 7106

Changing a PHP session variable via Javascript

I have a banner at my PHP website which can be closed by clicking on an image, using Javascript. But when a user reloads the page, or visits another page, the banner shows again, which I want to prevent. I need a way to access the session variable from that Javascript, in order to do it. Is there any way I can do this, or is there some way other than using the session variable?

I have seen that there are already some questions concerning this topic, but I haven't been able to succeed using those answers.

Upvotes: 0

Views: 933

Answers (1)

Sirko
Sirko

Reputation: 74086

You can't access the session variables directly, because they are held on the server and not on the client, where your JavaScript executes.

You can, however, send an AJAX request back to your server, requesting to set that session variable.

Another option would be to set a cookie value and retrieve that cookie value for the next site request.

In my opinion the first option is better, but depending on your system, the second approach might be more suitable in your case.

Upvotes: 2

Related Questions