Jump to content

Archived

This topic is now archived and is closed to further replies.

Drezard

Does PHP allow you to put 2 variables in a session?

Recommended Posts

Hello, I want to store 2 variables in one session at the same time.

How would i do this (Code examples please)

- Cheers, Daniel

Share this post


Link to post
Share on other sites
this is what I would put

<?php
session_start();
$firstname="Daniel";
$lastname="Cheers";
$_SESSION['username'] = $lastname.', '.$firstname;
?>

Share this post


Link to post
Share on other sites

<?php

echo $_SESSION['username'];

?>

the session must be started on the beginning of the page prior to using any session variables.

Share this post


Link to post
Share on other sites
like what happens if i want to pass 2 variables from one page to another. So
$_SESSION['user'] = $user. ',' .$pass;
and then on the next page.
$user = $_SESSION['userinfo']
$pass = $_SESSION['userinfo']

Thanks, Daniel

Share this post


Link to post
Share on other sites
first page would be
[code]$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
[/code]
second page would be
[code]$user = $_SESSION['user'];
$password = $_SESSION['password'];
[/code]

$_SESSION is an array, whatever vars you wish to cary page to page in your program can be put in this array as elements, you refer to these elements by the index you give them. ie, "user" is an element in the $_SESSION array that we set above, when you wish to refer to it you will use $_SESSION["user"], "user" being the index.

Share this post


Link to post
Share on other sites
Prehaps Drezard mean two values in one SESSION variable rather than having two session variables. So prehaps Drezard wants something like this:
[code=php:0]session_start();

$username = 'someuser';
$password = 'somepass';

// NOw we create 1 session var to store the username and password:
$_SESSION['userinfo']['username'] = $username;
$_SESSION['userinfo']['password'] = $password;[/code]

The above code creates 1 session variable ($_SESSION['userinfo'] which is an array) and holds two values the username and password.

To retieve the username and password use this:
$_SESSION['userinfo']['username'] - to grab the username
$_SESSION['userinfo']['password'] - to grab the password

Is that what you want?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.