-1

In a PHP file, I've started a session with session_start() and then I defined a session variable $_SESSION["foo"]. How can I check locally (in my browser) if the session actually started or not, and what is the value of $_SESSION["foo"]?

Another question: In this PHP file, a defined a variable $bar = "foo";. How can I check locally (in my browser) the value of $bar?

(I'm new to PHP.)

PharaohOfParis
  • 184
  • 2
  • 10
  • 1
    Should take a look https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming – user3783243 Jan 16 '21 at 16:59
  • 1
    Suggested reading as you obviously have not get grasped what a session is https://stackoverflow.com/questions/3804209/what-are-sessions-how-do-they-work – RiggsFolly Jan 16 '21 at 17:27
  • Thanks for those links. Now I understand better what a session is, but my question was how to get/read the variables from it. – PharaohOfParis Jan 16 '21 at 17:36

1 Answers1

2

You have to understand the difference between server-side and client-side languages. PHP is server-side and therefore your browser will never know anything that happens in the PHP code, as long as it is not returned to the client (the browser in this case).

You can change your code (on the server!) to echo out data that you want to see in your browser, like so:

echo $_SESSION["foo"];
echo $bar;

This will send the value of the respective variable to the client. Apart from that (or other methods of sending data to the output, like using e.g. var_dump()), you can not see the values in your browser!

Now for your first question about the session there is a bit of a speciality: While you can not directly "see" the session in the browser, in the default configuration of PHP you will get sent a session cookie that is named PHPSESSID to your client. You can infer from that, that a session was indeed started by looking it up in the response headers.

ArSeN
  • 5,133
  • 3
  • 19
  • 26
  • Indeed, I saw the cookie `PHPSESSID` in the Chrome DevTools. But I cannot see its details (like its variables). Where can I look for the response headers? – PharaohOfParis Jan 16 '21 at 17:24
  • 1
    You only get the cookie in the response, not the session contents (those stay on the server). You can find the response headers in the Chrome DevTools as well in the network tab. – ArSeN Jan 16 '21 at 17:42