Staredit Network

Staredit Network -> Computers and Technical -> PHP Help
Report, edit, etc...Posted by BeeR_KeG on 2006-04-17 at 15:07:32
Alright, I've come to the point where I need to save 3 variables, and possibly 5 in the future and the $_SESSIONS["name"] seems to only save 1 variable and not all 3, even if I make a $_SESSIONS["name"] for each variable. I've been reading about saving the variables to a sessions directory using session_save_path ( [string path] ) and I'm thinking of just including that sessions directory into every file that needs those variables.

How would I go to saving and storing the data in that sessions directory?
Report, edit, etc...Posted by Centreri on 2006-04-17 at 16:35:01
Don't know a lot about sessions, just the basics. Can't help ya' there.
Report, edit, etc...Posted by (SEN)Dante50 on 2006-04-17 at 16:38:46
I'll copy and paste all of that code and I'll see if it can run on my PHP tester.
Report, edit, etc...Posted by Shmeeps on 2006-04-17 at 18:03:48
Just use like, $_SESSION['name'], $_SESSION['money'], $_SESSION['someothervariable'].

All session variables are local, so you don't need to worry about that. As long as you know what things in the array you are using, you should be fine.

You could also use serialize and unserialize to store them in one variable, but that's kind of an extreme fix to an easierly solved problem.
Report, edit, etc...Posted by BeeR_KeG on 2006-04-17 at 20:03:43
I've been working around trying to fix it and I used multiple $_SESSION["variable"] but it only stored one session variable. Then I used session_register("variable"); and it too, only stored one variable, but a different variable. Here's the code if anyone wants to help out. take not that I've removed code that has absolutely nothing to do.

data.php
CODE
<?php
session_start();
srand((double)microtime() * 1000000);
$minerals_1 = rand(0, 2500);
$_SESSION['minerals_1'] = $minerals_1;
?>


index.php
CODE
<?php
session_start();
echo <<<HTML
<form action="form.php" method="POST">
Enter the amount of minerals you wish to bet: <input type="text" name="minbet" /><br />
<input type="submit" />
</form>
HTML;
require_once ("data.php");
echo "You currently have {$minerals_1} minerals <br />";
?>


form.php
CODE
<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE);
$minerals_1 = $_SESSION['minerals_1'];
if ($minerals_1 < $_POST["minbet"])
{
   echo <<<HTML
   <meta http-equiv="refresh" content="1; URL=error.php">
HTML;
}
elseif ($minerals_1 >= $_POST["minbet"])
{
$minerals_2 = $minerals_1 - $_POST["minbet"];
$minbet = $minerals_1 - $minerals_2;
   echo "You have selected {$minbet} minerals to bet with.<br /> You currently have {$minerals_2} after placing your bet.<br />";
   include ("dice_func.php");
}
?>


dice_func.php
CODE
<?php
session_start();
$minerals_1 = $_SESSION['minerals_1'];
$minerals_2 = $_SESSION['minerals_2'];
$minbet = $_SESSION['minbet'];
echo "{$minerals_1} = minerals_1<br />";
echo "{$minerals_2} = minerals_2<br />";
echo "{$minbet} = minbet<br />";
srand((double)microtime() * 1000000);
$diceroll_1 = rand(1, 6);
switch ($diceroll_1)
{
case 1:
echo "You've rolled a 1, you've lost {$minbet} minerals";
break;
case 2:
echo "You've rolled a 2, you've lost {$minbet} minerals";
break;
}
?>
Report, edit, etc...Posted by Shmeeps on 2006-04-17 at 20:34:18
In Index.php add somewhere
CODE
$minerals_1 = $_SESSION['mierals_1'];


Edit: Wait, wrong thinking there. wink.gif

You never registered the session variables in form.php, add
CODE
$_SESSION['minerals_2'] = $minerals_2;

and
CODE
$_SESSION['minbet'] = $minbet;


Also, what's with the random srand?
Report, edit, etc...Posted by BeeR_KeG on 2006-04-17 at 20:47:32
Thanks, even though you really didn't specify the actual problem that I had, i realized it. As I said before, I had put the $_SESSIONS["variable"], in every file for every variable. The problem was that I put that code before the actual variable was defined, so it came out as Null. Trial and Error FTW!

About random srand. I also don't know why I've got it in there, Yoshi da Sniper told me to use it, instead of my previous mt_rand(). He explained it to me, but I didn't really understand.
Report, edit, etc...Posted by Shmeeps on 2006-04-18 at 18:07:30
It's a seed random maker. It uses usually microtime() to help make a more random number, kinda weird but generally better to use.
Report, edit, etc...Posted by Doodle77(MM) on 2006-04-18 at 20:45:47
mt_rand uses a better random number generating algorithm I think.
Report, edit, etc...Posted by Pie_Sniper on 2006-04-18 at 22:55:36
mt probably stands for Mersenne Twister, which is one of the best pseudorandom number generators around.
Next Page (2)