Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

add parameters to session on different pages

   
6:04 pm on Jan 12, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,
I have been struggling with sessions and arrays for days.
I ever used arrays so donīt like them (understand them)

I need to start a session with some parameters in the session then go to another page using header and add some vars to the same session, then the same again, another page and add a new parameter.

However I wonder if it is really necesarry to do an array add the vars to the array.
Canīt I just do without an array:


page 1:
$_SESSION['1']='1';
$_SESSION['2']='2';
$_SESSION['3']='3';
page2:
$_SESSION['4']='4';
$_SESSION['5']='5';
$_SESSION['6']=6';
page3:
$_SESSION['7']='7';


Is it wrong to add vars like this without any array?
Thanks,
1:44 am on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I think you've 2 aspects to your question:
One is about arrays
The other is about sessions

Since arrays are used even in your example, let's start with that as it's more basic and needs to be understood before you can build on it further.

In PHP arrays are a way of storing key/value pairs. That's it.

The key kan be a string or a numeric, it even can be omitted (but a numeric key will be assigned automatically if omitted).
The value can be almost anything, including another array (allows you to create multidimensional arrays, trees, etc.

So
$a=array( 'foo', 'bar');

Creates an array
and
$a=array( 0=>'foo', 1=> 'bar'); 

$a=array( 0=>'foo', 'bar');

$a=array( 'foo', 1=>'bar');

$a[0]='foo';
$a[1]='bar'];

all create the same array
Since php 5.4 you can also use
$a=['foo','bar']; 

Again the same result:
and array with key 0 (numeric) having value foo (a stting)
and key 1 being 'bar'.

The key does not have to be sequential, nor does it have to be numeric (integer actually).
You can also use a string to create an associative array e.g.:

$b=array('wall'=>'green', 'floor'=>'red', 'ceiling'=>'orange');


To use an array you can either lookup specific keys

e.g.:
$c=$b['floor'];


or you can walk over all key/value pairs in an array:

foreach ($b as $key => $value) {
print('The '.$key.' is '.$value'."\n");
}

(you can also just walk over all values, ignoring the keys)

You don't have to assign an array in one go, you can also add values to it
e.g.
$b['table']='purple';


Again, you can omit the key and it'll assign a numeric one for you on the fly
$a[]='foobar';


You can also ceate an empty array with
$emtpy=array();


Essentially that's it.

To see how an array is structured when you have arrays that hold other arrays as values, use print_r: it shows it nicely.

Multi dimensional arrays are nothing more than arrays of arrays
e.g,:

$matrix=array (
0 => array (
'a' => 'row 0, col a',
'b' => 'row 0, col b'
),
1 => array (
'a' => 'row 1, col a',
'b' => 'row 1, col b'
)
)


Which you can then retrieve values from as $matrix[0]['a']

Reference:
[php.net...]
7:22 am on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you've 2 aspects to your question:
One is about arrays
The other is about sessions

Since arrays are used even in your example, let's start with that as it's more basic and needs to be understood before you can build on it further.


Thanks for the explanation.
I already did one, however did not manage to get it working well.
However I wonder if it is really necesarry to add into an array other than the session array.
I started first with just declaring the session vars on diferent pages and it worked however was told its not god to keep adding vars as can give problems, so I started to try to declare them in an array(). But as I said I keep wondering if really necesarry. Like I said before, would like to do this:
page 1
$_SESSION['id']=$id;
$_SESSION['whatever']='$whatever';
etc.
page2:
$_SESSION['x4']='$x4';
etc
page3:
$_SESSION['name']='$name';
8:34 am on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Do you mean all those examples literally?

blahblah['something']=$something
blahblah['something-else']=$something-else

with the same string value on each side? Or is that just how the examples came out?
9:05 am on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean all those examples literally?

blahblah['something']=$something
blahblah['something-else']=$something-else

with the same string value on each side?


Yes I do, as it is about picking up the value in forms
and phpscripts.
I already tried but ever finished storing the vars like this:
$_SESSION['id']=$id;
11:58 am on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Don't access $_SESSION['id'] directly, use session_start() session_id() instead.
12:05 pm on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't access $_SESSION['id'] directly, use session_start() session_id() instead.


sorry didnt mention that,
I have session_start() on top of all pages.
The question was about declaring the vars as there are many vars to be declared on different pages.
and I need to access many vars.
2:40 pm on Jan 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not sure if it is bad or not to add vars in diferent pages to an existing session.
I am considering doing this:
page 1:
 session_start();
$name = "notsetyet";
$whatever = "notsetyet"
$_SESSION['id'] = $id;
$_SESSION['name'] = $name;
$_SESSION['whatever'] = $whatever;
$_SESSION['etc'] = $etc;

Then on next pages where the vars with value "notsetyet" get a value, the valuea are updated to its real value.

I dont get it, it its true whats said that adding vars to existing session is bad, I suppose changing the value of declared var session is not bad, if true?
Please bring light to this.
More that I read, more confused am I.
Thanks