Results 1 to 7 of 7

Thread: Set Global Variable... SESSIONS...

  1. #1

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Set Global Variable... SESSIONS...

    I want to set a variable that can be accessed anywhere, how can I set it then access it? dont want a cookie though
    Last edited by damasterjo; May 8th, 2006 at 04:30 PM.
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  2. #2
    Addicted Member techwizz's Avatar
    Join Date
    Apr 2005
    Location
    U.S.A.
    Posts
    246

    Re: Set Global Variable, not comming from a post or form

    You could use sessions?

    PHP Code:
    //Start the session
    session_start();

    session_register('var1');
    session_register('var2'); 
    Then every page you go to from there needs to have...

    PHP Code:
    /Start the session
    session_start
    (); 
    at the top.

    Another option would be declaring variables in a file named something like main.php the requiring that file in each page.

    Thats what PHP-Nuke does.

    you can read here on sessions...

    http://us3.php.net/session

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Set Global Variable, not comming from a post or form

    Nuke is an atrocious example of PHP coding. I agree sessions are the correct way to implement this. However, don't use session_register, which is deprecated. Use instead the $_SESSION superglobal array.

    PHP Code:
    session_start();

    // ...

    // set a session var
    $_SESSION['var1'] = $var1;

    // get a session var
    $var1 $_SESSION['var1'];

    // unset a session var
    unset($_SESSION['var1']); 
    If you need the user to log out, you should at this point call session_destroy(), which unsets all session variables and removes the session ID.

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Set Global Variable, not comming from a post or form

    And one thing to add before you access the session variable array in another page you should start the session first.

    session_start();
    $var1 = $_SESSION['var1'];

  5. #5

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: Set Global Variable, not comming from a post or form

    Okay I got sessions to work, but when I leave the site and come back it does not remember

    This a code on one page
    $_SESSION['UserLoggedIn'] = mysql_result($result,$DBspot,"Name");


    This is on the main page
    Welcome to my website <strong><? echo $_SESSION['UserLoggedIn']; ?></strong>!

    When I leave and come back this function returns false
    if($_SESSION['UserLoggedIn'])

    ???
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  6. #6
    New Member
    Join Date
    Apr 2006
    Posts
    15

    Re: Set Global Variable, not comming from a post or form

    Has each user who logs in got their own ID?

    If so, why not register the member ID to the session variable?

    Then you can say if that session has a value then print the text, else print something else.

    This should work even if you close your browser session aswell. However depending on the setup of you php.ini your session may expire after a short amount of time

    PHP Code:
    if ('user has logged in code here'){
        
    $_SESSION['UserLoggedIn'] = $memberID;
    }

    if (isset(
    $_SESSION['UserLoggedIn'])){
        print 
    "Welcome to my website <strong>".$_SESSION['memberName']."</strong>";
    }else{
        
    # Login script here


  7. #7

    Thread Starter
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: Set Global Variable, not comming from a post or form

    ok well i think maybe the php.ini file may be formated a certain way, how would I make it never end unless they loged out?
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

Posting Permissions

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



Click Here to Expand Forum to Full Width