Jump to content

[SOLVED] Notice: Undefined index:


WEIR_SJ

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.