Jump to content


Photo

How do I echo a session variable?


  • Please log in to reply
21 replies to this topic

#1 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:05 PM

Okay, I'm getting there bit by bit.

At the top of my new page, I have:

<?php
session_start();
$_SESSION['empcode'];
?>

So why can't I echo $empcode? It doesn't give an error, but it doesn't echo anything.


#2 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 06 September 2006 - 04:05 PM

you have to write:

echo $_SESSION['empcode'];


#3 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 06 September 2006 - 04:11 PM

> So why can't I echo $empcode? It doesn't give an error, but it doesn't echo anything.

Because you didn't create that.

$empcode = $_SESSION["empcode"];

That would create the variable as a session.

#4 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:12 PM

you have to write:

echo $_SESSION['empcode'];


I tried that, but it doesn't echo anything. I wonder if maybe $empcode isn't getting passed across?

I have a form on the previous page that points to the new page as the form action. $empcode isn't anywhere in the form, but it shouldn't need to be if it's in the session, right?


#5 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:15 PM

> So why can't I echo $empcode? It doesn't give an error, but it doesn't echo anything.

Because you didn't create that.

$empcode = $_SESSION["empcode"];

That would create the variable as a session.


I tried this. No errors, but it doesn't echo anything. I'm thinking that maybe the $empcode variable isn't being properly passed from the previous page with the $_SESSION code.
???

#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 06 September 2006 - 04:18 PM

I assume you set $_SESSION['empcode'] on another page?

$_SESSION['empcode'] = "blah";

That should be on a previous page.  Also, make sure you're not using the "back" button in your browser to go back and reset the value.

You can also try print_r($_SESSION) to look at what is in the session array.

Info: PHP Manual


#7 quillspirit

quillspirit
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationOregon, USA

Posted 06 September 2006 - 04:27 PM

I assume you set $_SESSION['empcode'] on another page?

$_SESSION['empcode'] = "blah";

That should be on a previous page.  Also, make sure you're not using the "back" button in your browser to go back and reset the value.

You can also try print_r($_SESSION) to look at what is in the session array.


Thanks for that... I've been trying to figure out how to see what is in the session array. You gave me a parse error though... don't forget the blessed ; !!

print_r($_SESSION);
Peace,
Shawn
Simple PHP

#8 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:31 PM

I assume you set $_SESSION['empcode'] on another page?

$_SESSION['empcode'] = "blah";

That should be on a previous page.  Also, make sure you're not using the "back" button in your browser to go back and reset the value.

You can also try print_r($_SESSION) to look at what is in the session array.


Yes, on the previous page I have at the top:

<?php
session_start();
$_SESSION['empcode'];
?>

Further down the page I have....
$empcode = $_POST['employeenumber'];
$_SESSION['empcode']=$empcode;

But when I echo $empcode on the new page, it does nada.

When I tried print_r($_SESSION), I got "Array ( [empcode] => )"



#9 quillspirit

quillspirit
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationOregon, USA

Posted 06 September 2006 - 04:36 PM

Have you checked that your form is actually posting employeenumber?
On that previous page, after the form is submitted, try something like this...
if (isset($empcode))
{
echo "Employee: $empcode";
} else { echo "Null"; }

If it returns as Null, then your problem is in the form.

Also, when setting your session variable... session_register

                
session_register('empcode');
$_SESSION['empcode'] = $empcode;

Peace,
Shawn
Simple PHP

#10 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 06 September 2006 - 04:40 PM

For this to work, on the first page you'd need

<?
  session_start();
  $_SESSION['empcode'] = $_POST['empcode'];
?>

And to access it on another page you'd need
<?
  session_start();
  echo $_SESSION['empcode'];
?>

Any time you want to access the session variable you have to use $_SESSION['empcode'].  Sessions allow you to send variables across pages, but you can't just access the $empcode from the previous page, you have to create a $_SESSION variable

#11 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:43 PM

Have you checked that your form is actually posting employeenumber?


On the previous page, I can echo $empcode; and it does indeed echo the proper employee number.


#12 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:48 PM

For this to work, on the first page you'd need

<?
  session_start();
  $_SESSION['empcode'] = $_POST['empcode'];
?>

And to access it on another page you'd need
<?
  session_start();
  echo $_SESSION['empcode'];
?>

Any time you want to access the session variable you have to use $_SESSION['empcode'].  Sessions allow you to send variables across pages, but you can't just access the $empcode from the previous page, you have to create a $_SESSION variable


Works!

Many thanks!

:)

EDIT: Well....it works at the top of the page, but that isn't where I need to echo it. If I put echo $_SESSION['empcode']; further down the page, it does nothing.
???

#13 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 06 September 2006 - 04:55 PM

If I put echo $_SESSION['empcode']; further down the page, it does nothing.


Post your code.

#14 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 04:58 PM

If I put echo $_SESSION['empcode']; further down the page, it does nothing.


Post your code.


$firstdate = $_POST['firstinput'];
$seconddate = $_POST['secondinput'];
$firsttime = $_POST['firsttimeinput'];
$secondtime = $_POST['secondtimeinput'];

echo "<CENTER>";

echo "<font size=2 color=#000000 face=arial>You have requested leave starting on ";

echo "<b>";

echo $firstdate;

echo "</b>";

echo " at ";

echo "<b>";

echo $firsttime;

echo "</b>";

echo " and ending on ";

echo "<b>";

echo $seconddate;

echo "</b>";

echo " at ";

echo "<b>";

echo $secondtime;

echo "</b>";

echo ".";

echo "</font>";

echo "</CENTER>";

echo $_SESSION['empcode'];

?>


Note the line I bolded. I just dropped it here as a test. It'll echo if done at the top with the $_SESSION command, but not further down the page.

EDIT: I didn't realize that this board also interprets HTML tags. Anyway....

:)


#15 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 06 September 2006 - 05:03 PM

You need to post your code from all your pages you're trying to use.  From the stuff you posted there, you haven't started a session

#16 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 September 2006 - 05:03 PM

it works on one part of the page, but not another?

possible reasons:

a) your 2nd echo is inside a condition that is not being met. check your logic, also check to make sure any variables being used in the condition are holding what you expect them to be holding (echo the vars before the condition and see if it is echoing out the expected data)

b) is it inside a function? it won't work inside a function unless you declare it as a global variable inside the function. 

c) is it spelled right?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#17 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 05:06 PM

You need to post your code from all your pages you're trying to use.  From the stuff you posted there, you haven't started a session


This is at the top of the page:

<?php
session_start();
$_SESSION['empcode'];
?>

This is at the top of the page before:

<?php
session_start();
$empcode = $_POST['empcode'];
$_SESSION['empcode'] = $empcode;
?>

The page before also has....

$empcode = $_POST['employeenumber'];
$_SESSION['empcode']=$empcode;

Trust me, you DO NOT want to dig through the page before in its entirety! ;D


#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 September 2006 - 05:10 PM

When you post your code here please surround it with [code][/code] tags.

Ken

#19 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 05:14 PM

When you post your code here please surround it with [code][/code] tags.

Ken


Sorry about that. I used quote because I didn't know how to embolden something inside "code" tags. Is there a way to do that?



#20 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 06 September 2006 - 05:17 PM

it works on one part of the page, but not another?

possible reasons:

a) your 2nd echo is inside a condition that is not being met. check your logic, also check to make sure any variables being used in the condition are holding what you expect them to be holding (echo the vars before the condition and see if it is echoing out the expected data)


No, there is no "if" statement or anything conditional. It's just a straightforward echo.

b) is it inside a function? it won't work inside a function unless you declare it as a global variable inside the function.


No, it's not part of a function. 

c) is it spelled right?


Good ideas, though. It'll probably turn out that I've just done something boneheaded and it'll eventually dawn on me.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users