cneeds;10963911 wrote:
You didn't mention $COOKIE. Is it for this or was it designed with other purposes in mind?
Well, I suppose they were indeed ment to pass data to and from the browser, but since $SESSION usually gives you everything you need by using nothing but a session cookie (which you don't set manually, it's handled by session_start()), there is rarely, if ever, need for ordinary cookies. Moreover, some browsers lets users disable normal cookies while still allowing the session cookie.
Also, on every request to your server, all cookies are sent along in the request. Separate requests are made for css files, js files and all <img> elements among other things...
The only advantage for cookies are that they can be accessed directly through javascript, with the restriction that they need to follow the "same origin policy". But you can circumvent this need for cookies by using php scripts instead of plain .js files to make your session data accessible from js.
file.js.php
<?php
session_start();
?>
/*
js code...
*/
// need some of your session data here
<?php
$var = isset($_SESSION['var']) ? $_SESSION['var'] : '';
echo 'php_session_var_as_js_var = ' . $var .';';
?>
alert(php_session_var_as_js_var);
<script type="text/javascript" src="/file.js.php"></script>
But do keep in mind that js files (even if they end with .php) will be cached by the server, so if you do update them you'd need to change the src="", possible by adding a querystring timestamp of the last change.
Otherwise, you can keep your session data in the head section of the page instead
<html>
<head>
<script type="text/javascript">
<?php
$var = isset($_SESSION['var']) ? $_SESSION['var'] : '';
echo 'php_session_var_as_js_var = ' . $var .';';
?>
</script>
<script type="text/javascript" src="file_that_needs_the_above_var.js"></script>
</script>