0

I've got a couple of PHP scripts which I call functions from using XMLHttpRequest in JS from my front end.

If I hit the PHP scripts directly in the browser, the session (session_start();$_SESSION["working"] = "true";) and session variables are retained, but if I call them from the webpage using XMLHttpRequest they are not.

Is this expected behaviour, as PHP has no way of knowing the session is still active when calling it with XMLHttpRequest?

I was hoping (not expecting), that PHP would somehow know the session was still active.

Works fine if I access the PHP files directly in the browser, even if I switch between different files, but not when called from JS.

Tink
  • 11
  • 2
  • If the `XMLHttpRequest` request is made from the same domain and to the same URL that works fine when you visit it manually, then it should already work fine. On what URL does your script run? And to what URL are you making the request? Are there any errors in the console of your browser's Developer Tools? – Ivar Dec 12 '22 at 13:38
  • I'm not sure about your exact scenario. However, generally, PHP sessions generate a cookie on the client side called `PHPSESSID`, if you send that cookie along with your `XMLHttpRequest`, it should work. read more: https://stackoverflow.com/questions/1370951/what-is-phpsessid – Michael Yousrie Dec 12 '22 at 13:41
  • 1
    Thank you, was a stupid question and your answer made me think about where I was running it from. The PHP scrips where on my server, the HTML file running locally. Once I put the HTML on the server, it works fine. – Tink Dec 12 '22 at 13:42

0 Answers0