WEIR_SJ Posted June 1, 2009 Share Posted June 1, 2009 hi guys n gals, im a total newbie to php so this may seem like an easy question to most of you.... im stuck with a login php... the error im getting is Notice: Undefined index: user in C:\wamp\www\Learning\movie1.php on line 3 and the php code im using is from a book and i cant see where i have gone wrong... <?php session_start(); $_SESSION['username'] = $_POST['user']; $_SESSION['userpass'] = $_POST['pass']; $_SESSION['authuser'] = 0; //Check username and password infomation if (($_SESSION['username'] == 'Joe') and ($_SESSION['userpass'] == '12345')) { $_SESSION['authuser'] = 1; } else { echo 'Sorry, but you don\'t have permission to view this page!'; exit(); } ?> <html> <head> <title>Find my Favorite Movie!</title> </head> <body> <?php $myfavmovie = urlencode('Life of Brian'); echo "<a href=\"moviesite.php?favmovie=$myfavmovie\">"; echo 'Click here to see information about my favorite movie!'; echo '</a>'; ?> </body> </html> im hoping some1 can put me right thanks Stu Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/ Share on other sites More sharing options...
GingerRobot Posted June 1, 2009 Share Posted June 1, 2009 Line 3 is trying to set a session variable to something in the $_POST array. That something doesn't exist. Either you've a.) Not submitted the form before viewing this page, b.) set your method to GET and not POST, c.) set the name of the form input to something other than 'user' -- don't forget it's case sensitive; user is different from User or d.) done something else i cant think of right now. Maybe if you post up your form code we can help you further. Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846763 Share on other sites More sharing options...
WEIR_SJ Posted June 1, 2009 Author Share Posted June 1, 2009 ok below is all the code from all 3 pages... login.php <?php session_unset(); ?> <html> <head> <title>Please Log In</title> </head> <body> <form method="post" action="movie1.php"> <p>Enter your username: <input type="text" name"user"/> </p> <p>Enter your password: <input type="password" name="pass"/> </p> <p> <input type="submit" name="submit" value="submit"/> </p> </form> </body> </html> movie1.php <?php session_start(); $_SESSION['username'] = $_POST['user']; $_SESSION['userpass'] = $_POST['pass']; $_SESSION['authuser'] = 0; //Check username and password infomation if (($_SESSION['username'] == 'Joe') and ($_SESSION['userpass'] == '12345')) { $_SESSION['authuser'] = 1; } else { echo 'Sorry, but you don\'t have permission to view this page!'; exit(); } ?> <html> <head> <title>Find my Favorite Movie!</title> </head> <body> <?php $myfavmovie = urlencode('Life of Brian'); echo "<a href=\"moviesite.php?favmovie=$myfavmovie\">"; echo 'Click here to see information about my favorite movie!'; echo '</a>'; ?> </body> </html> moviesite.php <?php session_start(); //check to see if the user has logged in with a valid password if ($_SESSION['authuser'] !=1) { echo 'Sorry, but you don\'t have permission to view this page!'; exit(); } ?> <html> <head> <title>My Movie Site - <?php echo $_GET['favmovie']; ?></title> </head> <body> <?php echo 'Welcome to our site, '; echo '! <br/>'; echo 'My favorite movie is '; echo $_GET['favmovie']; echo '<br/>'; $movierate = 5; echo 'My movie rating for this movie is: '; echo $movierate ?> </body> </html> hope this helps Stu Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846771 Share on other sites More sharing options...
GingerRobot Posted June 1, 2009 Share Posted June 1, 2009 You're just missing an equals sign on this line: <input type="text" name"user"/> It should be: <input type="text" name="user"/> Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846773 Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 You should also check that an array index exists before you try to read from it. Either isset, empty or array_key_exists can help you with that. Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846789 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 In login.php, change: <input type="text" name"user"/> To: <input type="text" name="user"/> Hope, your script will work now. Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846794 Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 anupamsaha, you might want to read all the responses before replying. GingerRobot provided that solution almost an hour ago. Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846796 Share on other sites More sharing options...
WEIR_SJ Posted June 1, 2009 Author Share Posted June 1, 2009 yeah sorted now guy thanks very much for the replies/help i guess i will get stuck again before long, im just gonna have to watch what im typing a little better. Cheers Stu Quote Link to comment https://forums.phpfreaks.com/topic/160459-solved-notice-undefined-index/#findComment-846797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.