PHP syntax to output SESSION variable

I have a page whose SQL creates a user key then creates a SESSION variable and passes variable to the login_success.php file where I want to echo the value of the SESSION variable. However, I have not been able to successfully echo that value on the login_success page. Instead the page merely displays “Login was successful. The user key is .”

The code on my first page, beginning at the echo statement below which does echo the correct value of  $userkey is:
echo $userkey[0];
      
// Register $userkey and redirect to login_success.php
$_SESSION['userkey'] = ".$userkey[0].";

header("location:form03success.php");

The code on my login_success.php file is:
// Check if session is not registered , redirect back to main page.
session_start();
if(!session_is_registered(userkey))
      {
      header("location:form03.php");
      }
echo "Login was successful. The user key is ".$_SESSION['userkey'];

Can someone tell me where I am making the error(s)?
Thanks.
lepirtleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
I know this is a stupid question, but have you session_start() in the file where $userkey is registered?

session_start();
$_SESSION['userkey'] = ".$userkey[0].";

header("location:form03success.php");

Anyway, I suggest to put a print_r($_SESSION) in form03success.php so you can see what is in $_SESSION array...

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
"session_start" must go at the top of each an every page that is part of the session in order to set and pick up the session_id.  http://us2.php.net/manual/en/function.session-start.php  Putting $_SESSION['userkey'] = ".$userkey[0]."; before "session_start" means that it doesn't work or possibly assigns to some unknown session.

This page http://us2.php.net/manual/en/function.session-is-registered.php says not to do what you are doing with "session_is_registered" but to use 'isset' instead.
0
Marco GasiFreelancerCommented:
Probably, is something related to configuration: are you using cookies or SID?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Scott MadeiraCommented:
You need to do a session_start on your login page.  You don't indicate that you are doing that.

Also, as a debugging exercise,I would print the contents of your session variable in your login_success page to see what, if anything is there.

Put a print_r($_SESSION);  statement after your session_start(); and before the if() statement.

Also, session_is_registered() is deprecated in version 5.3.  I'd suggest checking if the key is in the array and testing the value:

if (! array_key_exists('userkey', $_SESSION)){

}
0
lepirtleAuthor Commented:
Not so stupid question. The answer is "no". I guess that makes me the "stupd" one. I thought the session_start function only needed to be on the login page, not the page that creates the session. I will add that and see what happens.
0
Dave BaldwinFixer of ProblemsCommented:
That doesn't make you 'stupid', just inexperienced.  This is a fairly common question and scenario.
0
Marco GasiFreelancerCommented:
I didn't mean to say you're stupid, not at all! :-) You can be inexperienced, as Dave suggests, or only careless or even tired: I do similar errors for all these three reasons separately or simoultaneously.

Thanks for points and good luck with your project!

Marco
0
lepirtleAuthor Commented:
Dave and marqusG,
No I didn't take that as a slight at all. But thanks for being apologetic and sensitive. I am very inexperienced and maybe a little tired after trying for 2 hours to figure out this problem then you solve it in minutes. I envy your knowledge.

I appreciate your continued assistance.
Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.