Cookies and Subdomains

Using a single web hosting account to host multiple sites

Cookies and Subdomains

Postby Fredev » Wed Nov 29, 2006 6:46 am

Hi, got subdomain to directory rewriting enabled with this code

Code: Select all
RewriteCond %{HTTP_HOST} !^(www\.)?domain.nl$ [NC]
RewriteCond %{HTTP_HOST}  ^(www\.)?([^\.]+)\.domain.nl$ [NC]
RewriteCond %2---%{REQUEST_URI} !^(.+)---/\1(/.*)?$
RewriteRule .* /%2%{REQUEST_URI} [QSA,L]


This all works fine, but...... now i have a cookie problem

when logging in at sub.domain.nl, the loginscript a domain.nl/sub/index.php will show up.

In this script, there will be checked for existing cookies 'name'and 'md5pass' . These cookies do exist, for the domain sub.domain.nl.

But although the browsers address bar says were at sub.domain.nl, actually the script is at domain.nl/sub/index.php.

For safety reasons, a browser can only read cookies from its own (sub)domain i guess.

Any Ideas?
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Postby richardk » Wed Nov 29, 2006 7:21 am

The browser doesn't know it's accessing domain.nl/sub/index.php. Are you sure the cookies are being set corretly to begin with?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Fredev » Wed Nov 29, 2006 12:30 pm

cookies are stored in the sub.domain.nl context

but when i try to read them from sub.domain.nl (which actually is domain.nl/sub/index.php) they do no seem to exist.

When I add the R flag to RewriteRule .* /%2%{REQUEST_URI} [QSA,L] I'm able to read the cookies as expected

The address bar then looks like

sub.domain.nl/sub, so that's no what i want
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Postby richardk » Wed Nov 29, 2006 2:41 pm

Cookies work fine for me. Try simplifying your code (only set the cookie and read the cookie) and testing. If you can't get the simplified version to work post it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Solved

Postby Fredev » Thu Nov 30, 2006 4:06 am

The login form action='' contained
$_SERVER['PHP_SELF']

While the addressbar contains sub.domain.nl/login.php, $_SERVER['PHP_SELF'] isn't /login.php, but /sub/login.php

So, after submitting the form, the address bar contains 'sub.domain.nl/sub/login.php'

Somehow php is looking for /sub cookies instead of / cookies.

Changed action=$_SERVER['PHP_SELF']" into action="login.php", which solved the problem
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 91 guests

cron