By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

PHP & Cookies order form

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tinkering with browser cookies directly is so...passe. Use PHP sessions
instead:

<?php

session_start();

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

?>

Uzytkownik "Daniel Ruscoe" <co*****@website.plz> napisal w wiadomosci
news:MP************************@news.btopenworld.c om...
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

Jul 17 '05 #2

P: n/a
Daniel Ruscoe wrote:
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.


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 fefsensmrrjyaheeoceoq\! | tr "jefroq\!" "@obe.uk"
Jul 17 '05 #3

P: n/a
In article <lb********************@comcast.com>, Chung Leong says...
Tinkering with browser cookies directly is so...passe. Use PHP sessions
instead:

<?php

session_start();

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

?>


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
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.