Moron Posted September 6, 2006 Share Posted September 6, 2006 Okay, I'm getting there bit by bit.At the top of my new page, I have:[code]<?phpsession_start();$_SESSION['empcode'];?>[/code]So why can't I echo $empcode? It doesn't give an error, but it doesn't echo anything. Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/ Share on other sites More sharing options...
Nicklas Posted September 6, 2006 Share Posted September 6, 2006 you have to write:[code=php:0]echo $_SESSION['empcode'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87203 Share on other sites More sharing options...
tleisher Posted September 6, 2006 Share Posted September 6, 2006 > 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 Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87210 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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? Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87212 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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. ??? Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87215 Share on other sites More sharing options...
ober Posted September 6, 2006 Share Posted September 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87219 Share on other sites More sharing options...
quillspirit Posted September 6, 2006 Share Posted September 6, 2006 [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); Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87224 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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]<?phpsession_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] => )" Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87225 Share on other sites More sharing options...
quillspirit Posted September 6, 2006 Share Posted September 6, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87228 Share on other sites More sharing options...
Wintergreen Posted September 6, 2006 Share Posted September 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87230 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87231 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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. ??? Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87232 Share on other sites More sharing options...
trq Posted September 6, 2006 Share Posted September 6, 2006 [quote]If I put echo $_SESSION['empcode']; further down the page, it does nothing.[/quote]Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87237 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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.... :) Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87241 Share on other sites More sharing options...
Wintergreen Posted September 6, 2006 Share Posted September 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87246 Share on other sites More sharing options...
.josh Posted September 6, 2006 Share Posted September 6, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87247 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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]<?phpsession_start();$_SESSION['empcode'];?>[/code]This is at the top of the page before:[code]<?phpsession_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 Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87248 Share on other sites More sharing options...
kenrbnsn Posted September 6, 2006 Share Posted September 6, 2006 When you post your code here please surround it with [nobbc][code][/code][/nobbc] tags.Ken Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87251 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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? Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87254 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87256 Share on other sites More sharing options...
Moron Posted September 6, 2006 Author Share Posted September 6, 2006 [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! Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87260 Share on other sites More sharing options...
.josh Posted September 6, 2006 Share Posted September 6, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/19920-how-do-i-echo-a-session-variable/#findComment-87263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.