www.webdeveloper.com
Results 1 to 3 of 3

Thread: Using $_SESSION properly

  1. #1
    Join Date
    Apr 2013
    Posts
    23

    Using $_SESSION properly

    I'm having problems with the $_SESSION code.
    I can't retrieve the info on the next page.

    After looking at the manual (which I think lacks enough examples / explanation) I came to this:

    <?php
    session_start();
    if (empty($_SESSION['count'])) {
    $_SESSION['count'] = 1;
    } else {
    $_SESSION['count']++;
    }
    ?>

    <html>
    <form enctype="multipart/form-data" method="POST" action="ads.php?<?php echo $_SESSION["count"]; ?>" />
    <input type="hidden" name="PHPSESSID" value="<?php echo $_SESSION["count"]; ?>" />
    Name:<input type="text" name ="name" /><br />
    Book:<input type="text" name ="book" /><br />
    Subject:<input type="text" name ="subject" /><br />
    Price (CAN):<input type="text" name ="price" /><br />
    Description: <br /><textarea cols="40" rows="5" name="description">
    </textarea><br />
    Click to upload:<input type="file" name="pix" /><br />
    <input type="submit" name="Upload" value="Upload Picture" />
    </form>
    </html>
    I managed to get the ID to send in a link to the other page.

    But on the next page, when I write:
    <?php
    echo $_SESSION['PHPDSESSID'];
    echo "<b>Name: </b>" . $_SESSION['name'] . "<br />";
    ?>

    How do I retrieve the info from the URL using $_SESSION? Also how do I generate the proper text from $_SESSION using the ID from the link.

    I'm open to anything you have in mind.

  2. #2
    Join Date
    Apr 2013
    Posts
    56
    You do not retrieve data from url with $_SESSION. In your code you have form which has a method "POST". This means that, once your form will be submitted via method you specified(POST in your case), you will be able to get post data via $_POST variables and not via $_SESSION variables.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Yep, session data and form data are two separate things. All you need (normally) to do to access session data is to stick a session_start() in any page where you want to access it (before any output and before any reference to $_SESSION), while your form data will be in $_POST or $_GET as applicable (or you can use $_REQUEST in rare cases where it could be either POST or GET).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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