In a particular case the PHP session_start doesn't work
If I type an URL e.g. www.vdh-lelystad.nl the PHP session_start() does work very well. All the SESSION variables are available trhoughout the website visit.
If I type URL vdh-lelystad.nl (without www.) no PHP session_start() will be there. No SESSION variables are available.
(In Chrome or IE if you type a URL without www. they put it automatically in front of the URL).
Please can you tell me what to do about it so that if people type the URL without www. ther won't be any problem. First it cost me a lot to find out what happened, and second it already took me a lot of time to try something, so I could put it in front of the URL myself (e.g. $URL = $_SERVER['SERVER_NAME'];
$URL = explode('.', $_SERVER['SERVER_NAME']);
if(strtolower($URL[0]) <> 'www')
{
$_SERVER['HTTP_HOST'] = "www.vdh-lelystad.nl";
header("Location: " . $_SERVER['HTTP_HOST']);
exit;
}
But that doesnot work cause the second time the URL will be:
vdh-lelystad.nlwww.vdh-lelystad.nl.
You make me happy if you know a way to solve this problem.
Many thanks in advance.
Best regards
Marc Molthoff'
Modified
Chosen solution
I can understand why www.vdh-lelystad.nl and vdh-lelystad.nl might have two different sessions (based on the session cookie that PHP is setting), but I don't know why vdh-lelystad.nl would be able to start a session at all. ???
I usually use .htaccess to rewrite to mydomain.com URLs to www.mydomain.com URLs, but I'm sure you could accomplish the same thing in PHP. You probably shouldn't try to change $_SERVER['HTTP_HOST']; it seems that it should be read-only. Why not just use the new string when you set the Location header?
Read this answer in context 👍 0All Replies (4)
Chosen Solution
I can understand why www.vdh-lelystad.nl and vdh-lelystad.nl might have two different sessions (based on the session cookie that PHP is setting), but I don't know why vdh-lelystad.nl would be able to start a session at all. ???
I usually use .htaccess to rewrite to mydomain.com URLs to www.mydomain.com URLs, but I'm sure you could accomplish the same thing in PHP. You probably shouldn't try to change $_SERVER['HTTP_HOST']; it seems that it should be read-only. Why not just use the new string when you set the Location header?
Thanks jscher200o. Please could you be a bit more specific about using the .htaccess?
Apache checks the directory of the requested page and its parent directories for an .htaccess file before serving the page, and processes those files. This is an example of directives for that file which redirect all requests to the www subdomain:
# Rewrite all URLs to www.example.com URLs (except www) RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Please note that typos or errors in .htaccess can block access to your site's content, and using .htaccess files does degrade performance to some extent. But depending on your hosting, this might be the simplest approach.
Hello jscher2000,
Thanks a lot. I work great.
best regards
Marc