PHP Session and variable question… – PHP

  php

Q(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,

A(Answer):

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:

A(Answer):

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:

A(Answer):

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:

A(Answer):

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:

A(Answer):

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

A(Answer):

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

LEAVE A COMMENT