SESSIONS [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

SESSIONS [solved]

Post by anthony88guy »

Okay... so I have a members area located at a sub-domain, but I have a login on the domain (home page). So in order for me to get the login to work I must send the form to a login script on that sub-domain. What would be the best route to link the domain and the sub-domain? Some type of cookie, linked to the database? When I go to the homepage and you’re logged in through the sub-domain, you should be redirect to the sub-domain, but you’re not since the session doesn’t exist for that domain. I hope thats understandable.

Many thanks.
Last edited by anthony88guy on Tue Feb 21, 2006 1:06 pm, edited 3 times in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could pass the sessID as a form var and then reset the session on the other 'domain'
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can set the cookie to match against the domain and web root, which will get the browser to send it when accessing any part of your domain, be they subdomain or not.

Code: Select all

domain: .example.com
path: /
session_set_cookie_params()
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Do I have to set the cookie params once, or on each page? I think I read each page.

Thanks for your help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

each page that uses sessions.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

So im trying to use the following:

Code: Select all

session_set_cookie_params(900, '/', '.theeliteforces.com');
But it still doesn’t work... Its kind of late (1:20am) so I'll play around with it tomorrow.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what other code is this being used with? If you're using a header() redirection the cookie may not set as the browser may choose to ignore it, or you server didn't send the cookie. session_write_close() can help fix the latter.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Thanks a great deal, everything works great.

-anthony
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Okay, so now that I have the sessions working on all sub-domains and domains. I'm having an issue when you’re logged in, if you go back to the home page you should be directed to the sub-domain where the member’s area is located, but your not. I have a $_SESSION variable that holds the value if your logged in your not.

Here is what I have...

Code: Select all

if($_SESSION['loggedin'] == 1)
{
	header('http://members.theeliteforces.com/index.php');
	
}

if($_SESSION['loggedin'] == 1)
{
	echo 'Session = logged';
}
else
{
	echo 'Session = not logged';
}
It echoes "Session = logged" but I don’t get redirected...

Here is what is at the top of the page...

Code: Select all

ob_start();
session_write_close();
session_set_cookie_params(0, '/', '.theeliteforces.com');
session_start();
Thanks, Anthony
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the close should come after the session is started...
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

feyd wrote:the close should come after the session is started...

Code: Select all

session_set_cookie_params(0, '/', '.theeliteforces.com');
session_start();
session_write_close();
This is how I’m starting most of the pages, depending if its the login page where ob_start(); is at the top.

Now I wont be redirect from the domain to sub-domain on login with the session_write_close(); being declared after the session is started.

What I don’t get is I have a little test shown in the above post, where it echoes’s out session = logged, but it won’t re-direct…under the some conditional statement.

Thanks for your help once again, Anthony
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your header call is missing one tiny piece of crucial information: Location.

Code: Select all

<?php

header('Location: http://example.com/full/url/to/script.php');

?>
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

feyd wrote:your header call is missing one tiny piece of crucial information: Location.

Code: Select all

<?php

header('Location: http://example.com/full/url/to/script.php');

?>
ohh damn, I feel stupid. Thanks...

EDIT: works like magic
Post Reply