Page 1 of 1
Loss of Session using "require"
Posted: Sun May 20, 2007 6:52 am
by josh_
Hello,
Upon detecting an error, my code redirects the user to another page which logs the data, sends an email to the administrator, and alerts the user of the error.
The user's data is stored using $_SESSION variables. In order to log the error, I pass the error number in the URL to the required file (in a different folder). However, in order for "require" to work properly when passing variables, the full URL to the required page must be entered. So here's what it looks like:
page1.php
Code: Select all
session_start();
$errorPath = "http://".$_SERVER['SERVER_NAME'].dirname(dirname($_SERVER['PHP_SELF']))."/Folder/errorLogScript.php";
echo $_SESSION['userID'];
...
if ($foo !='bar'){
require $errorPath.'?errorRef=0023';
die();
}
errorLogScript.php
session_start();
Code: Select all
if (isset($_SESSION['userID'])){
echo "session valid";
} else {
echo "session data lost";
}
---------------------------------------
Page1.php will display the userID but errorLogScript.php will display
each time there is an error detected. How can I keep my session open when navigating to this new page?
Thanks,
Josh
Posted: Sun May 20, 2007 8:24 am
by Arawn
Since the errorLogScript.php file appears to be on the same server, require() it directly, without the http. You'll need to get rid of the extra session_start() in errorLogScript.php too.
It would be a good idea to use an absolute path for errorLogScript.php or any include.
Posted: Sun May 20, 2007 9:28 am
by josh_
It would be a good idea to use an absolute path for errorLogScript.php or any include
I was hoping that was what I was accomplishing by using $errorPath. Is it not?
I tried removing "http://" as you suggested and got the error:
Code: Select all
[function.main]: failed to open stream: No such file or directory
copying and pasting the url into the browser brought me to the correct page (session closed, of course).
Also, as you suggested, I removed the extra session_start(); but no change.
Here's what I've tried that does not work:
Code: Select all
require $errorPath.'?errorRef=0001';
require 'http://www.myurl.com/folder/errorLogScript.php?errorRef=0001';
require 'www.myurl.com/folder/errorLogScript.php?errorRef=0001';//as you suggested
require '../folder/errorLogScript.php?errorRef=0001';
require 'http://www.myurl.com/folder/errorLogScript.php'; //I thought this should work since the relative url to the same file works
require 'www.myurl.com/folder/errorLogScript.php'; //as you suggested
Here's what does work...as far as loading errorLogScript.php:
Code: Select all
$_SESSION['errorRef']='0001';
require '../folder/errorLogScript.php';
However, I would have to pass the errorRef variable outside of the URL (which at this point would be quite painful to implement in all of the affected pages.
Any suggestions to avoid changing the notation on every affected page?
Thanks,
Josh
Posted: Sun May 20, 2007 1:37 pm
by Arawn
josh_ wrote:I was hoping that was what I was accomplishing by using $errorPath. Is it not?
I tried removing "http://" as you suggested and got the error:
Code: Select all
[function.main]: failed to open stream: No such file or directory
Path and URL are not the same thing. Path is the location according to the File System. URL is the location according to the HTTP Server. [Basically anyway.] Actually from the information I have, I can't figure out the absolute Path.
This page explains the difference between Path and URL somewhat:
http://www.ibdhost.com/help/path/
josh_ wrote:
Here's what does work...as far as loading errorLogScript.php:
Code: Select all
$_SESSION['errorRef']='0001';
require '../folder/errorLogScript.php';
However, I would have to pass the errorRef variable outside of the URL (which at this point would be quite painful to implement in all of the affected pages.
Any suggestions to avoid changing the notation on every affected page?
That's the solution I was referring to, though using a relative path. The only other solution I can think of is passing the needed session data via URL parameters like errorRef, not a good solution either.

Posted: Mon May 21, 2007 3:43 am
by josh_
Thanks for your help. I ended up biting the bullet and changed every iteration of the code to:
Code: Select all
$errorRef = 'xxxx';
require '../folder/errorLogScript.php';
No need to pass the error in a session variable when using include or require. Hindsight is 20/20. I should have just done that at the beginning.
Thanks!
Josh