Page 1 of 1

SESSION array...

Posted: Wed Jun 30, 2004 5:36 am
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... :)

Posted: Wed Jun 30, 2004 5:40 am
by feyd
[php_man]session[/php_man] read the examples they give...

Posted: Wed Jun 30, 2004 5:45 am
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..

Posted: Wed Jun 30, 2004 5:50 am
by feyd
$_SESSION is, mostly, just like any other variable available in PHP. So you can basically use it as such..

Posted: Wed Jun 30, 2004 8:27 am
by kevin7
it doesn't work... can u sure me a little example...?
sorry for disturbing... :oops:

Posted: Wed Jun 30, 2004 8:44 am
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'];
?>

Posted: Wed Jun 30, 2004 9:03 am
by kevin7
session array not like $_SESSION['name'][1] ?

Posted: Wed Jun 30, 2004 9:12 am
by markl999
session array not like $_SESSION['name'][1] ?
Not sure what you mean ... $_SESSION is an array, yeah.

Posted: Wed Jun 30, 2004 10:28 am
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

Posted: Wed Jun 30, 2004 10:36 am
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.