1

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.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
learning_curve
  • 53
  • 2
  • 12
  • 2
    You need to add `session_start()` to the beginning of each page. Also see what @Kishin says about the extra brackets. – Jay Blanchard Dec 16 '14 at 17:52
  • 3
    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. – JKaan Dec 16 '14 at 17:53
  • Thanks! I added the session_start at the top and removed the extra brackets (I thought I needed them from the research I was doing.) That got me part of the way. echo "

    Account Owner or Contact Name:" .$_SESSION["contact_name"]. ",

    "; now shows up on the page as: Account Owner or Contact Name:, reflecting just the html and not the variable.
    – learning_curve Dec 16 '14 at 18:09
  • Okay I got it to work by taking out the session_start(). From the answers posted above, that makes no sense... I am glad it is working, but would like to know why. – learning_curve Dec 16 '14 at 18:14

2 Answers2

1

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

O. Jones
  • 103,626
  • 17
  • 118
  • 172
0

session_start — Start new or resume existing session.

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

Ajit Kumar
  • 345
  • 3
  • 9