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.
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 ?
Last edited by dalecosp; 01-24-2013 at 12: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
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.
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.
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.
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.
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.
Bookmarks