Professional Web Applications Themes

Sessions not getting updated - PHP Development

Hi, I posted on this matter earlier with the subject line "Trouble with sessions," but someone else started a different thread with the same subject line that arrived before my post, so I'm changing my subject line in order to avoid confusion. I was asked to provide a code example for ysis, so here it is, along with a a description of what the problem is. I'm working with sessions and the values for HTML text fields (input="text") aren't getting updated after the first time I submit the form. If I shut down my browser and start anew, I can ...

  1. #1

    Default Sessions not getting updated

    Hi,

    I posted on this matter earlier with the subject line "Trouble with
    sessions," but someone else started a different thread with the same
    subject line that arrived before my post, so I'm changing my subject
    line in order to avoid confusion.

    I was asked to provide a code example for ysis, so here it is,
    along with a
    a description of what the problem is.

    I'm working with sessions and the values for HTML text fields
    (input="text") aren't getting updated after the first time I submit
    the form. If I shut down my browser and start anew, I can write to the
    session just once, as before. On the other hand, radio buttons -- used
    with the Sticky Multivalued Parameters script on pages 170-171 of
    O'Reilly's "Programming PHP" -- are updated every time. Here's an
    example of my code. Many thanks to anyone who can make sense of what's
    going on here.

    Best wishes,

    Mountain Man


    <html>
    <head>
    <title>First Form Sessions</title>
    </head>
    <body>
    <form action="form_processor.php4" method="POST">
    First Name: <input type="text" name="firstName" value="<?php echo
    $firstName ?>" /><br />
    <input type="submit" />
    </form>

    </body>
    </html>

    // Next, the form processing script which is on another page.
    // In my more complex script of actual use the form is only
    // included if it doesn't pass validation.

    <?php
    session_start();
    session_register("firstName");
    ?>

    <html>
    <head>
    <title>Form Processor Sessions</title>
    </head>
    <body>

    <?php
    echo "$firstName<br />";
    include 'first_form.php4';
    ?>

    </body>
    </html>
    Mountain Man Guest

  2. #2

    Default Re: Sessions not getting updated

    Mountain Man wrote:
    > I'm working with sessions and the values for HTML text fields
    > (input="text") aren't getting updated after the first time I submit
    > the form. If I shut down my browser and start anew, I can write to the
    > session just once, as before. On the other hand, radio buttons -- used
    > with the Sticky Multivalued Parameters script on pages 170-171 of
    > O'Reilly's "Programming PHP" -- are updated every time. Here's an
    > example of my code. Many thanks to anyone who can make sense of what's
    > going on here.
    >
    Using php version >= 4.1? Then start using the $_SESSION superglobal:

    <?

    // Enable sessions
    session_start();

    // Get the page URL
    $page = $_SERVER['PHP_SELF'];

    // Destroy the session var
    if (isset($_GET['unset'])) {
    if (isset($_SESSION['test'])) {
    unset($_SESSION['test']);
    header("Location: $page");
    exit;
    }
    }

    // Update/create the session var
    if (isset($_SESSION['test'])) {
    $_SESSION['test']++;
    } else {
    $_SESSION['test'] = 1;
    }

    // Debug output
    print "test = {$_SESSION['test']}<br />";
    print "<a href='$page?unset=1'>Destroy session</a>";

    ?>


    JW



    Janwillem Borleffs Guest

  3. #3

    Default Re: Sessions not getting updated

    Hi mountain man:

    (from)
    [url]http://nl.php.net/manual/en/function.session-start.php[/url]

    ---------------------------
    session_start

    (PHP 4 )
    session_start -- Initialize session data
    Description
    bool session_start ( void )

    session_start() creates a session or resumes the current one based on the
    current session id that's being passed via a request, such as GET, POST, or
    a cookie.

    This function always returns TRUE.

    Note: If you are using cookie-based sessions, you must call
    session_start() before anything is outputted to the browser.
    ---------------------------

    Read that nore carefully.
    Your code calls session_start halfway your script, AFTER output, so correct
    that first before anything else.
    Futhermore I see that you are echoing $firstName without initializing it.
    Did you put the (retarded) register globals on in your php.ini?
    Don't, it is bad.
    Modern php comes with registerglobals off by default.

    Better is this:
    $firstName = $_GET["firstName"]
    or
    $firstName = $_POST["firstName"]

    or even better:
    $firstName = (isset($_GET["firstName"]) ? $_GET["firstName"]: "unkown")


    Regards,
    Erwin Moller
    Erwin Moller Guest

Similar Threads

  1. CGI::Sessions : Deleting expired sessions
    By Vito Corleone in forum PERL Modules
    Replies: 1
    Last Post: June 15th, 02:49 PM
  2. Replies: 4
    Last Post: November 22nd, 08:36 PM
  3. Datagrid not updated during delete, but updated during insert and update
    By Dmitry Korolyov in forum ASP.NET Data Grid Control
    Replies: 3
    Last Post: September 23rd, 04:09 AM
  4. Database sessions and file sessions
    By Cditty in forum PHP Development
    Replies: 1
    Last Post: September 9th, 01:34 AM
  5. Imported + updated symbols don't stay updated
    By Bbonnn webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 1
    Last Post: August 8th, 01:13 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139