-1

When I run the following code, I get this error:

Notice: Undefined index: user in C:\wamp\www\moviel.php on line 3

And also this:

Notice: Undefined index: pass in C:\wamp\www\moviel.php on line 4

I can't find the mistake. Where is the problem?

<?php
session_start();
$_SESSION['username'] = $_POST['user'];   // <- LINE 3
$_SESSION['userpass'] = $_POST['pass'];   // <- LINE 4
$_SESSION['authuser'] = 0;
// Check username and password information
if(($_SESSION['username'] == 'Joe')and
    ($_SESSION ['userpass'] == '12345')){
    $_SESSION['authuser'] = 1;
}else{
    echo "Sorry, but youd don`t have the permission to view this page, you loser!";
    exit();
}
?>
<html>
<head>
<title>Find My Favorite Movie!</title>
</head>
<body>
<?php include "header.php";
?>
<?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>";
echo "<br>";
echo "<a href ='moviesite.php?movienum = 5'>";
echo "Click here to see my top 5 movies.";
echo "</a>";
echo "</br";
echo  "<a href ='moviesite.php?movienum = 10'>";
echo "Click here to see my top 10 movies"
?>
</body>
</html>
Jason C
  • 38,729
  • 14
  • 126
  • 182
Jough Drak
  • 17
  • 3

1 Answers1

2

$_POST['user'] doesn't exist, it hasn't been posted. Use isset to check if it exists before trying to set a value as it.

if(isset($_POST['user']))
    $_SESSION['username'] = $_POST['user'];
if(isset($_POST['pass']))
    $_SESSION['userpass'] = $_POST['pass'];
Matt
  • 2,851
  • 1
  • 13
  • 27