Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Global Variables and Session Variables

    Hello,

    Can someone explain to me what the difference between Session and Global Variables is? I can't seem to find an online resource that effectively summarizes their differences.

    Also take a look at the following script:
    http://www.evolt.org/node/60384

    I've done OOP before (in C++ and Java) - my main question is how is it allowing a user to persist? It seems like every time a user goes to a new page a new database, session, etc objects have to be created...what's the point?
    -sushi
    -visit http://napkinz.com/index.php - funny comic updated weekly

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,325
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    There is no difference.

    Sessions are global.

    A global variable means that a variable is available to any part of your script - main code, functions, classes etc. If it isn't global it is only available to the local scropt - IE say your inside a function and you want to access $Var which is part of the main code outside the function. You won't be able to unless you declare it a global like this:

    global $Var;

    OR you pass it in the function parameters.

    $_SESSION is the same as $_GET, $_POST, $_SERVER, $_REQUEST - they are all available to any part of your code because they are global.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by tangoforce View Post
    There is no difference.
    That is not true.

    Session variables persist between pages during a session. Global variables do not persist between pages during a session.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,325
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Yes there is that

    What I meant was that there is no difference in session variables not being global.. if that makes sense.

    I see I could have worded that better
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    New Coder
    Join Date
    Apr 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, so basically if a variable is defined outside a given function, you need to define it inside the function?

    Did anyone have a look at the link? Is that just recreating objects at every load? Is that what you're supposed to do? Are objects not supposed to persist?
    -sushi
    -visit http://napkinz.com/index.php - funny comic updated weekly

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by sushi4664 View Post
    Ah, so basically if a variable is defined outside a given function, you need to define it inside the function?
    This is typical of most high level languages. Its referred to as a scope; functions, classes, namespaces etc declare their variables local to themselves, and not local to the entire program. $a declared within main() and $a declared within myfunc() have no relationship to each other by default. I'd avoid using 'global' keyword (for the same reasons we avoid it in C) when you can, and modify the signature to accept an argument for the variable instead.

    Did anyone have a look at the link? Is that just recreating objects at every load? Is that what you're supposed to do? Are objects not supposed to persist?
    I didn't follow the link no (I don't from work).
    Technically speaking, there is NO persisting data when over static protocols like http. PHP is no exception, sessions themselves do not actually persist, rather the information is stored within a file (or database or whatever you want really), and the client presents a session id passed either through GET, POST or COOKIE to the application and tells PHP 'I have this id, do you know about me?'. This is why you can find tutorials to prevent session hijacking.
    The above is the typical approach to using sessions. Beyond this, I'd expect that APC *could* be used to actually persist data in the server memory. I would NOT recommend using it though, APC is quite particular to use, and has a huge huge flaw: its server wide and not site wide.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •