Pulling two single variables from a Session Array

I have a session that is created and stored. On another page, I want to pull two variables from this session. One of the variables I want to echo as a welcome and the other I want to use in a query to update a mysql table. I just can't quite seem to figure out the correct wording as I have really not worked with sessions and someone else created the initial code. The session name is what is really giving me the trouble.

Here is the code for naming the session:

$directory = "http://$serverName/".$journalName['db'];
mysql_connect('server','user','pass');
mysql_select_db($journalName['db']);

ini_set('session.use_trans_sid', false);
ini_set('session.use_only_cookies', true);

session_name($journalName['db']);
session_set_cookie_params(6*60*60); // 6 hrs until login cookie expires
session_start();

It has the possibility of three different session names. If I do a print of the array (print_r($_SESSION);) here is what shows up:

Array ( [loggedin] => 1 [person] => 1 [account_id] => 1 [order_id] => [type] => individual [unlimited] => yes [status] => active [agreeterms] => no [bounced] => no [subscription_begin] => 0000-00-00 [subscription_end] => 0000-00-00 [contact_name] => Doe, Jane [email] => jane.doe@gmail.com [email_cc] => [institution] => IRA [net_addresses] => [department] => CELT [phone] => 123-456-9383 [address] => 303 S. Patterson Ave [city] => Butte [state] => ID [zip] => 12345 [country] => United States [tech_contact] => [tc_phone] => [tc_email] => [last_login] => 2014-12-15 13:20:24 [last_access] => 2012-03-28 13:45:23 [administrator] => yes [notes] => [last_reset] => 2011-11-19 13:36:59 [last_notified] => 2007-08-14 [combo] => no )

I want to pull the variable contact_name for display at the top of the page and I want to pull the variable email and use it to run and update query. Here is what I currently have to pull the contact_name:

<?php echo "<h2>Account Owner or Contact Name:" .$_SESSION[["contact_name"]]. ",</h2>"; ?>

It is not working at all. Sessions really have me flumoxed at the moment.


Is it possible that you just made a typo ? It seems $_SESSION[["contact_name"]] should be $_SESSION["contact_name"]. Without the extra pair of [] brackets.

And also add

 session_start();

to the page, because if you don't the session will not be available


session_start — Start new or resume existing session.

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

链接地址: http://www.djcxy.com/p/59734.html

上一篇: 会话变量(种类)不加载

下一篇: 从会话数组中提取两个单变量