Creating javascript function to destroy php session

后端 未结 4 1427
鱼传尺愫
鱼传尺愫 2021-01-15 03:09

I am having trouble figuring out how to create a javascript function that will destroy a php session. I have a clickable link that will call a function called destroyphpses

相关标签:
4条回答
  • 2021-01-15 03:29

    Your JavaScript runs client-side, and your PHP runs server-side. You can't call PHP functions from JavaScript this way. You have two options:

    Do an AJAX call to a server-side script that clears the session data (recommended, for consistency and proper clearing of that stuff server-side) Clear the PHPSESSID cookie with JavaScript (won't work if you reconfigure how you handle sessions, or if sessions are handled by URL parameters)

    0 讨论(0)
  • 2021-01-15 03:32

    Everyone keeps saying AJAX. This would be a good idea if you want to clean up the cookie on the server side as well, but I think if you knew what AJAX was you probably would have used it already...

    I found AJAX pretty confusing when I first came across it. So what I would suggest is having a look at and maybe using jQuery's AJAX functionality. Including jQuery for just one function is probably overkill, but it might be a bit easier.

    0 讨论(0)
  • 2021-01-15 03:36

    A PHP session generally uses a cookie that is stored client side. The following code will clear that cookie thus unlinking the session.

    document.cookie = 'PHPSESSID=; expires=Thu, 01-Jan-70 00:00:01 GMT;';

    0 讨论(0)
  • 2021-01-15 03:41

    Your JavaScript runs client-side, and your PHP runs server-side. You can't call PHP functions from JavaScript this way. You have two options:

    1. Do an AJAX call to a server-side script that clears the session data (recommended, for consistency and proper clearing of that stuff server-side)
    2. Clear the PHPSESSID cookie with JavaScript (won't work if you reconfigure how you handle sessions, or if sessions are handled by URL parameters)
    0 讨论(0)
提交回复
热议问题