Windows 10 reached EOS (end of support) on October 14, 2025. If you are on Windows 10, see this article.

Search Support

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Learn More

In a particular case the PHP session_start doesn't work

  • 4 replies
  • 3 have this problem
  • 29 views
  • Last reply by marmelac
  • Konponduta

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'

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.<br /> 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).<br /> 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'];<br /> $URL = explode('.', $_SERVER['SERVER_NAME']);<br /> <br /> <pre><nowiki> if(strtolower($URL[0]) <> 'www') { $_SERVER['HTTP_HOST'] = "www.vdh-lelystad.nl"; header("Location: " . $_SERVER['HTTP_HOST']); exit; }</nowiki></pre> But that doesnot work cause the second time the URL will be:<br /> vdh-lelystad.nlwww.vdh-lelystad.nl.<br /> You make me happy if you know a way to solve this problem.<br /> Many thanks in advance. Best regards<br /> Marc Molthoff'

Modified by cor-el

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 👍 0

All 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