Need advice on which way is the best to pass variables

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello All!

Still very new to PHP and I was wondering about the easiest and simplest way  
to go about doing something for a project I am working on.  I would simply  
like advice on what I'm asking so I can go and learn it myself through doing  
(best way for me).

I am building a card game as a learning-project as it involves many (to most  
of the) things that I would like to learn to do with PHP.

This project is intended to be Flat-File only, no SQL connections.  I'd much  
rather not use cookies for this if at all possible.

The project involves loading different pictures on the top of the page while  
the game is being played.  That's easy enough to do, but it forces a refresh  
when the picture changes.

which brings me to needing to find the porper solution to the following:

I am needing to keep roughly 12 or so variables tracked during the game.  
This should be automatically parsed when the user clicks on 'DEAL' or  
whatever to go to the next turn (going to the next turn would result in a  
different picture being shown and thus force a refresh of the page)

I am not asking particularly HOW to pass the variables, as I have 3 options  
that should work, I am simply asking which of these ideas seems to be the  
most correct way to accomplish this task.

Method 1:  pass the PHP variable through the form from the VALUE.....such as
echo "<input...................VALUE='<?php>$MyVariable</?>";

Method 2:  write the variables to a text file (or some other file) and read  
the file at the beginning of the turn and then write to the file at the end  
of the turn, using the FILE commands.

Method 3:  Start a session at the onset of the game and declare the  
variables at session variables and work with those using the SESSION libray.

I am relatively inexperienced with PHP but am fluent with other languages,  
so the concepts are not foreign, only the syntax.

So your guidance on the best way to pass an arbitrary number of variables  
during the refresh would be most helpful to point me in the direction that I  
need to focus my studies on.

thanks All!

Re: Need advice on which way is the best to pass variables

contained the following:

Quoted text here. Click to load it

The disadvantage of hidden fields is that they are visible if you view
the source. And of course they can be changed by the user which might
not be what you want in a game.

I'd say sessions are your best bet provided they game is designed to run
over a relatively short time period. If you want extended play (over a
number of sessions) then files or database is the way to go.

Geoff Berrow  0110001001101100010000000110

Re: Need advice on which way is the best to pass variables

Quoted text here. Click to load it
 I think you should take a look at AJAX, or simply the "httprequest" object.
That way you can send and recieve data from the server, and display it,
without having to refresh the page.
You could also create a session if you want your data to persist during the
Vince Moran

Re: Need advice on which way is the best to pass variables

Quoted text here. Click to load it

sessions are certainly your best bet, as 2 and 3 are essentially the
same (sessions are flat files), except the hard work is done for you

if you intend your game to last across sessions (browser close /
logout etc...) then you will have to serialise the session data and
write to a flat file, and deserialise back into a session later. The
way you keep track of those files is up to.

you would write your app using $_SESSION['var_name'] instead of
$var_name, it's almost as simple as that, using sessions avoids
prediction attacks which might leave your game open to cheats (if you
didn't code sufficiently randomised filenames etc...), although in a
LAN environment, sessions are not enough protection from an ARP/proxy
cheater - but it all adds to the fun huh!

Re: Need advice on which way is the best to pass variables

I can't add anything to the great advice you've gotten except to point
out that sessions do involve using a cookie, something you mentioned
you didn't want to do.


Re: Need advice on which way is the best to pass variables

Quoted text here. Click to load it

Sessions do not need to involve a cookie if you include the session ID
on every URL a user clicks on.

What you will struggle with, however, is storing the state of play of
the game in progress without writing to a database or file at some

Re: Need advice on which way is the best to pass variables

Quoted text here. Click to load it

It has to be said that *any* data passed as a result of playing the
game will be alterable, but the idea behind using a session is that
you dont have to carry the full weight of the variables on each round
You could, say encrypt a string and send it as a hidden field, (rather
like the postback, but using real encryption)
there are many possibilities, but ultimately the convienience of a
session wins through.
Although it does involce a cookie - it is one handled entirely with
javascript as a HTTP header, and does not need to be interacted with
in any way with client logic, the logic is all server side, the only
thing the client does is handle the posting of the data0
When using the session ID in the URL as a query string, (or indeed
storing it somewhere in the markup) you trade the lack of a cookie,
with the high visibility to a casual onlooker (or proxy log etc..) of
the session ID which can enable full session privs without needing to
know the password. At least a header is slightly less obvious, but of
course is still interceptable.

Re: Need advice on which way is the best to pass variables

Thanks everyone!

I dinkered around a bit with sessions last night, and once i figured out  
that you cannot have anything else going on BEFORE you opena sesison, I  
think that's the way to go.

since I'm making this project as my own learning experience, I'm not really  
worried about security, or game hacking, etc.

thank you fro the help


Quoted text here. Click to load it

Site Timeline