Simple Question about PHP Sessions

Discussion in 'PHP' started by oseymour, Aug 26, 2009.

  1. #1
    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):
     
    oseymour, Aug 26, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    premiumscripts, Aug 26, 2009 IP
  3. oseymour

    oseymour Well-Known Member

    Messages:
    3,960
    Likes Received:
    92
    Best Answers:
    0
    Trophy Points:
    135
    #3
    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?
     
    oseymour, Aug 26, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, if you add:

    ob_start(); 
    PHP:
    at the top of your script, it will work just fine.
     
    premiumscripts, Aug 26, 2009 IP
  5. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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:
     
    SHOwnsYou, Aug 26, 2009 IP
    oseymour likes this.
  6. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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)
     
    kblessinggr, Aug 26, 2009 IP
  7. oseymour

    oseymour Well-Known Member

    Messages:
    3,960
    Likes Received:
    92
    Best Answers:
    0
    Trophy Points:
    135
    #7
    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.
     
    oseymour, Aug 26, 2009 IP
  8. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    kblessinggr, Aug 26, 2009 IP
    oseymour likes this.
  9. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    SHOwnsYou, Aug 26, 2009 IP
  10. iamben

    iamben Active Member

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #10
    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?
     
    iamben, Aug 26, 2009 IP
    oseymour likes this.
  11. oseymour

    oseymour Well-Known Member

    Messages:
    3,960
    Likes Received:
    92
    Best Answers:
    0
    Trophy Points:
    135
    #11
    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.


     
    oseymour, Aug 26, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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 :)
     
    premiumscripts, Aug 26, 2009 IP