SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing global variables

    HI how to access global variable across files in php.

    i am currently working on an application, where i need to take a value from one file to another.. It cannot be obtained through get or post method since it takes some two or three files to render the next view from the form window.

    Eg:From the login page it executes four pages to render the next view page


    Here, i need to take a value from the second to third page. How can i do it.

    I gave in the second file as global $username;

    in third file how to access the global variable declared in file2 in file3..

    thanks in advance.

    Regards,
    muthu

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use sessions to carry the values across multiple pages.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put this at the top of the page that first receives the data you want to save:
    PHP Code:
    <?php
    session_start
    ();
    ?>
    Warning: there cannot be any spaces or blank lines before that opening php tag. In other words, it has to be at the very top of the file.

    Then anywhere on the page, insert the data you want to save into the $_SESSION[] array:
    PHP Code:
    $_SESSION['someVarName'] = 'some data'
    Whenever you are on another page and you want to add more data to the $_SESSION[] array or retrieve information from the array, at the top of the page put:
    PHP Code:
    <?php 
    session_start
    ();
    ?>
    and then you can freely access the array, e.g.
    PHP Code:
    echo $_SESSION['someVarName']; 

  4. #4
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys.. for you support..


Bookmarks

Posting Permissions

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