SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Session problem

  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Session problem

    I am using sessions on a web site I've been working with for quite some time, but when testing some of the pages, I'm having problems with the sessions that I use from page to page to store some information.
    All of a sudden I think I read a tip from someone telling me that sessions are not really working anymore in newer versions of php, is that right?

    Then, what is the solution to make it work, when I have my page on a hosting server that is probably updated to the latest version.
    I use things like these in my pages:

    PHP Code:
    .
    session_start();

    session_register('username');
    $_SESSION['username'] = $username
    What do I need to change to make it work? Or is it an easier way to change something globally?

  2. #2
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    933
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brad62 View Post
    I am using sessions on a web site I've been working with for quite some time, but when testing some of the pages, I'm having problems with the sessions that I use from page to page to store some information.
    All of a sudden I think I read a tip from someone telling me that sessions are not really working anymore in newer versions of php, is that right?

    Then, what is the solution to make it work, when I have my page on a hosting server that is probably updated to the latest version.
    I use things like these in my pages:

    PHP Code:
    .
    session_start();

    session_register('username');
    $_SESSION['username'] = $username
    What do I need to change to make it work? Or is it an easier way to change something globally?
    Can you post the article that tells you session doesn't work anymore on newer versions of php?

    By the way are you using php alone or are you using a php framework?
    Quality codes are optimized and tested...
    Click here for inspiration..

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't have an article. I just thought I read something about it a couple of months ago. And I've had some problems with sessions lately. Just working with php.
    Maybe it's something else in my script that is causing the problem. I just woke up this morning and thought I heard something about it... but I might be wrong?

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the exact same problem. I just changed my host server from PHP 5.2.17 to PHP 5.4.2 and now sessions don't work. I can't log in to my own site.
    I know the reason why, session_register is depreciated in PHP 5.4+, but I don't know the new way to set sessions.
    Does anyone know?

  5. #5
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to use session_register any more, you can just do this:

    Code:
    $_SESSION['myvar']='Value';

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I thought, but after commenting out the session_registers my login still doesn't work.
    I changed the login script to echo the sessions, to check they are there, and they were.
    But, they are not available to other pages it seems.
    After login you are redirected to the homepage, it (and the other pages) start with a login check that looks at the session data. The page displays differently if you are logged in, and it's displaying like I'm not. If I try make the homepage echo the sessions, it doesn't show.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The sessions appear on the page they are created, but not on any other page. To simplify things I made a very simple test script, rather than having to look at the whole login script.
    This is page 1
    PHP Code:
    <?php
    $sessid
    =21;
    $sessvalue "This Text";
    $_SESSION['id']=$sessid;
    $_SESSION['ses']=$sessvalue;
    ?>
    <head>
        <title>Session Test Set</title>
    </head>
    <body>
        <h1>Session Test Set</h1>
        <p>This should set the session.</p>
        <p>Session ID is: <?php echo $_SESSION['id']; ?></p>
        <p>Session value is: <?php echo $_SESSION['ses']; ?></p>
        <p><a href="session2.php">Link to next page.</a></p>
    </body>
    When I view this page it displays the session info.
    The link goes to this page:
    PHP Code:
    <?php
    session_start
    ();
    $id=$_SESSION['id'];
    $sessvalue=$_SESSION['ses'];
    ?>
    <head>
        <title>Session Test Read</title>
    </head>
    <body>
        <h1>Session Test Read</h1>
        <p>This should read the session.</p>
        <p>Session ID is: <?php echo $id?></p>
        <p>Session value is: <?php echo $sessvalue?></p>
        <p><a href="session1.php">Link to other page.</a></p>
    </body>
    When I click the link to go to this page, I don't see any of the session data.
    What am I doing wrong?

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, forgive my stupidity.
    I just added session_start() to the first script and it now works.
    I did the same with my login script and that works too now.
    It's just that they did not need that with the old PHP that used session_register(), so I never thought to add it.


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
  •