Log in

View Full Version : Changing System Paths Within a Page



Strangeplant
07-05-2006, 04:46 PM
Hi,

I have several directories/pages where I want to execute the same php routines. The first is on a page that includes code, and may branch to other files, also with included code, where all the included code is in the same directory (or subdirectories); code example:
<?php
session_start();
$_SESSION['where'] = "http://earth.engr.ccny.cuny.edu/noaa/wc/Lidar/chainedmenu2.php";
include_once("inc/auth.inc.php");
/*echo "where = "; echo $_SESSION['where'];*/
$user = _check_auth($_COOKIE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

Now, in a different directory I have a similar thing:
<?php
session_start();
$_SESSION['where'] = "http://earth.engr.ccny.cuny.edu/noaa/wc/DailyData/chainedmenu02.php";
include_once("inc/auth.inc.php");
/*echo "where = "; echo $_SESSION['where'];*/
$user = _check_auth($_COOKIE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
The catch is that I want the second page to have the included code from the first, not second directory. If all is ok with the password authentication, it will take me back to the second page by redirect. The second page needs to have the path changed to be equal to what it was when running the first page before the include is done (remember that the include may also have more includes, so the path variable needs to be changed.) I'm using php 4.4.2 with Apache2.

What system variable do I need to change and how do I change it?

Twey
07-05-2006, 07:07 PM
You can use the chdir (http://www.php.net/chdir)() function to change the working directory of a page.

Strangeplant
07-06-2006, 02:26 PM
I see now that it works, but am getting this: Warning: Cannot modify header information - headers already sent by (output started at /webmail/apache/htdocs/noaa/wc/DailyData/chainedmenu02.php:8) in /webmail/apache/htdocs/noaa/wc/Lidar/inc/auth.inc.php on line 32. How do I get around this? My code, so far is:
<?php
ob_start();
session_start();
$_SESSION['where'] = "http://earth.engr.ccny.cuny.edu/noaa/wc/DailyData/chainedmenu02.php";
$dir = getcwd();
chdir('../');
chdir('Lidar');
ob_end_flush();
include_once("inc/auth.inc.php");
$user = _check_auth($_COOKIE);
chdir($dir);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
And the line 32 that it is complaining about in /inc/auth.php is the redirect,
header("Location: login.php");
It differs only in changing the path from this, which does not generate the warning:
<?php
session_start();
$_SESSION['where'] = "http://earth.engr.ccny.cuny.edu/noaa/wc/Lidar/chainedmenu2.php";
include_once("inc/auth.inc.php");
/*echo "where = "; echo $_SESSION['where'];*/
$user = _check_auth($_COOKIE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
BTW, the ob_start() was an attempt to get rid of this warning by postponing the output.....(didn't work)

Twey
07-06-2006, 02:42 PM
As you've guessed, it's because you're trying to call header() after some output has already been sent to the browser.

BLiZZaRD
07-06-2006, 03:49 PM
Dont forget that header locations SHOULD be complete URLS ( http://somesite.com/folder/page.php ) Not just /page.php ;)

Also note that I have had this exact same problem, even though the ONLY thing in the php was the redirect, and there was NO error in the script, or white space or anything.

Eventually I contacted server Admin and there was a problem on their end, which fixed it right up.

Strangeplant
07-06-2006, 04:48 PM
Twey, could you suggest something of a fix, please tell me how? The problem is caused solely by the chdir() and no placement of ob_start() and ob_end_flush() will help out. It seems that you just can't ever do a chdir().:eek: Likewise, full path does not solve the issue.:confused: What happens, is that the header("Location: login.php"); does not execute so the rest of the page displays, bypassing the redirect! (ugg!, then a long sigh....and I'm learning to cuss, which is not good in an academic setting.)

Twey
07-06-2006, 05:16 PM
That's peculiar -- if you remove the chdir()s, it works?

Strangeplant
07-06-2006, 07:47 PM
Ok, things seemed strange. So, I installed all of the scripts in both directories so I wouldn't get the error until I could work it out. And, of course, the problem surfaced. When I login in directory DailyData, I can access the data correctly, but when I change to the other directory, Lidar, the page won't display saying that redirect is messed up. If I then go and logout, I can then login in Lidar, but DailyData is messed up. I can finagel it so that I login in both places to access the data in both, but not through the correct links. Clearing cookies and sessions frees up both so that I can login on one, but not the other - the second should be accessed immediately because of the cookie and session, but is not.

So, my take on it is that the session ID is not preserved across the pages accessed through the primary link. I remember something about setting SID somewhere, though, and a quick search turns up nothing yet. Is there a simple answer, I suppose that I just have not discovered it yet.

Twey
07-06-2006, 08:10 PM
Is there a simple answer, I suppose that I just have not discovered it yet.My first guess, after seeing the header() error, would be that there is some data output before you call session_start().

Strangeplant
07-07-2006, 01:37 PM
Data output before the session_start(); is not the problem (I think).

This is what I now have, and the strangeness of it all. I have copied the program/page files into two directories and everything is identical except for the one line that governs the redirect back to the starting page. If I'm in Directory `DailyData`, the program goes to the login page on a fresh day (cookie(s) expired). I login and the data is accessed correctly. I then change to the `Lidar` directory, where I expect to be still logged in, and the page comes up saying redirect error. If I go back to the `DailyData` directory, I'm still logged in. Back to the `Lidar` directory I still get the same redirect error - page cannot be displayed. I logout from the `Lidar` directory, and it immediately takes me to the login page. Good so far. I login, and the `Lidar` data is accessible. Fine. I go back to the `DailyData` page and immediately access the data - I'm logged in. The converse is true if I wipe out session data and cookies, meaning whatever directory I start in, the other gives me a redirect error until I logout and back in, making both accessible.

The code on the top of each page is this (with the exception that the one redirect location is appropriately, I think, changed):
<?php
session_start();
$_SESSION['where'] = "http://earth.engr.ccny.cuny.edu/noaa/wc/Lidar/chainedmenu2.php";
include_once("inc/auth.inc.php");
$user = _check_auth($_COOKIE);
?>The code for the login page is this:
<?php
session_start();
include_once("inc/auth.inc.php");
_already_logged($_COOKIE);

if(isset($_POST['submit']))
{
$user_data = _check_database(fm($_POST['user']),fm($_POST['pass']));
if($user_data == 0) login_page();
else _set_cookie($user_data,fm($_POST['rem']),session_id(),fm($_POST['user']));
} else
login_page();
?>I think the problem has something to do with the session not being acceptable to the _check_auth($_COOKIE) function, probably that the session has changed. So.....now, any ideas? I'm confused on how to keep the sessions the same. BTW, I can't find my cookie to see if the SID is changing. Where is it? (not is C:\Documents and Settings\`user`\Cookies) Any suggestions, pleeeeeze:o