Results 1 to 3 of 3

Thread: session sharing between pages

  1. #1
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default session sharing between pages

    i am unable to access same session variable from different
    pages (the pages are on same server)

    the code works on my pc (wampserver)
    but when i upload online it doesn't work


    i set session veriable in one php file
    and when i check using isset() in another php file it returns false



    i would like to know if theres some some sort of php setting or
    could anybody suggest an alternate solution.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Session variables are available on any page using the same session.

    Include <?php session_start(); ?> at the top of every page (it must execute before any output is sent to the browser) and it should work. But it must be on every page, not just the first one.

    Sessions do expire after some time, but I don't think that's the problem here. (Usually expiration happens after 15 minutes or more of inactivity, or closing the browser.)


    Note that sessions cannot be shared across different domains because sessions use a cookie to store the session ID that connects everything. There is NO way to make a session function across multiple domains.*
    For the same reason, subdomains can be complicated: if you want to share a session across two subdomains you must set the cookie's domain to *.yourdomain.com


    Finally, you should make sure the default cookie path is set to / and not a local directory. If you set the session cookie (and therefore a session) in /somedir/ then it won't be available in /someotherdir/. But this is not the default for PHP as far as I know because it should save the directory as / by default-- that will make it available anywhere on your site. (And *.domain.com will make it available on any subdomain.)



    (*There are some very complicated work arounds but they generally do involve two separate sessions, linked together by some sort of integration between the servers such as sharing a database and remembering that the two separate sessions are connected. This is usually not required, and it's difficult to deal with when it is. I suggest avoiding it.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks djr33 for your very informative reply

    its working now!!!!!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •