PHP & Cookies order form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Ruscoe

    PHP & Cookies order form

    Hi chaps,

    I'm relatively new to the language, but I want to create a simple order
    form using PHP and cookies. Please let me know if there's a better way
    in this situation, database isn't availible.

    What I'm trying to get is this:

    1) Variable(A) passed from HTML page to script.
    2) Script checks for cookie.
    3) If no cookie, script creates one, Variable(A) the value.
    4) If cookie, script adds Variable(A) value.

    Then the script should produce an "order form" type page, listing the
    variables within the cookie.

    I'm new to PHP and I have no experience of cookies. I may be going about
    this the wrong way, so please let me know!

    Many thanks for any help,

    - Daniel
  • Chung Leong

    #2
    Re: PHP & Cookies order form

    Tinkering with browser cookies directly is so...passe. Use PHP sessions
    instead:

    <?php

    session_start() ;

    if(is_array($_S ESSION['orders'])) {
    $_SESSION['orders'] = array();
    }
    array_push($_SE SSION['orders'], $a); // from your form

    ?>

    Uzytkownik "Daniel Ruscoe" <contact@websit e.plz> napisal w wiadomosci
    news:MPG.1a3c63 123a95292b98976 7@news.btopenwo rld.com...[color=blue]
    > Hi chaps,
    >
    > I'm relatively new to the language, but I want to create a simple order
    > form using PHP and cookies. Please let me know if there's a better way
    > in this situation, database isn't availible.
    >
    > What I'm trying to get is this:
    >
    > 1) Variable(A) passed from HTML page to script.
    > 2) Script checks for cookie.
    > 3) If no cookie, script creates one, Variable(A) the value.
    > 4) If cookie, script adds Variable(A) value.
    >
    > Then the script should produce an "order form" type page, listing the
    > variables within the cookie.
    >
    > I'm new to PHP and I have no experience of cookies. I may be going about
    > this the wrong way, so please let me know!
    >
    > Many thanks for any help,
    >
    > - Daniel[/color]


    Comment

    • Robert Downes

      #3
      Re: PHP &amp; Cookies order form

      Daniel Ruscoe wrote:
      [color=blue]
      > Hi chaps,
      >
      > I'm relatively new to the language, but I want to create a simple order
      > form using PHP and cookies. Please let me know if there's a better way
      > in this situation, database isn't availible.
      >
      > What I'm trying to get is this:
      >
      > 1) Variable(A) passed from HTML page to script.
      > 2) Script checks for cookie.
      > 3) If no cookie, script creates one, Variable(A) the value.
      > 4) If cookie, script adds Variable(A) value.
      >
      > Then the script should produce an "order form" type page, listing the
      > variables within the cookie.[/color]

      Do you need a cookie? Do you need to know the value of the data beyond
      the order form page? Could you send data straight to the order form page
      using forms, and perhaps hidden form fields?

      If you do need to keep the data for several pages, then using PHP
      sessions may be useful. Sessions use a cookie to store a session key
      that points to a set of session data on the PHP server (though make sure
      to think about security of session variables and whether there's a risk
      of the session keys being intercepted over open connections). See the
      Session handling functions (chapter XCV) in the PHP user manual.

      If you need to store the data on the user's machine for more than
      several pages, e.g. for several hours, days, months, or years, then
      cookies are the way. Again, make sure to think about security of data
      being stored on the remote machine, and about transmitting the data over
      open (non-encrypted) connections.
      --
      Bob
      London, UK
      echo Mail fefsensmrrjyahe eoceoq\! | tr "jefroq\!" "@obe.uk"

      Comment

      • Daniel Ruscoe

        #4
        Re: PHP &amp; Cookies order form

        In article <lbydnQGIEJY2bk 6iRVn-gw@comcast.com> , Chung Leong says...[color=blue]
        > Tinkering with browser cookies directly is so...passe. Use PHP sessions
        > instead:
        >
        > <?php
        >
        > session_start() ;
        >
        > if(is_array($_S ESSION['orders'])) {
        > $_SESSION['orders'] = array();
        > }
        > array_push($_SE SSION['orders'], $a); // from your form
        >
        > ?>[/color]

        Thanks for that, I'm beginning to get the basics now. I've looked up
        sessions and I see how they work. Looks like the perfect solution.

        I'm stuck trying to figure out how I would add multiple items to a
        session.

        Say I want a form on one page, when the submit button is clicked 4
        variables are sent to a PHP script to be added to a session.

        Then I'd need that script to list them in an order form style:

        Title Title Title Title
        Variable1(A) Variable1(B) Variable1(C) Variable1(D)

        With the ability to add more sets of variables afterwards to give:

        Title Title Title Title
        Variable1(A) Variable1(B) Variable1(C) Variable1(D)
        Variable2(A) Variable2(B) Variable2(C) Variable2(D)

        Can anybody point a beginner in the way of some code?

        Thank you.

        - Dan

        Comment

        Working...