Session error: Undefined index, But it cant be!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Session error: Undefined index, But it cant be!

Post 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!
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

it says the error is in "uploadfrm.php on line 4" Is sessionerror.php including uploadfrm.php ?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply