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
kevin7
Forum Commoner
Posts: 96 Joined: Fri May 21, 2004 6:54 am
Post
by kevin7 » Wed Jun 30, 2004 5:36 am
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...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 30, 2004 5:40 am
[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 » Wed Jun 30, 2004 5:45 am
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..
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 30, 2004 5:50 am
$_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 » Wed Jun 30, 2004 8:27 am
it doesn't work... can u sure me a little example...?
sorry for disturbing...
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Jun 30, 2004 8:44 am
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 » Wed Jun 30, 2004 9:03 am
session array not like $_SESSION['name'][1] ?
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Jun 30, 2004 9:12 am
session array not like $_SESSION['name'][1] ?
Not sure what you mean ... $_SESSION is an array, yeah.
scorphus
Forum Regular
Posts: 589 Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:
Post
by scorphus » Wed Jun 30, 2004 10:28 am
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
is it lost
)
-- Scorphus