|
Posted by My Pet Programmer on 12/28/07 17:31
Aaron Gray said:
> "My Pet Programmer" <anthony@mypetprogrammer.com> 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:
<?php
@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 "<pre>";
print_r($_SESSION);
print "</pre>";
?>
<html>
<head>
<title>Session Test</title>
</head>
<body onLoad="document.forms[0].elements['submitquery'].focus()">
<h1>Session Test</h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<table>
<tr>
<td>
Host:
<td>
<input type="text" name="mysqlhost" value="<?php echo
$mysqlhost; ?>"/><br/>
</td>
<tr>
<td>
User:
<td>
<input type="text" name="mysqluser" value="<?php echo
$mysqluser; ?>"/><br/>
</td>
<tr>
<td>
Password:
<td>
<input type="password" name="mysqlpass" value="<?php
echo $mysqlpass; ?>"/><br/>
</td>
</tr>
</table>
<p>
<input type="submit" name="submitquery" value="Submit Query" />
</p>
</form>
</body>
</html>
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
Navigation:
[Reply to this message]
|