Alex Porter
asked on
Sessions and subdomains
I'm having trouble with sessions. I added the first line to the code below to make the sessions work regardless of subdomain at my site. When I run this code, it prints YES100001000Alex on the screen as it should. However, if I try to access those session variables on a different page, I get nothing. If I come back to the page with the code below, it again shows the correct response, but I tihnk it's just because the variables are defined in the page. The file only sets a cookie the first time I access it.
Any advice?
Thanks!
<?
ini_set("session.cookie_do main","ejo btools.com ");
session_start();
session_register('AUTHORIZ ED');
session_register('Employee ID');
session_register('Business ID');
session_register('FName');
$_SESSION[AUTHORIZED] = "YES";
$_SESSION[EmployeeID] = "10000";
$_SESSION[BusinessID] = "1000";
$_SESSION[FName] = "Alex";
echo "$_SESSION[AUTHORIZED]";
echo "$_SESSION[EmployeeID]";
echo "$_SESSION[BusinessID]";
echo "$_SESSION[FName]";
?>
Any advice?
Thanks!
<?
ini_set("session.cookie_do
session_start();
session_register('AUTHORIZ
session_register('Employee
session_register('Business
session_register('FName');
$_SESSION[AUTHORIZED] = "YES";
$_SESSION[EmployeeID] = "10000";
$_SESSION[BusinessID] = "1000";
$_SESSION[FName] = "Alex";
echo "$_SESSION[AUTHORIZED]";
echo "$_SESSION[EmployeeID]";
echo "$_SESSION[BusinessID]";
echo "$_SESSION[FName]";
?>
ASKER
Here's the code on my index file for a given subdomain at my site. I tried the script that you suggested, cracky, but when I access this other page, $_SESSION[AUTHORIZED] == "YES" is a false value.
<?
$section = "account";
require("/files/authentica tion.php") ;
require("/files/tabs.php") ;
if ($_POST[LoginButtonClicked ]) {
if ($_POST[Username] && $_POST[Password]) {
doAuthorize($_POST[Usernam e], $_POST[Password]);
} else {
}
}
if ($_SESSION[AUTHORIZED] == "YES") {
$LOGO = "http://account.ejobtools.com/$_SESSION[BusinessID]/logo.gif";
$TITLE = "$section - $_SESSION[BusinessID] - ejobtools.com";
} else {
$LOGO = "http://www.ejobtools.com/images/logo.gif";
$TITLE = "$section - ejobtools.com";
$BUSINESS_NAME = "ejobtools.com";
}
?>
One thing that's weird: I have cookie notification turned on so that I can see when it tries to post a cookie, but it only posts a cookie the first time I access the code from the first part of my question. Even if I change the values, reupload the file, and refresh the browser, it doesn't post a new cookie. It does however display the updated values on that page. Is this because session values are stored on the server, and the only thing that needs to be stored on the computer is the session ID?
If I update the first file to be the following after running the original and without ending the session, it displays nothing.
<?
session_start();
echo "$_SESSION[AUTHORIZED]";
echo "$_SESSION[EmployeeID]";
echo "$_SESSION[BusinessID]";
echo "$_SESSION[FName]";
?>
<?
$section = "account";
require("/files/authentica
require("/files/tabs.php")
if ($_POST[LoginButtonClicked
if ($_POST[Username] && $_POST[Password]) {
doAuthorize($_POST[Usernam
} else {
}
}
if ($_SESSION[AUTHORIZED] == "YES") {
$LOGO = "http://account.ejobtools.com/$_SESSION[BusinessID]/logo.gif";
$TITLE = "$section - $_SESSION[BusinessID] - ejobtools.com";
} else {
$LOGO = "http://www.ejobtools.com/images/logo.gif";
$TITLE = "$section - ejobtools.com";
$BUSINESS_NAME = "ejobtools.com";
}
?>
One thing that's weird: I have cookie notification turned on so that I can see when it tries to post a cookie, but it only posts a cookie the first time I access the code from the first part of my question. Even if I change the values, reupload the file, and refresh the browser, it doesn't post a new cookie. It does however display the updated values on that page. Is this because session values are stored on the server, and the only thing that needs to be stored on the computer is the session ID?
If I update the first file to be the following after running the original and without ending the session, it displays nothing.
<?
session_start();
echo "$_SESSION[AUTHORIZED]";
echo "$_SESSION[EmployeeID]";
echo "$_SESSION[BusinessID]";
echo "$_SESSION[FName]";
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your continued help - I've raised the points on this question because I'm still having trouble.
I made a simplified file in order to test things out. Here's the file:
<?
session_start();
$_SESSION['AUTHORIZED'] = "YES";
?>
<a href=test2.php>click</a>
The next page, test2.php looks like this:
<?
session_start();
echo "$_SESSION[AUTHORIZED]";
?>
It displays nothing :( I think I've followed all of your advice....any other advice?
Thanks,
Alex
I made a simplified file in order to test things out. Here's the file:
<?
session_start();
$_SESSION['AUTHORIZED'] = "YES";
?>
<a href=test2.php>click</a>
The next page, test2.php looks like this:
<?
session_start();
echo "$_SESSION[AUTHORIZED]";
?>
It displays nothing :( I think I've followed all of your advice....any other advice?
Thanks,
Alex
Are you going to a subdomain when you call the variable?
If so, you might need to set ini_set("session.cookie_do main",".ej obtools.co m"); as you have done before.
After you visit the first page, does it set a cookie? Check in your cookies folder for a cookie from your specified domain.
Also, see if calling your session variable works when outside quotes:
echo $_SESSION['AUTHORIZED'];
If so, you might need to set ini_set("session.cookie_do
After you visit the first page, does it set a cookie? Check in your cookies folder for a cookie from your specified domain.
Also, see if calling your session variable works when outside quotes:
echo $_SESSION['AUTHORIZED'];
ASKER
I figured it out and I'm so excited! Here's what I needed:
ini_set("session.cookie_do main",".ej obtools.co m");
ini_set("session.save_path ","/sessio n");
session_start();
Thanks for your help. Your information did help me get this working.
Alex
ini_set("session.cookie_do
ini_set("session.save_path
session_start();
Thanks for your help. Your information did help me get this working.
Alex
Great! Glad to hear it :)
Second, by registering the session variables again on each page load, you are essentially resetting them.
Third, as far as I know, you need to prefix your domain with a . dot to include subdomains.
Try this:
<?
ini_set("session.cookie_do
session_start();
$_SESSION['AUTHORIZED'] = "YES";
$_SESSION['EmployeeID'] = "10000";
$_SESSION['BusinessID'] = "1000";
$_SESSION['FName'] = "Alex";
echo "$_SESSION[AUTHORIZED]";
echo "$_SESSION[EmployeeID]";
echo "$_SESSION[BusinessID]";
echo "$_SESSION[FName]";
?>