469,336 Members | 5,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

PHP Session and variable question...

Hello,

I have a really easy (I hope) question but have come you a blocking point
and need some help...

My web page contains both standard HTML and PHP code. No problem there... On
one page after the user logs in I set a session variable $_SESSION['fName']
= $fName // $fName is passed to the function. When I draw this page the
variable in $_SESSION['fName'] echo's correctly and I can go to other pages
and it will still be there correctly. However, I need more than one variable
$_SESSION['account']=$eMailAddr where $eMailAddr = their email address. If I
add this to the code where I set $_SESSION['fName] it all echoes correctly
on the first page, however, if I got to another page only the ['fName]
echoes, ['account] does not. Any suggestions? Remember this php code is
imbedded in an HTML page.

Thanks,
Jul 16 '05 #1
6 13955
Rob @ Home wrote:
Hello,

I have a really easy (I hope) question but have come you a blocking point
and need some help...

[...]

try putting these statements at the very top of your pages:

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 0);
ini_set('error_reporting', E_ALL);
?>

What they do is set your php to display all errors, warnings and
notices on the browser (and not log them). After you find the error
(if it is found this way), remove the lines ...

....

.... or let them in and remove all errors, all warnings, and all
notices that show up when you run your scripts.

Happy Coding :-)

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #2
Pedro,

I did this and got "Notice: Undefined index: fName in
/home/blah/public_html/userFuncs.php line 10"

line 10 is:
echo $_SESSION['fName'];

at the top of the function I call global $_SESSION;

Does this shed any light?
Rob
"Pedro" <he****@hotpop.com> wrote in message
news:gm********************************@4ax.com...
Rob @ Home wrote:
Hello,

I have a really easy (I hope) question but have come you a blocking point
and need some help...

[...]

try putting these statements at the very top of your pages:

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 0);
ini_set('error_reporting', E_ALL);
?>

What they do is set your php to display all errors, warnings and
notices on the browser (and not log them). After you find the error
(if it is found this way), remove the lines ...

...

... or let them in and remove all errors, all warnings, and all
notices that show up when you run your scripts.

Happy Coding :-)

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:

Jul 16 '05 #3
Rob @ Home wrote:
Pedro,

I did this and got "Notice: Undefined index: fName in
/home/blah/public_html/userFuncs.php line 10"

line 10 is:
echo $_SESSION['fName'];

at the top of the function I call global $_SESSION;

Does this shed any light?


You don't need to 'globalize' the _SESSION array

<?php
function print_test() {
echo $_SESSION['test'];
}

session_start();
$_SESSION['test'] = 'test ok';
print_test();
?>

will work very nicely.
My guess is you have variable names mixed up somewhere in your code.
Hopefully, seeing all the errors, warnings and notices, would let you
find the misnamed variables.

As for your specific error (fName undefined) are you sure you set the
_SESSION index as "fName" (capitalization matters!!!)

try outputting all of the _SESSION array right after line 10

<?php
// ...
// line 9
echo $_SESSION['fName'];
foreach ($_SESSION as $k => $v) echo $k, ' = ', $v, '<br />';
// etc. ...
?>

and check the names of the indices you have set versus what you think
you should have :)

Pay attention to l, 1, O, 0, ... and capitalization

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #4
The trick was not including the global $_SESSION

Thanks

"Pedro" <he****@hotpop.com> wrote in message
news:ih********************************@4ax.com...
Rob @ Home wrote:
Pedro,

I did this and got "Notice: Undefined index: fName in
/home/blah/public_html/userFuncs.php line 10"

line 10 is:
echo $_SESSION['fName'];

at the top of the function I call global $_SESSION;

Does this shed any light?


You don't need to 'globalize' the _SESSION array

<?php
function print_test() {
echo $_SESSION['test'];
}

session_start();
$_SESSION['test'] = 'test ok';
print_test();
?>

will work very nicely.
My guess is you have variable names mixed up somewhere in your code.
Hopefully, seeing all the errors, warnings and notices, would let you
find the misnamed variables.

As for your specific error (fName undefined) are you sure you set the
_SESSION index as "fName" (capitalization matters!!!)

try outputting all of the _SESSION array right after line 10

<?php
// ...
// line 9
echo $_SESSION['fName'];
foreach ($_SESSION as $k => $v) echo $k, ' = ', $v, '<br />';
// etc. ...
?>

and check the names of the indices you have set versus what you think
you should have :)

Pay attention to l, 1, O, 0, ... and capitalization

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:

Jul 16 '05 #5
Well it should work!

page1:
<?
session_start();
$_SESSION['fName'] = $fName;
$_SESSION['account'] = $eMailAddr;
?>

page2:
<?
session_start();
echo $_SESSION['fName'];
echo $_SESSION['account'];
?>

logically, that works fine. so if it does not on yours then the problem is
elsewhere.. you may find that its not even being set because it never
reaches that part of the code.

*paste code*
pasting code makes replies likely to be more accurate :)

bai bai

Petey

Jul 17 '05 #6
Well it should work!

page1:
<?
session_start();
$_SESSION['fName'] = $fName;
$_SESSION['account'] = $eMailAddr;
?>

page2:
<?
session_start();
echo $_SESSION['fName'];
echo $_SESSION['account'];
?>

logically, that works fine. so if it does not on yours then the problem is
elsewhere.. you may find that its not even being set because it never
reaches that part of the code.

*paste code*
pasting code makes replies likely to be more accurate :)

bai bai

Petey

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nate Spillson | last post: by
4 posts views Thread by John Kraft | last post: by
2 posts views Thread by Joe Molloy | last post: by
2 posts views Thread by +The_Taco+ | last post: by
5 posts views Thread by Andy G | last post: by
1 post views Thread by Vidyadhar Joshi | last post: by
reply views Thread by sweetpotatop | last post: by
17 posts views Thread by Control Freq | last post: by
11 posts views Thread by Dave | last post: by
5 posts views Thread by Twayne | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.