PHP variables....

C++, C#, Java, PHP, ect...
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

PHP variables....

Post by Torniquet »

How do i carry one variable set on one page to another page?

im sure i have seen where to do this... but cant find it again :(

any1 help me please :)


<3
New Site Coming Soon! Stay tuned :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP variables....

Post by hallsofvallhalla »

$_GET or $_POST

I use it in the videos quite a lot.
jpoisson
Posts: 245
Joined: Sun Aug 02, 2009 5:12 pm

Re: PHP variables....

Post by jpoisson »

You can use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.

Session:

Code: Select all

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];
Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

Cookie:

Code: Select all

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];
The big difference between sessions and cookies are that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.

GET and POST

You can either add the variable in the link to the next page:

Code: Select all

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>
This will create a GET variable, or include a hidden field in a form that submits to page two:

Code: Select all

<form method="get" action="page2.php">
    <input type="hidden" name="varname" value="var_value">
    <input type="submit">
</form>
And then on page two

Code: Select all

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.
Laguages:
PHP, MYSQL, (X)HTML, HTML5, JQuery, CSS 3.0,
C, C#, C++, Python, Pascal, Perl, Ruby, Turing

Software:
Adobe MC CS4, Visual Studio 2008, Notepad++,
NetBeans IDE, WAMPSERVER

Browsers:
Internet Explorer, Firefox, Opera, Safari, Chrome
(Always have latest patches for browsers.)

Free time:
...
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP variables....

Post by hallsofvallhalla »

nice explanation jpoisson

also remember your player base. If you plan on having thousands of players you will be created hundreds even thousands of concurrent sessions. That can become a memory issue depending on your server.

$_POST variables are very secure
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP variables....

Post by Torniquet »

well im keepin sesions down to a minimum. and using cookies for stuff like battles to stop people escaping (i.e closing down the browser) from battles without taking concequences.

i did consider POST and GET variables... but its not just like a 1 word variable. its entire sentances lol.

creature has caused X damage to you

you have caused X damage to creature.

so instead of going down the route halls did. and clicking attack then clicking go back to fight or what ever it was. you click attack and it goes to another page does the turns then returns you to the fight page without you seeing nothing else.
New Site Coming Soon! Stay tuned :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP variables....

Post by hallsofvallhalla »

a easy fix to people leaving the browser in the middle of a fight is have the battle page minus 100exp per level or whatever when it loads. Then place text saying "If you leave the battle improperly you will suffer an exp loss." Then when either, the player kills the creature, dies, or runs, it adds the exp back.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP variables....

Post by Torniquet »

no im making it so there is no escape other than dyin winning or running.and if you close the browser and come back to the site. you are back in battle. also you cant change page untill the battle is over (do something else. go to shops. veiw profile pages etc)
New Site Coming Soon! Stay tuned :D
jpoisson
Posts: 245
Joined: Sun Aug 02, 2009 5:12 pm

Re: PHP variables....

Post by jpoisson »

so like the battle is paused until you get there so no one else can come along and snag your monster?
Laguages:
PHP, MYSQL, (X)HTML, HTML5, JQuery, CSS 3.0,
C, C#, C++, Python, Pascal, Perl, Ruby, Turing

Software:
Adobe MC CS4, Visual Studio 2008, Notepad++,
NetBeans IDE, WAMPSERVER

Browsers:
Internet Explorer, Firefox, Opera, Safari, Chrome
(Always have latest patches for browsers.)

Free time:
...
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP variables....

Post by Torniquet »

no it just pauses the battle.

thats another flaw i see in halls way of doing it (no offence :p) one person goes up against a creature starts fighting it... then someone gets the same creature and it starts off with like half hp. then everything gets messy because 2+ people are trynig to fight the same thing with constatly changing variables.

so i just store the stats inside cookies and use them to do do all the stuff it needs to then delete them at the end of the battle. means i dont have to keep updating the creature list and putting in about 1000 of each creature to try stop people getting the same one.

or am i missing something with halls way lol?
New Site Coming Soon! Stay tuned :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP variables....

Post by hallsofvallhalla »

i didnt know you were using the tuts as an example. In my example I use it Planetary wars, where everything is instanced. So it doesn't really matter. You could just set a variable at the same time that makes a creature not fightable if it is set to true. Then make your creature query select * where that does not equal that variable. Then set a cron to delete half dead creatures everyday.

Eh so many ways to do it. I like your way it works.
Post Reply

Return to “Coding”