SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Different session value of same variable in different folders

    Hi All,

    I have two folders in my www: test1/index.php and test2/index.php
    code test1/index.php
    PHP Code:
    <?php
    @session_start();
    if(isset(
    $_SESSION['name']) && $_SESSION['name'] != ''){
        echo 
    "Name=" $_SESSION['name'];
    } else {
        
    $_SESSION['name'] = 'Foo';
    }
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function fun(){
        window.open("../test2/index.php","NewWindow", "width=600");
    }
    </script>
    </head>
    <body>
    <input type="button" value="Click" onclick="fun();" />
    </body>
    </html>
    Code in test2/index.php
    PHP Code:
    <?php
    @session_start();
    if(isset(
    $_SESSION['name']) && $_SESSION['name'] != ''){
        echo 
    "Name=" $_SESSION['name'];
    } else {
        
    $_SESSION['name'] = 'Bar';
    }
    ?>
    Through browser when accessing test1/index.php and clicking on 'Click' button,
    o/p=> Name="Foo"

    Is it possible
    1) If I access test1, o/p=> Foo
    2) If I access test2 (as clicking on 'Click' button), o/p=> Bar

    Your help will be highly appreciated.

    Thanks in advance.
    Barbara

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,057
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    So you either want a new session started from the pop-up window (controlled by the browser) or you want to namespace your sessions so you can pragmatically separate your session data between the two windows.

    Is that correct?

    The only solution I know of is to prefix your session variable names with the directory you are in.
    PHP Code:
    <?php
    $directory 
    basename(dirname($_SERVER['PHP_SELF']));
    @
    session_start();
    if(isset(
    $_SESSION[$directory.'name']) && $_SESSION[$directory.'name'] != ''){
        echo 
    "Name=" $_SESSION[$directory.'name'];
    } else {
        
    $_SESSION[$directory.'name'] = 'Bar';
    }
    ?>
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks cpradio.

    But in my folder, there are so many session variable (which I may not be knowing) in so many files.
    Barbara

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,057
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You can also try setting the session_id('place value here'), but I'm not sure how that works across threads (if that needs to be unique to each thread, etc)
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


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
  •