Search code examples
phpsession-cookies

Include session on server for assets but prevent session Cookie header from being sent for assets


Someone recently pointed out that the asset files were also sending the session Cookie header, not necessary. I would like to access a few arrays/variables stored in $_SESSION in some of the asset files though without having the Cookie header be sent to the client for those asset files.

In the asset files (e.g. like JavaScript) I simply include the session.php file. Since the client initializes the HTTP request to the asset files this is not simply passing $_SESSION as a parameter.

How do I initialize and access $_SESSION though not have it's associated Cookie header sent for asset files?

I can obviously determine how to combine substr with $_SERVER['REQUEST_URI'] to determine if the request is for an asset (e.g. *.js) and all asset files have proper file extensions.


Solution

  • if (!headers_sent()) {header_remove('set-cookie');}