/    Sign up×
Community /Pin to ProfileBookmark

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:

[LIST=1]

  • [*]

    User fills in information, clicks button, post is used to send it to the next page.


  • [*]

    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


  • [*]

    I use sessions to retrieve the data and process it.


  • [/LIST]

    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

    to post a comment
    PHP

    15 Comments(s)

    Copy linkTweet thisAlerts:
    @criterion9Aug 02.2012 — 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.
    Copy linkTweet thisAlerts:
    @m610authorAug 02.2012 — Thanks for the reply.

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

    Some code:

    Page 1. getauthor.html
    [code=php]
    <form action="confauthor.php" method="post" name="form1" id="form1">[/code]


    Page 2. confauthor.php
    [code=php]<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>[/code]


    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.
    Copy linkTweet thisAlerts:
    @m610authorAug 02.2012 — 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/daviswrites/test/getauthor.html
    Copy linkTweet thisAlerts:
    @criterion9Aug 02.2012 — Session must always come before all output (best to be the first line in the file).
    [code=php]
    <?php
    session_start();
    ...

    [/code]
    Copy linkTweet thisAlerts:
    @m610authorAug 02.2012 — Session must always come before all output (best to be the first line in the file).
    [code=php]
    <?php
    session_start();
    ...

    [/code]
    [/QUOTE]


    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.
    [code=php]
    <?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);
    //}
    ?>

    [/code]


    And for page 3, but with the functions I am not using yet removed for clarity.
    [code=php]
    <?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);
    //}

    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @m610authorAug 03.2012 — 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/viewtopic.php?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."
  • Copy linkTweet thisAlerts:
    @criterion9Aug 03.2012 — 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.
    Copy linkTweet thisAlerts:
    @m610authorAug 03.2012 — 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
    Copy linkTweet thisAlerts:
    @criterion9Aug 03.2012 — 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.
    Copy linkTweet thisAlerts:
    @m610authorAug 07.2012 — 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.
    Copy linkTweet thisAlerts:
    @criterion9Aug 07.2012 — You may have hosting specific issues, but at least you'd know you code worked in a standard installation.
    Copy linkTweet thisAlerts:
    @m610authorAug 18.2012 — 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.
    Copy linkTweet thisAlerts:
    @m610authorAug 18.2012 — 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:


  • [code=php]header('P3P: CP="CAO PSA OUR"');
    ob_start();
    session_start();
    [/code]


    to declare a privacy policy before starting the session. That didn't work.

    It looks like I may being going back to using cookies.
    Copy linkTweet thisAlerts:
    @m610authorAug 18.2012 — 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.
    Copy linkTweet thisAlerts:
    @m610authorAug 19.2012 — Fixed. it. Damn it. It was easy, but not obvious, which I am learning seems to be pretty standard, especially for beginners like me.

    In the end it all I had to do was put the session_start() command at the very beginning. Before anything. Including the DOCTYPE line. W3Schools.com says to put it before the <html> line, but it has to go even higher.

    Having pretty much given up on $_SESSION I spent the day studying cookies for PHP and had figured out what I'd need to do if I was going to go that route. Basically, create the cookies in php, manipulate the data in php, then use JavaScript's window.onload to save the new values to the client computer. A bit awkward, but not horrible. I just had to use some "if {! isset($_COOKIE..." code to make sure I didn't overwrite existing cookies.

    I thought it was weird that the code to create the cookies had to come first. What idiot puts code before the start of the program, was what I was thinking, but that was the only thing that worked, and yes, I could stick my function CreateNewCookie in there, too.

    So that sent me back to php.net to read up more on session_start and there were many accounts of problems like what I was having, with solutions. One was
    Sorry for the noob comment but for those having the error :

    Warning: session_start(): Cannot send session cache limiter - headers already sent ...

    And before trying all the solutions added by the experts, simply ensure to have your php code before even the DOCTYPE tag like this (and not between the doctype and html tags as I was doing):

    <?php session_start(); ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Test Page</title>

    </head>

    <body>

    etc ....

    It might sound funny but I am sure this post will save time to some others noobs like me ... ![/QUOTE]


    Many thanks, alakys. I suffered plenty before I found your post and tried it so I didn't save much time, but damn I was getting headaches, cramps, a really sore butt, and was close to giving up, and I would have hated myself forever for that. ?

    I'll put it to bed tonight, get another late dinner, and will turn up my amp and bang on my guitar until I get some of this out of my system. It's 9:30 PM on a Saturday. The neighbors will understand. ?

    Mike

    P.S. How the hell did I ever get this to work at all? For the past week or so SESSIONS was working for me, on this one machine.
    ×

    Success!

    Help @m610 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...