Ways to assign a value to a session variable.

I am wandering which way to assign a value to a session variable
exist. Which of the following examples will work.

Example #1:
session_start();
session_register("ex");
$ex = 2.0;

Example #2:
In first.php:
session_start();
session_register("ex");
In second.php:
$ex = 2.0;

Example #3:
In first.php:
session_start();
session_register("ex");
Load second.php?ex=2.0

Example #4:
In first.php:
session_start();
session_register("ex");
Go to the second.php via submission of the form on the first.php,
which contains form-variable $ex.

Example #4 seams not to work? Is it expectable?
kurdayon [ Mo, 14 Januar 2008 02:24 ] [ ID #1907459 ]

Re: Ways to assign a value to a session variable.

Sorry, I made a mistake. In all 4 cases the second.php should contain
session_start();
kurdayon [ Mo, 14 Januar 2008 02:25 ] [ ID #1907460 ]

Re: Ways to assign a value to a session variable.

unless u r using an older version, otherwise avoid session_register
Peter Pei [ Mo, 14 Januar 2008 02:43 ] [ ID #1907462 ]

Re: Ways to assign a value to a session variable.

"Kurda Yon" <kurdayon [at] yahoo.com> wrote in message
news:420f8581-3870-4908-bb70-61593fbcac8b [at] f47g2000hsd.google groups.com...
> Sorry, I made a mistake. In all 4 cases the second.php should contain
> session_start();
>
Peter Pei [ Mo, 14 Januar 2008 02:43 ] [ ID #1907463 ]

Re: Ways to assign a value to a session variable.

that's a given
Peter Pei [ Mo, 14 Januar 2008 02:44 ] [ ID #1907464 ]

Re: Ways to assign a value to a session variable.

4 works if you did things right. cannot go more detailed than that unless
see some code.
Peter Pei [ Mo, 14 Januar 2008 02:46 ] [ ID #1907466 ]

Re: Ways to assign a value to a session variable.

On Jan 13, 8:43 pm, "Peter Pei" <yan... [at] telus.com> wrote:
> unless u r using an older version, otherwise avoid session_register

The example #4 seems not to work. In the second file the session
variable do not want to take the value of the form variable. I tried
to force session to do it by the explicit command: $_SESSION["ex"] =
$ex; But it also does not help. I think that session value of the
variable has a higher priority with respect to the from variable. In
the first file I set the value to the session variable $ex, and than I
submit a form which change the value of the $ex variable, but the
second file does not want to see the values of the form variables. It
sees the values of the session variables.
kurdayon [ Mo, 14 Januar 2008 02:52 ] [ ID #1907470 ]

Re: Ways to assign a value to a session variable.

On Mon, 14 Jan 2008 02:24:16 +0100, Kurda Yon <kurdayon [at] yahoo.com> wrote:

> I am wandering which way to assign a value to a session variable
> exist. Which of the following examples will work.

Don't use session_register() anymore. Use a reference if you must.
--
Rik Wasmus
luiheidsgoeroe [ Mo, 14 Januar 2008 02:53 ] [ ID #1907471 ]

Re: Ways to assign a value to a session variable.

Kurda Yon wrote:
> I am wandering which way to assign a value to a session variable
> exist. Which of the following examples will work.
>
> Example #1:
> session_start();
> session_register("ex");
> $ex = 2.0;
>
> Example #2:
> In first.php:
> session_start();
> session_register("ex");
> In second.php:
> $ex = 2.0;
>
> Example #3:
> In first.php:
> session_start();
> session_register("ex");
> Load second.php?ex=2.0
>
> Example #4:
> In first.php:
> session_start();
> session_register("ex");
> Go to the second.php via submission of the form on the first.php,
> which contains form-variable $ex.
>
> Example #4 seams not to work? Is it expectable?
>

session_register() has been deprecated. The correct way to do it is to
use the $_SESSION superglobal array, i.e.

session_start();
$_SESSION['ex'] = 2.0;

session_start();
$ex = $_SESSION['ex'];

BTW - there is no direct relationship between 'ex' as an index value,
and $ex as a variable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Mo, 14 Januar 2008 02:53 ] [ ID #1907472 ]

Re: Ways to assign a value to a session variable.

there is a relationship if register_global is on, this is at least true for
5.2.5 on vista
Peter Pei [ Mo, 14 Januar 2008 02:56 ] [ ID #1907473 ]

Re: Ways to assign a value to a session variable.

On Jan 13, 8:46 pm, "Peter Pei" <yan... [at] telus.com> wrote:
> 4 works if you did things right. cannot go more detailed than that unless
> see some code.

OK. In the first.php I have:
session_start();
session_register("ex");
$ex = 2.0;
..........
<form enctype="multipart/form-data" action="second.php" method="post">
......
print "<input type='text' name='ex' value=\"$ex\">\n";
......
</form>

When I submit the form I put in the text field corresponding to $ex
3.0.

In the second.php I have:
session_start();
$_SESSION["ex"] = $ex;
print "===> $txt_l_1";
print "---> {$_SESSION["txt_l_1"]}";
die();

As the output I have:
===> 2.0
---> 2.0

Not 3.0, as I expect.
kurdayon [ Mo, 14 Januar 2008 02:59 ] [ ID #1907474 ]

Re: Ways to assign a value to a session variable.

On Mon, 14 Jan 2008 02:59:39 +0100, Kurda Yon <kurdayon [at] yahoo.com> wrote=
:
> On Jan 13, 8:46 pm, "Peter Pei" <yan... [at] telus.com> wrote:
>> 4 works if you did things right. cannot go more detailed than that =

>> unless
>> see some code.
>
> OK. In the first.php I have:
> session_start();
> session_register("ex");

$_SESSION['ex'] =3D 2.0;
$ex =3D &$_SESSION['ex'];

> .........
> <form enctype=3D"multipart/form-data" action=3D"second.php" method=3D"=
post">
> .....
> print "<input type=3D'text' name=3D'ex' value=3D\"$ex\">\n";
> .....
> </form>
>
> When I submit the form I put in the text field corresponding to $ex
> 3.0.
>
> In the second.php I have:
> session_start();
> $_SESSION["ex"] =3D $ex;

Why overwrite the value in the $_SESSION array here? If you're not even =
=

using it, why store it to begin with?

> print "=3D=3D=3D> $txt_l_1";
> print "---> {$_SESSION["txt_l_1"]}";


Euhm, what magic do you expect from a in this sample code uninitiated & =
=

unassigned variable $txt_1_1?
-- =

Rik Wasmus
luiheidsgoeroe [ Mo, 14 Januar 2008 03:03 ] [ ID #1907475 ]

Re: Ways to assign a value to a session variable.

this is because of the order globals are assigned. u mapped the global
twice, once from _SESSION, once from _POST. don't duplicate the indexes - it
only confuses u and cause nasty bugs.

It's more php's fault than yrs - global, and bad global.
Peter Pei [ Mo, 14 Januar 2008 03:05 ] [ ID #1907476 ]

Re: Ways to assign a value to a session variable.

> > OK. In the first.php I have:
> > session_start();
> > session_register("ex");
>
> $_SESSION['ex'] = 2.0;
> $ex = &$_SESSION['ex'];
What for do you introduce $ex variable? What for do you have "&" in
the second line?

>
> > .........
> > <form enctype="multipart/form-data" action="second.php" method="post">
> > .....
> > print "<input type='text' name='ex' value=\"$ex\">\n";
> > .....
> > </form>
>
> > When I submit the form I put in the text field corresponding to $ex
> > 3.0.
>
> > In the second.php I have:
> > session_start();
> > $_SESSION["ex"] = $ex;
>
> Why overwrite the value in the $_SESSION array here? If you're not even
> using it, why store it to begin with?
>
$ex supposed to be a form variable and before to use "header" I would
like to put the values of the form variables into the session
variables.

> > print "===> $txt_l_1";
> > print "---> {$_SESSION["txt_l_1"]}";
>
> Euhm, what magic do you expect from a in this sample code uninitiated &
> unassigned variable $txt_1_1?
Instead of $txt_l_1 should be $ex.
kurdayon [ Mo, 14 Januar 2008 03:09 ] [ ID #1907478 ]
PHP » comp.lang.php » Ways to assign a value to a session variable.

Vorheriges Thema: Two Submit buttons on one form
Nächstes Thema: PHP mail marked is phishing