Jump to content

How do I echo a session variable?


Moron

Recommended Posts

[quote author=Nicklas link=topic=107125.msg429312#msg429312 date=1157558746]
you have to write:

[code=php:0]echo $_SESSION['empcode'];[/code]
[/quote]

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?
Link to comment
Share on other sites

[quote author=tleisher link=topic=107125.msg429319#msg429319 date=1157559062]
> 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.
[/quote]

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.
???
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=ober link=topic=107125.msg429329#msg429329 date=1157559486]
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.
[/quote]

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);
Link to comment
Share on other sites

[quote author=ober link=topic=107125.msg429329#msg429329 date=1157559486]
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.
[/quote]

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

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

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

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

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

Link to comment
Share on other sites

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

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

Also, when setting your session variable... session_register

[code]               
session_register('empcode');
$_SESSION['empcode'] = $empcode;[/code]
Link to comment
Share on other sites

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

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

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

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
Link to comment
Share on other sites

[quote author=quillspirit link=topic=107125.msg429338#msg429338 date=1157560601]
Have you checked that your form is actually posting employeenumber?
[/quote]

On the previous page, I can echo $empcode; and it does indeed echo the proper employee number.
Link to comment
Share on other sites

[quote author=Wintergreen link=topic=107125.msg429340#msg429340 date=1157560846]
For this to work, on the first page you'd need

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

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

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
[/quote]

Works!

Many thanks!

:)

[b]EDIT:[/b] 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.
???
Link to comment
Share on other sites

[quote author=thorpe link=topic=107125.msg429347#msg429347 date=1157561716]
[quote]If I put echo $_SESSION['empcode']; further down the page, it does nothing.[/quote]

Post your code.
[/quote]

[quote]$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>";

[b]echo $_SESSION['empcode'];[/b]

?>[/quote]

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....

:)
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

[quote author=Wintergreen link=topic=107125.msg429356#msg429356 date=1157562200]
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
[/quote]

This is at the top of the page:

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

This is at the top of the page before:

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

The page before also has....

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

Trust me, you DO NOT want to dig through the page before in its entirety! ;D
Link to comment
Share on other sites

[quote author=kenrbnsn link=topic=107125.msg429361#msg429361 date=1157562629]
When you post your code here please surround it with [nobbc][code][/code][/nobbc] tags.

Ken
[/quote]

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

Link to comment
Share on other sites

[quote author=Crayon Violent link=topic=107125.msg429357#msg429357 date=1157562212]
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)[/quote]

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

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

No, it's not part of a function. 

[quote]c) is it spelled right?
[/quote]

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

[quote author=Moron link=topic=107125.msg429366#msg429366 date=1157563057]
It'll probably turn out that I've just done something boneheaded and it'll eventually dawn on me.
[/quote]

I hate to quote myself, but this was a self-fulfilling prophecy; I had declared the $empcode variable a second time.

Duh.

Thanks, all!

Link to comment
Share on other sites

well if it's none of the above, then i'm out of ideas. Other than that, I have no idea why it would echo just fine one time, and then not echo later on in the script. As suggested, you're going to have to post the entire script that has the offending code. No need to post the 1st script that originally creates the session variable; if it's echoing the first time, then it appears to be passing properly.

edit: you posted same time as me. okay good, cheers!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.