www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Page starts new session

  1. #1
    Join Date
    Aug 2012
    Posts
    15

    Page starts new session

    New here. Frustrated. Searched and found helpful information. Tired stuff. Called Yahoo support and they couldn't help.

    I have a sequence of 3 pages:
    1. User fills in information, clicks button, post is used to send it to the next page.
    2. I grab the user info and store it in a session array and display it. User confirms it is OK and clicks the next button
    3. I use sessions to retrieve the data and process it.


    Pages 2 and 3 have the session_start(); line in it at the beginning of the php script.

    I checked the tmp file on the server and after page 2 loads a session file containing about 400 bytes shows up on the server. 400 bytes is about right.

    Once page 3 displays I see a second session file in the tmp folder. It contains 0 bytes. Also, the session info does not seem to be picked up by this page.

    I put a print_r ($_SESSION); line in the body of pages 2 and 3. On page 2 the correct data are displayed. On page 3 I just get "array = ()". The session array is empty.

    Am I doing this right?

    Why is a second session array/file being created?

    Thanks, Mike

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    Is it possible you are bumping to different domains (i.e. www.domain.com vs domain.com) between page loads? Are you getting any errors? Barring those two we might need to see some code.

  3. #3
    Join Date
    Aug 2012
    Posts
    15
    Thanks for the reply.

    All three pages are in the same folder on the server.

    Some code:
    Page 1. getauthor.html
    PHP Code:
    <form action="confauthor.php" method="post" name="form1" id="form1"
    Page 2. confauthor.php
    PHP Code:
    <head>
    .
    .
    <?php
    //****** Main section ***************************************************************
    // Here we pick up the data sent via post, then we clean it up and store it as part
    // of the user's session.

    session_start();
    //set_exception_handler('dw_Exception');
    //try
    //{
        
    GetPageData();
        
    FilterPageData();
        
    ParsePageData();
        
    ValidatePageData();
        
    StorePageData();
    //}
    //catch(Exception $e)
    //{
    //    dw_Exception($e);
    //}
    ?>
    </head>

    <body>
    <?php
        print_r 
    ($_SESSION);
    ?> 
    <form id="form1" name="form1" method="post" action="setauthor.php">

    <p>Click the <em>Accept</em> button to complete the submission process,
    or <em>Back</em> to go back and make changes.
    </p>

    <p align="center">
    <input type="submit" name="dw_AcceptBtn" id="dw_AcceptBtn" value="Accept" tabindex="1" />&nbsp;&nbsp;
    <input type="button" value=" Back " onclick="goBack()" />
    </p>

    </form>
    </body>
    Page 3. setauthor.php
    This is very similar to page 2.

    Page 2 works. (I didn't include the functions called in the main section.)
    Page 3 seems to be using a different session.

  4. #4
    Join Date
    Aug 2012
    Posts
    15
    BTW, I am testing this from the Yahoo servers because I don't have a server set up in my home office.

    You can try it yourself if you like. Unfortunately you won't be able to view the php code.

    Link: http://www.kstreetstudio.com/daviswr...getauthor.html

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    Session must always come before all output (best to be the first line in the file).
    PHP Code:
    <?php
    session_start
    (); 
    ...

  6. #6
    Join Date
    Aug 2012
    Posts
    15
    Quote Originally Posted by criterion9 View Post
    Session must always come before all output (best to be the first line in the file).
    PHP Code:
    <?php
    session_start
    (); 
    ...
    Thanks. I figured it was OK down below because other than defining and initializing some local variables I had not called any of the functions yet.

    I moved that line. No difference.

    Here's the full php script for page 2.
    PHP Code:
    <?php
    session_start
    ();
    $dw_ErrorMsg "";
    $dw_Error false;
    $dw_Data = array(
        
    '',    //  0 = Author's last name
        
    '',    //  1 = Author's rest name
        
    '',    //  2 = Address
        
    '',    //  3 = Phone
        
    '',    //  4 = Email
        
    '',    //  5 = Wed site
        
    '',    //  6 = Permission to publish email address
        
    '',    //  7 = Permission to publish web address
        
    '',    //  8 = Age was checked
        
    '',    //  9 = Photo, file name and path of a photo of the author
        
    '',    // 10 = Bio, file name and path of the author's bio
        
    '',    // 11 = Compensation terms accepted
        
    '');// 12 = Comments
        
    $dw_ValidEmail false;
    $dw_ValidWebSite false;
        
    function 
    dw_Exception($exception)
    {
        global 
    $dw_ErrorMsg$dw_Error;
        
        
    $dw_ErrorMsg $exception;
        
    $dw_Error true;
        
    //    SendErrorEmail($dw_ErrorMsg);
    }

    function 
    GetPageData()
    {
        global 
    $dw_Data;
        
        
    $dw_Data[0] = $_REQUEST["dw_LastName"];
        
    $dw_Data[1] = $_REQUEST["dw_RestName"];
        
    $dw_Data[2] = $_REQUEST["dw_Address"];
        
    $dw_Data[3] = $_REQUEST["dw_Phone"];
        
    $dw_Data[4] = $_REQUEST["dw_Email"];
        
    $dw_Data[5] = $_REQUEST["dw_Website"];
        
    $dw_Data[6] = $_REQUEST["dw_EmailOK"];
        
    $dw_Data[7] = $_REQUEST["dw_WebOK"];
        
    $dw_Data[8] = $_REQUEST["dw_AgeOK"];
        
    $dw_Data[9] = $_REQUEST["dw_Photo"];
        
    $dw_Data[10] = $_REQUEST["dw_Bio"];
        
    $dw_Data[11] = $_REQUEST["dw_CompensationOK"];
        
    $dw_Data[12] = $_REQUEST["dw_Comments"];
    }

    function 
    FilterPageData()
    {
        global 
    $dw_Data;
        
        for (
    $i=0$i<=12$i++)
        {
            
    $dw_Data[$i]=filter_var($dw_Data[$i],FILTER_SANITIZE_STRING);
        }
    }

    function 
    ParsePageData()
    {
        function 
    YesNoStr($S)
        {
            if (
    $S == "on") {return "Yes";} else {return "No";};
        }
        
        global 
    $dw_Data;
        
        
    $dw_Data[4]=filter_var($dw_Data[4],FILTER_SANITIZE_EMAIL);
        
    $dw_Data[5]=filter_var($dw_Data[5],FILTER_SANITIZE_URL);
        
    $dw_Data[6]=YesNoStr($dw_Data[6]);
        
    $dw_Data[7]=YesNoStr($dw_Data[7]);
        
    $dw_Data[8]=YesNoStr($dw_Data[8]);
        
    $dw_Data[11]=YesNoStr($dw_Data[11]);
    }

    function 
    ValidatePageData()
    {
        global 
    $dw_ValidEmail$dw_ValidWebSite;
        
        
    $dw_ValidEmail filter_var($dw_data[4], FILTER_VALIDATE_EMAIL);
        
    $dw_ValidWebSite filter_var($dw_data[5], FILTER_VALIDATE_URL);
    }

    function 
    StorePagedata()
    {
        global 
    $dw_Data;
        
        
    $_SESSION['dw_LastName'] = $dw_Data[0];
        
    $_SESSION['dw_RestName'] = $dw_Data[1];
        
    $_SESSION['dw_Address'] = $dw_Data[2];
        
    $_SESSION['dw_Phone'] = $dw_Data[3];
        
    $_SESSION['dw_Email'] = $dw_Data[4];
        
    $_SESSION['dw_WebSite'] = $dw_Data[5];
        
    $_SESSION['dw_EmailOK'] = $dw_Data[6];
        
    $_SESSION['dw_WebOK'] = $dw_Data[7];
        
    $_SESSION['dw_AgeOK'] = $dw_Data[8];
        
    $_SESSION['dw_Photo'] = $dw_Data[9];
        
    $_SESSION['dw_Bio'] = $dw_Data[10];
        
    $_SESSION['dw_CompensationOK'] = $dw_Data[11];
        
    $_SESSION['dw_Comments'] = $dw_Data[12];
    }

    //****** Main section ***************************************************************
    // Here we pick up the data sent via post, then we clean it up and store it as part
    // of the user's session.

    //set_exception_handler('dw_Exception');
    //try
    //{
        
    GetPageData();
        
    FilterPageData();
        
    ParsePageData();
        
    ValidatePageData();
        
    StorePageData();
    //}
    //catch(Exception $e)
    //{
    //    dw_Exception($e);
    //}
    ?>
    And for page 3, but with the functions I am not using yet removed for clarity.
    PHP Code:
    <?php
    session_start
    ();
    $dw_ErrorMsg "";
    $dw_Error false;
    $dw_Data = array(
        
    '',    //  0 = Author's last name
        
    '',    //  1 = Author's rest name
        
    '',    //  2 = Address
        
    '',    //  3 = Phone
        
    '',    //  4 = Email
        
    '',    //  5 = Wed site
        
    '',    //  6 = Permission to publish email address
        
    '',    //  7 = Permission to publish web address
        
    '',    //  8 = Age was checked
        
    '',    //  9 = Photo, file name and path of a photo of the author
        
    '',    // 10 = Bio, file name and path of the author's bio
        
    '',    // 11 = Compensation terms accepted
        
    '');// 12 = Comments
        
    $dw_RegData = array(
        
    '',    //  0 = AuthorID number
        
    '',    //  1 = Date registered
        
    '',    //  2 = Time registered
        
    '',    //  3 = Browser
        
    '',    //  4 = Host
        
    '',    //  5 = IP address
        
    0,    //  6 = Number of submissions by this author
        
    '',    //  7 = Author's folder
        
    '');//  8 = Full path to author's folder
        
    $dw_Status = array(
        
    '',    //  0 = Submission deadline
        
    '',    //  1 = Author count
        
    '',    //  2 = Submission count
        
    '');//  3 = Page count

    function dw_Exception($exception)
    {
        global 
    $dw_ErrorMsg;
        
        
    $dw_ErrorMsg $exception;
        
    $dw_Error true;
        
    //    SendErrorEmail($dw_ErrorMsg);
    }

    function 
    GetPageData()
    {
        global 
    $dw_Data;

        
    $dw_Data[0] = $_SESSION['dw_LastName'];
        
    $dw_Data[1] = $_SESSION['dw_RestName'];
        
    $dw_Data[2] = $_SESSION['dw_Address'];
        
    $dw_Data[3] = $_SESSION['dw_Phone'];
        
    $dw_Data[4] = $_SESSION['dw_Email'];
        
    $dw_Data[5] = $_SESSION['dw_WebSite'];
        
    $dw_Data[6] = $_SESSION['dw_EmailOK'];
        
    $dw_Data[7] = $_SESSION['dw_WebOK'];
        
    $dw_Data[8] = $_SESSION['dw_AgeOK'];
        
    $dw_Data[9] = $_SESSION['dw_Photo'];
        
    $dw_Data[10] = $_SESSION['dw_Bio'];
        
    $dw_Data[11] = $_SESSION['dw_CompensationOK'];
        
    $dw_Data[12] = $_SESSION['dw_Comments'];
    }

    function 
    ReadProjectStatus()
    {
        global 
    $dw_Status;

        
    // Read the project status file.
        
    $Fn "files/status.txt";
        
    $file fopen($Fn,"r");
        
    $dw_Status[0] = fgets($file);
        
    $dw_Status[1] = fgets($file);
        
    $dw_Status[2] = fgets($file);
        
    $dw_Status[3] = fgets($file);
        
    fclose($file);
        
        
    // Get the value from each string.
        
    $dw_Status[0] = substr($dw_Status[0],strpos($dw_Status[0],": ")+2);
        
    $dw_Status[1] = substr($dw_Status[1],strpos($dw_Status[1],": ")+2);
        
    $dw_Status[2] = substr($dw_Status[2],strpos($dw_Status[2],": ")+2);
        
    $dw_Status[3] = substr($dw_Status[3],strpos($dw_Status[3],": ")+2);
    }

    //set_exception_handler('dw_Exception');
    //try
    {
        
    GetPageData();
        
    ReadProjectStatus();
    //    GetAuthorID();
    //    GetAuthorFolder();
    //    CreateAuthorFolder();
    //    WriteInfoFile();
    //    WriteAuthorList();
        // Send emails
    }
    //catch(Exception $e)
    //{
    //    dw_Exception($e);
    //}

    ?>

  7. #7
    Join Date
    Aug 2012
    Posts
    15
    Some things I have found and tried

    1. ...session_start(); should be your first operation you do even before the header('Content-Type: text/html; charset=UTF-8');

    I tried it and it did not help.

    2. Client computer is not accepting cookies.

    My computer does accept cookies. I checked. Not sure if there is any reason the session cookie would be different.

    3. Send the SID manually through the url <a href="page2.php?' . SID . '">page 2</a> or a cookie. Ugly, but I'll try this later.

    4. Changing a php.ini file setting fixed it for one guy. The setting:

    session.referer_check = "www.my-freaking-old-domain.com"

    For this guy it was set to an old domain. I don't think I have access to this file via Yahoo Web Hosting.

    5. "Internet Explorer just refused to store my cookies, while other browsers are fine. I found a post stating that if you have an underscore in your url things can go mad in IE, it just refuses. I had an underscore in my url which was resolved by removing the underscore from my domain name. This cost me a day of investigation, so I hope this saves some of you some time."

    6. Another server-side solution: "I thought I had solved this already with the .htaccess method described earlier. Alas, I found out that my hosting provider uses FCGI interface to run PHP on top of Apache, instead of mod_php that would have complied. It took me a good half a year to try the fix they provided - lo and behold, now it works! I remind you that I encountered the problem only with the hosting provider. Locally (Win XP) the sessions work as they should be. Here's how I did it (only applicable with my host):" Source: http://forum.symfony-project.org/vie...p?f=22&t=30214

    7. "I changed the value for $config[‘cookie_prefix’] and now everything is working fine."

    8. Third party cookies are enabled on my PC. Is it possible that the cookie expires before I get to use it again, a few seconds later?

    9. "Long story short, when I redirected with a forward-slash "/" AFTER the .php, the page would create a different session ID than the rest of my domain was using."

    10. This was mentioned - "What I realized was that the URL was changing from www.coryforsyth.com to coryforsyth.com. When the presence of the WWW changed, PHP thought it was a different session and created an empty one that caused my script to log me out. If I went to the location bar of my browser and added/removed the WWW (to the way it had been before), all was well and I was still logged in."

    11. Another IE issue: "You can lose the session however if the the page contains a reference to <img src=""> with name and id references (which may be used if the image is referencing a dynamic image, called by javascript) This seems to cause IE6 to refresh the page session id and hence loose the session."

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    Do you have error reporting on? If so, maybe you can share any errors that are displayed or found in the log? A lot of times PHP will try to tell you when there is a session instantiation problem.

  9. #9
    Join Date
    Aug 2012
    Posts
    15
    Maybe. I am not sure. How's that for a confidence builder?

    I don't have php or and and server software on my PC. I've never written server or even database apps before. I ftp my php files to my Yahoo-hosted site and test them there.

    I've seen the access log folder and looked at the most recent log. It's difficult to find any mention of errors in that. There has to be an error log somewhere.

    This is my first ever php project. Last week was my first JavaScript project. I'm learning this all on my own at home using resources available on the Internet. It's working out for the most part, although I can get really stuck at times.

    My last post was my collection of all the things I found that can cause the problem I was (Yes, was.) having. A lot of people have spent a lot of time on this type of problem and often the problem is on the server side, which I do not have access to.

    Late last night I was about to give up and go back to using cookies. That's how I was doing this when I started this project. I was also starting to think about finding another hosting service. Then... it started working. I don't know how or why, but it just started working. That's good in a sense, because it is working, but bad because I didn't learn anything and I may end up dealing with this again.

    I had tired a number of the things suggested on other forums, and here, and none seemed to work, but somehow, now it all does.

    My programming background is mostly in Delphi and I have done a fair bit of desktop app development using it. I'm finding JS and php odd in how loose these seem to be, as in declaring variables and variable types, and also how tight in that both are case sensitive. And I often find myself typing = instead of == in IF statements, for example, so acclimating to the php language itself has gone slower than I expected. That's why I held off forever before joining a forum to ask for help, because such minor typos can cause me trouble and I can't go around asking people for help with that. I suspect I corrected such a typo and now things are working, and that really bugs me.

    In my JS project I started using try/catch early to tell me what my errors were and that helped greatly. I have this in php now but had not fully implemented it, and now that I have I'm still not seeing my errors. I guess the server gets them all.

    Thanks for the support and for providing a place to vent.

    Mike

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    Let us know if the problem comes back.


    On a side note, if you are planning to do any further development in PHP a good idea is to install a localhost server to do your development on. That way you can make sure something is working and isn't an environment problem.

    Wamp is probably the easiest way to get a localhost up and running in windows.

  11. #11
    Join Date
    Aug 2012
    Posts
    15
    Thanks. I''ll look into wamp, but I have to keep in mind at my Yahoo host might do things differently.

    BTW, the problem has not come back.

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    You may have hosting specific issues, but at least you'd know you code worked in a standard installation.

  13. #13
    Join Date
    Aug 2012
    Posts
    15
    The problem has returned, but not on my current machine.

    The project was nearly done and I was doing a lot of final testing, then switched to using my other PC to test from there. On that machine the session array is cleared on when I navigate between pages.

    If it works on one PC but not another I don't see how it can be a server problem.

    My first machine is running Windows 7 and I am using FireFox. The second machine is running XP Pro and I get the same problems with FireFox (14.01) and IE.

    I checked security settings and turned off ad-blocker and other stuff. I've got the same antivirus software on both machines, eset. I cleared the cookies for this site and I set the security settings for FF to the same as on my first machine. I cleared all of the session files on the web server.

    Still, the SESSION data is being lost when I navigate between pages, but only on the XP machine.

  14. #14
    Join Date
    Aug 2012
    Posts
    15
    I worked until past 3 AM on this last night. It's very frustrating. Got some sleep and it didn't fix itself over night.

    Other things read and tried or not applicable here.

    1. Underscores in file names messes up IE and dumps the session. I checked and I did use underscores in three "include" files. Fixed that and a previously ignored error showed up, using the same name for a function in two different "include" files. Fixed all that but my sessions are still being dropped.

    2. Cookies size is reportedly a problem. My session data is only 100-200 bytes. The second machine does have a lot of cookies stored by the browser but that can't be what they mean.

    3. W3Schools says that the session_start command should go before the <html> tag. I moved it. No change.

    4. One person solved their problem by using the following:

    PHP Code:
    header('P3P: CP="CAO PSA OUR"');
    ob_start();
    session_start(); 
    to declare a privacy policy before starting the session. That didn't work.

    It looks like I may being going back to using cookies.

  15. #15
    Join Date
    Aug 2012
    Posts
    15
    Just tried it on my laptop (Win Vista 64-bit, FF 3.something). Same problem. I have to assume everyone is going to have this problem.

    Strange that it works on this machine, and that in the beginning it had the same problem but somehow it went away.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles