/    Sign up×
Community /Pin to ProfileBookmark

$_SESSION not working without WWW

I have read about this problem before.

The code giving me trouble was written by someone else. I creates this:

$_SESSION[‘userid’]

When a user logs in. Here is the problem, if a user changes from [url]www.(website).com[/url] to (website).com, the session values disappears.

I tried solving the issue:

[code=php]
$subdect = explode(‘.’, $_SERVER[HTTP_HOST]);
if (count($subdect) >= 3)
{
$clean = “.”.$subdect[1].”.”.$subdect[2];
ini_set(“session.cookie_domain”,$clean);
}
else $clean = “.”.$_SERVER[HTTP_HOST];
session_set_cookie_params(3600, ‘/’, $clean);
session_start();
[/code]

I put that on every website where there is a session_start(), and that hasn’t solved the problem, so what’s going on?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 05.2010 — Does it work OK if you put in a literal value instead of doing the explode stuff? E.g.:
[code=php]
session_set_cookie_params(3600, '/', '.example.com');
session_start();
[/code]
Copy linkTweet thisAlerts:
@rpcarnellauthorFeb 05.2010 — It is the same thing.
Copy linkTweet thisAlerts:
@NogDogFeb 05.2010 — Just a couple things to try:

  • - Clear the cookies on your browser (for that domain, don't have to clear all).


  • - Remember that you'll need to do the same session_set_cookie_params() stuff on every page that does a session_start().


  • - On the off chance that you don't have error reporting turned on, check the error logs to make sure it's not something like the cookie not getting sent due to output already being sent to the browser prior to the session_start().
  • Copy linkTweet thisAlerts:
    @rpcarnellauthorFeb 05.2010 — The problem continues. I guess this is a server problem I won't be able to solve.

    Thanks anyway.
    Copy linkTweet thisAlerts:
    @MindzaiFeb 05.2010 — Another option might be to direct all requests without a subdomain to www:

    <i>
    </i>&lt;IfModule mod_rewrite.c&gt;
    RewriteCond &amp;#37;{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC,L]
    &lt;/IfModule&gt;


    I'm confused why NogDog's suggestions haven't worked though...
    Copy linkTweet thisAlerts:
    @SrWebDeveloperFeb 05.2010 — It is the same thing.[/quote]

    I found this note added on php.net for the session_set_cookie_params():

    [FONT=verdana]

    [/FONT][FONT=verdana][/FONT]Cross-domain Cookies do work in all browsers (path '/' server '.example.com'), except the case you try it in IE6/7 and the server name is retreived from :etc/hosts: file, in that case the cookie won't be even saved.[/QUOTE] (Nov 2008)

    If it's still true or not, don't know. I didn't write it! Just an FYI.

    -jim
    Copy linkTweet thisAlerts:
    @rpcarnellauthorFeb 05.2010 — The website was created to have subdomains. I have to mess with .htaccess a lot, and I also had to change the DNS to allow multiple domains. Maybe I messed something up. Perhaps the idea of redirecting non-subdomain URLs is the best solution.
    ×

    Success!

    Help @rpcarnell spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...