SESSION array...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

SESSION array...

Post by kevin7 »

i'm doing a shopping cart... and i'v discovered SESSION array...
can anyone of you show me a little code sample on how to use it?

tq... :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]session[/php_man] read the examples they give...
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

hmm... that dun help me a lot.. but thank...

how i declare a session array?

Code: Select all

<?php
...

$_SESSION['test'][];

$_SESSION['test'][1]="testing";

echo $_SESSION['test'][1];

...
?>
... is that will look like above?
tq..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_SESSION is, mostly, just like any other variable available in PHP. So you can basically use it as such..
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

it doesn't work... can u sure me a little example...?
sorry for disturbing... :oops:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

This is a simple example of using the $_SESSION array.
It should increment the counter every time you refresh the page.

Code: Select all

<?php
//set error_reporting to catch any errors
error_reporting(E_ALL);

//start the session
session_start();

//check if the session variable 'counter' has been set
//if not, set it to 0
if(empty($_SESSION['counter'])){
  $_SESSION['counter'] = 0;
}
//add 1 to the session variable 'counter' and echo it
echo ++$_SESSION['counter'];
?>
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

session array not like $_SESSION['name'][1] ?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

session array not like $_SESSION['name'][1] ?
Not sure what you mean ... $_SESSION is an array, yeah.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

This might be useful:

page1.php:

Code: Select all

<?php
session_start();
if (!isset($_SESSION['user']))
   $_SESSION['user'] = 'treinar';
echo '$_SESSION[''user''] = '.$_SESSION['user'];
echo '<br><br><a href="page2.php">Link to page 2</a>';
?>
page2.php:

Code: Select all

<?php
session_start();
echo '$_SESSION[''user''] = '.$_SESSION['user'];
echo '<br><br><a href="page3.php">Link to page 3</a>';
?>
page3.php:

Code: Select all

<?php
session_start();
echo 'You must login:<br><form action="page4.php" method="post">
User: <input type="text" name="user" value="'.$_SESSION['user'].'"><br>
Pass: <input type="password" name="pass"><br>
<input type="submit">
</form>';
?>
page4.php:

Code: Select all

<?php
session_start();
if ( $_POST['user'] == 'treinar' && $_POST['pass'] == 'pass' ) {
   $_SESSION['auth'] = true;
   $_SESSION['user'] = $_POST['user'];
   $_SESSION['data'] = range(1, 5);
   header("Location: page5.php");
}
else {
   $_SESSION['auth'] = false;
   $_SESSION['user'] = '';
   header("Location: page3.php");
}
?>
page5.php:

Code: Select all

<?php
session_start();
if (!$_SESSION['auth'])
   header("Location: page3.php");
echo 'Good, you logged in!';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
echo '<a href="page6.php">Logout</a>';
?>
page6.php:

Code: Select all

<?php
session_start();
session_destroy();
$_SESSION = array();
header("Location: page1.php");
?>
You can test this here: http://scorphus.no-ip.com/lab/session/

This is a part of what used to be a session tutorial by Jason. (could not find it :roll: is it lost :?: )

-- Scorphus
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Whee, found it in my docs archive! I printed it once in a time, see: http://scorphus.no-ip.com/lab/session/s ... torial.pdf

Regards,
Scorphus.
Post Reply