1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to maintain session between Domain and subdomain in php

Discussion in 'PHP' started by im.imrankhan, Sep 1, 2008.

  1. #1
    Hi

    I am in a problem. I have a site Customermath.com. I have created a new subdomain on comlaint.customermath.com Here any one can come and post there complaint. I am also providing other service on customermath.com which need person to login. I want to maintain session between subdomain like if some one log in through customermath.com then he should not be asked to log in on complaint.customermath.com and vis-versa. Please help me if any one can.
     
    im.imrankhan, Sep 1, 2008 IP
  2. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try to upload a .htaccess file to your site root and subdomain root with the following content:
    php_value session.cookie_domain ".customermath.com"
     
    elias_sorensen, Sep 1, 2008 IP
  3. im.imrankhan

    im.imrankhan Active Member

    Messages:
    164
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Hi elisa
    thanks but this code is not working. If you can explain a little more it will be helpful. I have try but it show internal server error 500.
     
    im.imrankhan, Sep 1, 2008 IP
  4. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Well.. Do you have access to your php.ini?
     
    elias_sorensen, Sep 1, 2008 IP
  5. im.imrankhan

    im.imrankhan Active Member

    Messages:
    164
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    I am on shared hosting I don't thing I have access to php.ini
     
    im.imrankhan, Sep 1, 2008 IP
  6. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Hmm.. I advise you to contact your hosting provider, telling them that you can't set the value with and .htaccess (got same problem with URL-rewriting on one of my hosts and it returned an 505) :)
     
    elias_sorensen, Sep 1, 2008 IP
  7. im.imrankhan

    im.imrankhan Active Member

    Messages:
    164
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Hi elias
    I have talk to them they told me that I can put php.ini my domain root and it will work Now if you have any solution then please tell it will be very kind of you.
     
    im.imrankhan, Sep 1, 2008 IP
  8. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #8
    You need to find a php.ini and edit it. Maybe ask you host, if they could send their standard php.ini to you.

    After they've done that (or you've found one), you'll need to find the line where it says:
    session.cookie_domain, and change it's value to session.cookie_domain = ".customermath.com"

    Then upload to your root, and try again.
     
    elias_sorensen, Sep 1, 2008 IP
  9. im.imrankhan

    im.imrankhan Active Member

    Messages:
    164
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    thanks elisa

    I have follow the same procedure and it works. Now its maintaining the session through out the sub domains. Thanks for your timely help.
     
    im.imrankhan, Sep 1, 2008 IP
  10. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you for asking and answering, this is very helpfull :)
     
    Ilyes, Sep 1, 2008 IP
  11. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #11
    your're welcome.
     
    elias_sorensen, Sep 2, 2008 IP
  12. jobBole

    jobBole Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hi Elisa,

    My VB 4 website has same problem as well.

    When user has logged in, they can view session info under www.mydomain.com. However, if they change URL to forum.mydomain.com. The session info is disappeared.

    I followed your advice in this post, set the php.ini

    session.cookie_domain = .mydomain.com

    or

    session.cookie_domain = ".mydomain.com"

    But it still doesn't work. Any other aspects should be set in php.ini?

    Thanks
     
    jobBole, Aug 29, 2010 IP
  13. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #13
    The setcookie function allows you to do that without having to go and edit the php.ini file.

    Look for the setcookie function on VB4 and change it to something like this:

    
    /* expire in 1 hour */
    setcookie("TestCookie", "TestValue", time()+3600, "/", ".example.com", 1);
    
    PHP:
    This will set the cookie/session for all subdomains.
     
    ThePHPMaster, Aug 29, 2010 IP
  14. jobBole

    jobBole Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thank you, but I prefer to change configuration in php.ini. Any suggestions about it?
     
    jobBole, Aug 29, 2010 IP
  15. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #15
    jobBole:
    Remember to restart your server after changing the php.ini - otherwise, the server won't recognize the new settings.

    A second solution is to do it by .htaccess with php_value session.cookie_domain ".domain.com".

    You could also consider throwing session_set_cookie_params() before session_start(). Using that function you can set the domain allowing the session cookie to be used across subdomains.

    http://php.net/manual/en/function.session-set-cookie-params.php
     
    elias_sorensen, Sep 13, 2010 IP
  16. vikramkumar5

    vikramkumar5 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #16
    Hi All,

    Could you please suggest me how to do the above things in java. As I am also facing the same issue and wanted to share a common session in domain and sub domains website.

    Please reply me
    Vikram
     
    vikramkumar5, Sep 18, 2013 IP
  17. johannawilliams

    johannawilliams Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #17
    This can be done via the php.ini file. You need to save a copy of php.ini the root directory of your website. Then edit the line with the following code:

    session.cookie_domain =

    Change this to as below:

    session.cookie_domain = “.<domain name>”;

    For example if your domain is abc.com and subdomain is test.abc.com you will need to replace the domain name above with abc.com.
     
    johannawilliams, Sep 20, 2013 IP
  18. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #18
    You don't need to edit your php.ini file. vBulletin has cookie specific settings in the admin CP, you can change it there.
     
    nico_swd, Sep 30, 2013 IP