I am writing a custom script and learning about php sessions. I know that you must start the session before the <head> tag. I then want to add variables to the session. The variables are not generated until some javascript runs at the end of the page. If I want to modify the session with some code that is generated with the javascript, how do I go about doing that? Is it as simple as just opening the session and then running the following code at the end of the page to change the session variable <?php $_SESSION['size']='12'; ?> Code (markup):
Javascript is clientside, sessions are serverside. Why is javascript necessary to calculate the session variables? Can't you do the same in PHP? It's possible to accomplish what you say via a hackish way, but I don't recommend it unless absolutely necessary.
Ok I see what you are saying. I can calculate the variables using PHP. I still have another question, the session is opened before the header but the calculations still wouldn't be done until near the end of the html. Can I still write to the session then?
You don't even need ob_start(). Just put session_start(); at the top of your page. You do not have to populate your session variables at that point, you can do that anytime you want. So, if you start your script with session_start(), you can still allow your variables to be obtained towards the bottom, then declare your session values after that! <?php session_start(); all your other liens of code variable generation $_SESSION['size'] = '12'; ?> PHP:
you could always use $_COOKIE (though in php you typically set with setcookie and then can later access using the $_COOKIE method), thats something accessible by both javascript and PHP (long as not set to htmlonly) But yes in order for sessions to work you need to make sure to start them at the beginning of the page using them (and they act like header() so you can't have any output prior to starting a session)
So just to confirm, I can start the session and declare the variables at the top of the page before the html starts, then I can change the values towards the end of the html page using the <?php variable generation $_SESSION['size'] = '12'; ?> Code (markup): kblessinggr thanks for the $_COOKIE option but I prefer to stick with sessions at this time.
Any particular reason why (regarding cookie) ? Far as session, you have to understand how PHP works in a manner of speaking. The PHP code from top to bottom is executed prior to sending anything to the browser. But the flow of output still goes in the usual order. For example $var = 12; echo "<p>".$var."</p>"; $var = 9; echo "<i>".$var."</i>"; PHP: your output would be then <p>12</p> <i>9</i> Code (markup): though I'm not sure why it matters if the session value itself can be set down at the end or top, when javascript cannot access a session variable directly as it remains on the server.
To use what you've put in between teh code tags, you must include "session_start();" as the very first line, or else you will get in valid header errors.
Hey - Just to add my 2c to this - You MUST start the session at the top of the page (before the headers are sent), you can declare session variables at any point in the page, BUT you won't be able to get anything that javascript generate. PHP is server side, javascript client side. For instance: <?php session_start(); ?> <html> <head></head> <body> Body stuff here <script> var foo = 'bar'; var mysum = 2 + 2; </script> </body> </html> <?php $_SESSION['foo'] = 'bar'; ?> The session variable 'foo' will be set, but the result of of javascript sum we won't be able to get in PHP - the javascript will be processed after all the PHP has already been done. Make sense?
Thanks, iamben, your explanation made perfect sense kblessinggr, I chose sessions because I wanted to learn it and I'll need the skills for something I'll be working on soon, so I just decided to go ahead and learn it.
Well, if you just use sessions for font size (just gessing based on $_SESSION['size']) that's not a very good use for it. Always use the best tool for the job. Since sessions are server side, it also means it's more resource intensive (reading, writing, etc) versus using $_COOKIEs. Ofcourse, you may ignore this, just adding to the convo