[RESOLVED] Session losing variable
Results 1 to 8 of 8

Thread: [RESOLVED] Session losing variable

  1. #1
    Senior Member
    Join Date
    Jan 2009
    Location
    CA
    Posts
    257

    resolved [RESOLVED] Session losing variable

    Why is that when I sometimes have an INCLUDE with the complete root
    PHP Code:
    include "http://myWeb/myTournament/myHomePage.php"
    that I lose the $_SESSION? All my php pages have session_start() and if I put in the virtual path, it works fine?

    I could use the virtual path to solve the problems, but how do I handle PHP modules that are in sub-sub directories ?

    Thanks,
    robkir

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,722
    Don't include the files via HTTP. I would imagine that there's session activity on myHomePage.php that's overwriting the session data in the caller. See include() and allow_url_fopen() for more info.

    Quote Originally Posted by robkir
    I could use the virtual path to solve the problems, but how do I handle PHP modules that are in sub-sub directories ?
    What about the *real* path? There's even a function for that.
    Last edited by dalecosp; 01-24-2013 at 01:57 PM. Reason: Blargh ... I meant HTTP, not PHP. "Don't include via PHP" ... duh....
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Quote Originally Posted by dalecosp View Post
    I would imagine that there's session activity on myHomePage.php that's overwriting the session data in the caller.
    It couldn't possibly overwrite the session data, because 'myHomePage.php' won't even know about the session.

    After all, the "end user" in the include()'s perspective is the web server itself, since you're making PHP open a new HTTP connection to the webserver it's hosted on. I really wish I could find my analogy I used to describe the ridiculousness of include/require()'ing your own files via HTTP... something about sitting on your couch, deciding you're thirsty, going out your front door, walking around your house, knocking on your front door, etc. etc.

  4. #4
    Senior Member
    Join Date
    Jan 2009
    Location
    CA
    Posts
    257

    virtual versus absolute

    Quote Originally Posted by bradgrafelman View Post
    It couldn't possibly overwrite the session data, because 'myHomePage.php' won't even know about the session.

    After all, the "end user" in the include()'s perspective is the web server itself, since you're making PHP open a new HTTP connection to the webserver it's hosted on. I really wish I could find my analogy I used to describe the ridiculousness of include/require()'ing your own files via HTTP... something about sitting on your couch, deciding you're thirsty, going out your front door, walking around your house, knocking on your front door, etc. etc.
    Your explanation makes sense (and it was a good analogy). But I don't see an easy way around the problem of a menu which provides links to php pages in sub directories and sub/sub directories.

    To illustrate--here's a scenario

    nav_public.php is included in all the user pages.

    It is in the directory homepage/nav_public.php
    nav_public.php is included in home.php (same directory)
    nav_public.php is included in games/games_list.php
    nav_public.php is included in equipment/fields/fields_list.php

    If I am in the same directory as nav_public.php the path back to home would be (obviously) home.php.

    If I am in games/games_list.php the path to home.php would be ../homepage/home.php

    If I am in equipment/fields/fields_list.php the path to home.php would be ../../homepage/home.php

    In the past I've just hardwired it with the absolute path. But...as I discovered...I am having session issues.

    Any advice would be appreciated

    robkir

  5. #5
    Senior Member
    Join Date
    Mar 2009
    Posts
    812
    Are you sure you're not accessing a page that doesn't have session_start() somewhere? I don't remember off hand if visiting a page without it, and then going back to one that does will modify the session (can someone possibly confirm/correct this?). Just make sure on every page where you want to use the session you have session_start(), usually at the top if possible.
    Declare variables, not war.

  6. #6
    Senior Member
    Join Date
    Jan 2009
    Location
    CA
    Posts
    257
    Quote Originally Posted by Bonesnap View Post
    Are you sure you're not accessing a page that doesn't have session_start() somewhere? I don't remember off hand if visiting a page without it, and then going back to one that does will modify the session (can someone possibly confirm/correct this?). Just make sure on every page where you want to use the session you have session_start(), usually at the top if possible.
    Good thought, but each and every page has the session_start() at the very top of the page. Without that, SESSIONS won't work.

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,905
    Quote Originally Posted by robkir
    But I don't see an easy way around the problem of a menu which provides links to php pages in sub directories and sub/sub directories.
    Use absolute filepaths instead of http URLs. The $_SERVER contains values that can be used to build such paths.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  8. #8
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,901
    First off, consider using require or require_once rather than include. Unlike include, require will cause a fatal error if the file you are looking for is not found.

    As for the path issue, I think I understand your confusion. If I go visit a script on my website at http://example.com/index.php then inside that file, there is the idea of the current working directory (CWD) which determines the include_path. The CWD and include_path concepts are fundamental to every operating system. The basic idea is that in a PHP script, the web server launches your script with a particular directory in mind as your CWD. You can find out what this is with the function getcwd. You can set it to something else with chdir. If you have ever used the linux command line, these functions should remind you of pwd and cd, respectively. In the example above, the CWD is probably something like /var/www/html. If I visit a script at a different url that has some path bits to it, such as http://example.com/my/file/path/index.php, then the CWD will be different -- probably something like /var/www/html/my/file/path

    The include_path in PHP is a list of directories that PHP will search when you include, require, or otherwise reference a file. You can find out this value with the function get_include_path:
    PHP Code:
    echo get_include_path(); 
    What results is a list of paths separated by colons in a linux machines and semicolons on a windows machine (note that PHP defines the global constant DIRECTORY_SEPARATOR as either colon or semicolon accordingly).

    When you include or require a file, PHP will search those paths in order until it finds the file you specified -- NOTE that it does so by appending the path and file you have specified to each path in turn. When it finds one, it goes with it.

    How does one deal with this in a PHP project that spans many folders? I can think of 3 days:
    1) Have every single file in your project require some file named constants.php or something that defines the root of your project like so:
    PHP Code:
    define("PROJECT_ROOT_PATH""/var/www/html"); 
    or perhaps like this:
    PHP Code:
    define("PROJECT_ROOT_PATH"realpath(dirname(__FILE__))); // sets it to whatever directory contains the PHP file in which this code exists 
    drawback: If you wanted to include this file from /var/www/games/games.php, then games.php would need to know where to find constants.php:
    PHP Code:
    // in games.php
    require_once "../constants.php"// we use a relative reference
    // then you can do this:
    require PROJECT_ROOT_PATH "/some/other/file.php"
    As long as the relative location of games.php and constants.php is preserved, this will always work. Defining this constant is usually a good idea.

    2) Have every access to your site go through a single file, usually index.php
    In this strategy, the CWD is always the same, because every single http access to your site always goes through the same file first. In practice this makes includes easy because every file can be included relative to that one file. The tradeoff is that you need something like mod_rewrite to get SEO-friendly urls instead of http://example.com/index.php?some_in...cify_an_action . Also, your index.php file can be pretty ugly unless you concoct a safe scheme to route code execution to the right file in the right subdirectory. CodeIgniter has a pretty clever one that I like lots.

    3) Redefine the include path in your PHP.ini file.
    This is only typically feasible if your server hosts only one website or if you can somehow define a php.ini file specific to your website. The idea is that you define the include_path such that it knows where your project path is.

    I hope this helps.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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