Link to home
Start Free TrialLog in
Avatar of Alex Porter
Alex PorterFlag for United States of America

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_domain","ejobtools.com");
session_start();

session_register('AUTHORIZED');
session_register('EmployeeID');
session_register('BusinessID');
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]";

?>
Avatar of cracky
cracky

Firstly, you don't need to use session_register() if you are using the superglobals. Also, when used outside quotes, you need quotes inside your $_SESSION array definitions as I have corrected below.
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_domain",".ejobtools.com");
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]";

?>
Avatar of Alex Porter

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/authentication.php");
require("/files/tabs.php");

if ($_POST[LoginButtonClicked]) {

      if ($_POST[Username] && $_POST[Password]) {
            doAuthorize($_POST[Username], $_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]";
?>
ASKER CERTIFIED SOLUTION
Avatar of cracky
cracky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Are you going to a subdomain when you call the variable?

If so, you might need to set ini_set("session.cookie_domain",".ejobtools.com"); 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'];
I figured it out and I'm so excited!  Here's what I needed:

ini_set("session.cookie_domain",".ejobtools.com");
ini_set("session.save_path","/session");
session_start();

Thanks for your help. Your information did help me get this working.

Alex
Great! Glad to hear it :)