Page 1 of 1

Session error: Undefined index, But it cant be!

Posted: Thu Apr 22, 2004 8:04 am
by yosuke_
Hi!
Im createing a login script and I have problems with sessions!! I have 4 pages! Default.php, Login.php, Logincheck.php and sessionerror.php.

Default.php displays username if user is loged in like this:

Code: Select all

<?php
session_start();
if (isset($_SESSION['myusername'])){
echo "<font face='verdana' size='1'>Welcome <b>" . $_SESSION['myusername'] . "</b>. <a href='sessionerror.php'>Session Error</a> | Change Password | <a href='Logout.php'>Logout</a><font><br>";
}
else
{
echo "<font face='verdana' size='1'>Welcome <b>Guest</b>, you may <a href='login.php'>login</a> or <a href='registration.htm'>register</a>.</font></right><br>";
}
?>
The seccond page is login page and it checks for valid username and password in database, if user and password is valid then it do this:

Code: Select all

<?php
$_SESSION['myusername'] = $username;
$_SESSION['mypassword'] = $password;
echo "Thank you " . $_SESSION['myusername'] . ",You are loged in successfully.";
echo "<meta http-equiv='Refresh' content='3;url=/default.php'>";
?>
and then it redirects user to default.php and in default.php it now displays this:
Welcome Username, Session Error | Change Password | Logout

And then when I click Session Error link I get this:

Notice: Undefined index: myusername in C:\inetpub\wwwroot\uploadfrm.php on line 4

and code for sessionerror.php is this:

Code: Select all

<?php
session_start();
echo $_SESSION['myusername'];
?>
Why does it looses index??? What is wrong? Thank you!

Posted: Thu Apr 22, 2004 8:09 am
by magicrobotmonkey
it says the error is in "uploadfrm.php on line 4" Is sessionerror.php including uploadfrm.php ?

Posted: Thu Apr 22, 2004 8:39 am
by twigletmac
Maybe you are missing a [php_man]session_start[/php_man]() - you need session_start() on every page in which you set or access session variables.

Mac