PHP session_start(); not working when I visit the domain with out www.?

Discussion in 'PHP' started by tdd1984, Nov 24, 2011.

  1. #1
    Why does my session not work when I visit mywebsite.com vs www.website.com. It works if I go to www.website.com. I have a script setup on page 2 that our forms takes the visitor into. On page 2 it is a https. I am using a header redirect on that page. It redirects if a session is not set.

    The problem is it always redirect back into page 1. However if I go to page 1 using www.mywebsite.com the session starts and assigned the proper variable just fine. If I go to mywebsite.com then submit the form to page 2 it redirects?
     
    tdd1984, Nov 24, 2011 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    if you assign the session to www.mywebsite.com and then send a user to mywebsite.com it will not transfer the session because it sees them as two different domains

    Edit: or the same thing the other way around
     
    Anveto, Nov 24, 2011 IP
  3. iliveforyou

    iliveforyou Active Member

    Messages:
    207
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    I'm not sure, but you can try to store the session in the database or in a page session.php (call the session.php each time on new page)
     
    iliveforyou, Nov 25, 2011 IP
  4. tdd1984

    tdd1984 Well-Known Member

    Messages:
    2,357
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    150
    #4
    That's interesting, where does it say that in the PHP manual though?

    By the way I setup a .htaccess to just redirect any non www. to www.mydomain.com
     
    tdd1984, Nov 25, 2011 IP
  5. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #5
    I don't believe it is in the php manual. To me it's just common sense that www.mydomain.com is different from mydomain.com. Think of the www as a subdomain. If you look at dns configurations you will see that the www is added. Someone else could probably explain this better.
     
    Anveto, Nov 25, 2011 IP
  6. Qarizma

    Qarizma Member

    Messages:
    55
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    45
    #6
    It's common sense. As a webmaster you have to decide to use www. only, or non-www only. I prefer the last one.
     
    Qarizma, Nov 27, 2011 IP
  7. pcsgroups

    pcsgroups Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    A session can't be transferred from one domain to other domain. If need to do this then you have to store you the session_id in you db and then redirect it to the other domain with user_id. As you arrives in 2nd domain, then you need to fetch the user_id and check the db is session is selected or not.

    After that you can go on with your job.
     
    pcsgroups, Nov 28, 2011 IP