Session problem

Session problem

am 28.12.2007 17:23:21 von Aaron Gray

Hello, this is my first posting to comp.lang.php, happy crimbo and all that
:)

I am having problems with sessions, first entry fine but next entry I am
getting blanks, then next entry I am getting datafields back again, and this
alternates.

My example is here :-

http://www.aarongray.org/Test/PHP/test.php

And source :-

http://www.aarongray.org/Test/PHP/test.php.txt

Hope I am not doing something too stupid.

Many thanks in advance,

Aaron

Re: Session problem

am 28.12.2007 17:54:20 von My Pet Programmer

Aaron Gray said:
> Hello, this is my first posting to comp.lang.php, happy crimbo and all that
> :)
>
> I am having problems with sessions, first entry fine but next entry I am
> getting blanks, then next entry I am getting datafields back again, and this
> alternates.
>
> My example is here :-
>
> http://www.aarongray.org/Test/PHP/test.php
>
> And source :-
>
> http://www.aarongray.org/Test/PHP/test.php.txt
>
> Hope I am not doing something too stupid.
>
> Many thanks in advance,
>
> Aaron
>
>
The .txt link is still rendering as PHP, can't see the source, bud.

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

Re: Session problem

am 28.12.2007 18:12:08 von Aaron Gray

"My Pet Programmer" wrote in message
news:fl39o0$pkh$1@registered.motzarella.org...
> Aaron Gray said:
>> Hello, this is my first posting to comp.lang.php, happy crimbo and all
>> that :)
>>
>> I am having problems with sessions, first entry fine but next entry I am
>> getting blanks, then next entry I am getting datafields back again, and
>> this alternates.
>>
>> My example is here :-
>>
>> http://www.aarongray.org/Test/PHP/test.php
>>
>> And source :-
>>
>> http://www.aarongray.org/Test/PHP/test.php.txt
>>
>> Hope I am not doing something too stupid.
>>
>> Many thanks in advance,
>>
>> Aaron
>>
>>
> The .txt link is still rendering as PHP, can't see the source, bud.

Try :-

http://www.aarongray.org/Test/PHP/test.txt

Sorry have not done this before !:)

Aaron

Re: Session problem

am 28.12.2007 18:31:19 von My Pet Programmer

Aaron Gray said:
> "My Pet Programmer" wrote in message
> news:fl39o0$pkh$1@registered.motzarella.org...
>> Aaron Gray said:
>>> Hello, this is my first posting to comp.lang.php, happy crimbo and all
>>> that :)
>>>
>>> I am having problems with sessions, first entry fine but next entry I am
>>> getting blanks, then next entry I am getting datafields back again, and
>>> this alternates.
>>>
>>> My example is here :-
>>>
>>> http://www.aarongray.org/Test/PHP/test.php
>>>
>>> And source :-
>>>
>>> http://www.aarongray.org/Test/PHP/test.php.txt
>>>
>>> Hope I am not doing something too stupid.
>>>
>>> Many thanks in advance,
>>>
>>> Aaron
>>>
>>>
>> The .txt link is still rendering as PHP, can't see the source, bud.
>
> Try :-
>
> http://www.aarongray.org/Test/PHP/test.txt
>
> Sorry have not done this before !:)
>
> Aaron
>
>
For the sake of brevity, I put all the PHP code at the top, here. Just a
habit of mine. It happens first, so I like it at the top.

if (!isset($_SESSION['mysqlhost']))
$mysqlhost = 'localhost';
else
$mysqlhost = $_SESSION['mysqlhost'];

You should be bracing even single statement blocks, just so you don't
get confused.


if (isset($_SESSION['mysqlusr']))
$mysqlusr = $_SESSION['mysqlusr'];

This causes a warning because if the session var isn't set, you have an
undeclared variable you're using later.

if (isset($_SESSION['mysqlpass']))
$mysqlpass = $_SESSION['mysqlpass'];

Same here, if the session is not set, you have no variable, yet you use
it anyway.


As an aside, if I submitted your form with the enter key instead of
clicking the button, this would only fire in some browsers,

if (isset($_POST['submitquery']))
{

$_SESSION['mysqlhost'] = $mysqlhost = $_POST['host'];
$_SESSION['mysqlusr'] = $mysqlusr = $_POST['user'];
$_SESSION['mysqlpass'] = $mysqlpass = $_POST['password'];

Just don't do this. Your intention is unclear, and someone will have
to code after you if you're coding professionally sometime, and this is
a pain in the butt to read.
}

Your code should look more like:


@session_start();

if (isset($_POST['submitquery']) && $_POST['submitquery']) {
$_SESSION['mysqlhost'] =
htmlspecialchars(stripslashes($_POST['mysqlhost']));
$_SESSION['mysqluser'] =
htmlspecialchars(stripslashes($_POST['mysqluser']));
$_SESSION['mysqlpass'] =
htmlspecialchars(stripslashes($_POST['mysqlpass']));
} // if

$mysqlhost = isset($_SESSION['mysqlhost']) ?
$_SESSION['mysqlhost'] :
"localhost";
$mysqluser= isset($_SESSION['mysqluser']) ?
$_SESSION['mysqluser'] :
"";
$mysqlpass= isset($_SESSION['mysqlpass']) ?
$_SESSION['mysqlpass'] :
"";
print "

";
print_r($_SESSION);
print "
";
?>


Session Test


Session Test














Host:




User:




Password:












And you'll get better results.

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

Re: Session problem

am 28.12.2007 20:34:46 von Aaron Gray

"My Pet Programmer" wrote in message
news:fl3btb$3hs$1@registered.motzarella.org...
> Aaron Gray said:
>> "My Pet Programmer" wrote in message
>> news:fl39o0$pkh$1@registered.motzarella.org...
>>> Aaron Gray said:
>>>> Hello, this is my first posting to comp.lang.php, happy crimbo and all
>>>> that :)
>>>>
>>>> I am having problems with sessions, first entry fine but next entry I
>>>> am getting blanks, then next entry I am getting datafields back again,
>>>> and this alternates.
>>>>
>>>> My example is here :-
>>>>
>>>> http://www.aarongray.org/Test/PHP/test.php
>>>>
>>>> And source :-
>>>>
>>>> http://www.aarongray.org/Test/PHP/test.php.txt
>>>>
>>>> Hope I am not doing something too stupid.
>>>>
>>>> Many thanks in advance,
>>>>
>>>> Aaron
>>>>
>>>>
>>> The .txt link is still rendering as PHP, can't see the source, bud.
>>
>> Try :-
>>
>> http://www.aarongray.org/Test/PHP/test.txt
>>
>> Sorry have not done this before !:)
>>
>> Aaron
>>
>>
> For the sake of brevity, I put all the PHP code at the top, here. Just a
> habit of mine. It happens first, so I like it at the top.
>
> if (!isset($_SESSION['mysqlhost']))
> $mysqlhost = 'localhost';
> else
> $mysqlhost = $_SESSION['mysqlhost'];
>
> You should be bracing even single statement blocks, just so you don't get
> confused.
>
>
> if (isset($_SESSION['mysqlusr']))
> $mysqlusr = $_SESSION['mysqlusr'];
>
> This causes a warning because if the session var isn't set, you have an
> undeclared variable you're using later.
>
> if (isset($_SESSION['mysqlpass']))
> $mysqlpass = $_SESSION['mysqlpass'];
>
> Same here, if the session is not set, you have no variable, yet you use it
> anyway.
>
>
> As an aside, if I submitted your form with the enter key instead of
> clicking the button, this would only fire in some browsers,
>
> if (isset($_POST['submitquery']))
> {
>
> $_SESSION['mysqlhost'] = $mysqlhost = $_POST['host'];
> $_SESSION['mysqlusr'] = $mysqlusr = $_POST['user'];
> $_SESSION['mysqlpass'] = $mysqlpass = $_POST['password'];
>
> Just don't do this. Your intention is unclear, and someone will have to
> code after you if you're coding professionally sometime, and this is a
> pain in the butt to read.
> }
>
> Your code should look more like:
>
> >
> @session_start();
>
> if (isset($_POST['submitquery']) && $_POST['submitquery']) {
> $_SESSION['mysqlhost'] =
> htmlspecialchars(stripslashes($_POST['mysqlhost']));
> $_SESSION['mysqluser'] =
> htmlspecialchars(stripslashes($_POST['mysqluser']));
> $_SESSION['mysqlpass'] =
> htmlspecialchars(stripslashes($_POST['mysqlpass']));
> } // if
>
> $mysqlhost = isset($_SESSION['mysqlhost']) ?
> $_SESSION['mysqlhost'] :
> "localhost";
> $mysqluser= isset($_SESSION['mysqluser']) ?
> $_SESSION['mysqluser'] :
> "";
> $mysqlpass= isset($_SESSION['mysqlpass']) ?
> $_SESSION['mysqlpass'] :
> "";
> print "

";
> print_r($_SESSION);
> print "
";
> ?>
>
>
> Session Test
>
>
>

Session Test


>
>

>
>
>
>
>
>
>
>
>
>

> Host:
>

>

>

> User:
>

>

>

> Password:
>

>

>

>
>


>
>


>

>
>
>
> And you'll get better results.

Thanks Anthony,

That works fine, good programming practices too.

But when I add the rest of my code it fails to fill in the form feilds,
cannot fathom why.

Aaron

Re: Session problem

am 28.12.2007 21:27:58 von My Pet Programmer

Aaron Gray said:
[snip]
> That works fine, good programming practices too.
>
> But when I add the rest of my code it fails to fill in the form feilds,
> cannot fathom why.
>
> Aaron
>
>
Well, let's take a look! Show me, and I'll help if I can.

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

Re: Session problem

am 28.12.2007 21:41:42 von Aaron Gray

"My Pet Programmer" wrote in message
news:fl3m8j$g52$3@registered.motzarella.org...
> Aaron Gray said:
> [snip]
>> That works fine, good programming practices too.
>>
>> But when I add the rest of my code it fails to fill in the form feilds,
>> cannot fathom why.
>>
>> Aaron
>>
>>
> Well, let's take a look! Show me, and I'll help if I can.

Anthony,

Can I contact you off group ?

Aaron

Re: Session problem

am 28.12.2007 21:50:19 von My Pet Programmer

Aaron Gray said:
> "My Pet Programmer" wrote in message
> news:fl3m8j$g52$3@registered.motzarella.org...
>> Aaron Gray said:
>> [snip]
>>> That works fine, good programming practices too.
>>>
>>> But when I add the rest of my code it fails to fill in the form feilds,
>>> cannot fathom why.
>>>
>>> Aaron
>>>
>>>
>> Well, let's take a look! Show me, and I'll help if I can.
>
> Anthony,
>
> Can I contact you off group ?
>
> Aaron
>
>
Feel free, most people just do it without asking. :)

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein