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
fahim
Forum Commoner
Posts: 36 Joined: Sun Jan 05, 2014 7:06 pm
Location: Dhaka, Bangladesh
Post
by fahim » Tue Jan 14, 2014 1:14 am
Hi everybody,
I'm a learner of PHP. Trying to learn PHP just for last few days. I've written a very short program. Unfortunately, the output is showing some error, which I can't understand. Please help me in this regard.
THE CODE
Code: Select all
<?php
session_start();
$_SESSION['username'] = $_POST['user'];
$_SESSION['userpass'] = $_POST['pass'];
$_SESSION['authuser'] = 0;
if (($_SESSION['username'] == 'Joe') and
($_SESSION['userpass'] == '12345')) {
$_SESSION['authuser'] = '1';
} else {
echo "Sorry, but you don't have permission to view this page, you loser!";
exit();
}
?>
THE ERROR MESSAGE
Notice: Undefined index: user in /opt/lampp/htdocs/movie1.php on line 4
Notice: Undefined index: pass in /opt/lampp/htdocs/movie1.php on line 5
Please help anyone. Thanks in advance to all.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Jan 14, 2014 7:46 am
$_POST['user'] and $_POST['pass'] are undefined. This is commonly from trying to read values from a form that hasn't been submitted, or fields therein which were left blank. Prior to attempting to use these array indices, you want to first check that they are set.
Code: Select all
$_SESSION['username'] = isset($_POST['user']) ? $_POST['user'] : null;
fahim
Forum Commoner
Posts: 36 Joined: Sun Jan 05, 2014 7:06 pm
Location: Dhaka, Bangladesh
Post
by fahim » Tue Jan 14, 2014 9:51 am
Thanks to Celauran for your reply.
Maybe it'd be great if I mention this before. I've created a file named
login.php trying to define the values
$_POST['user'] and $_POST['pass'] from that file. The code of login.php below :
Code: Select all
<?php
session_unset();
?>
<html>
<head>
<title>Please Log In</title>
</head>
<body>
<?php include "header.php"; ?>
<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>
Regards.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Jan 14, 2014 9:58 am
And if you var_dump($_POST) inside movie1.php?
fahim
Forum Commoner
Posts: 36 Joined: Sun Jan 05, 2014 7:06 pm
Location: Dhaka, Bangladesh
Post
by fahim » Tue Jan 14, 2014 11:24 am
Celauran wrote: And if you var_dump($_POST) inside movie1.php?
Would you please tell me where/which line can I put "var_dump($_POST)"?
Sorry, for this type of question. Hope you won't be annoyed, as I said I'm very new with PHP
.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Jan 14, 2014 11:27 am
Right at the top of movie1.php
fahim
Forum Commoner
Posts: 36 Joined: Sun Jan 05, 2014 7:06 pm
Location: Dhaka, Bangladesh
Post
by fahim » Tue Jan 14, 2014 11:40 am
Celauran wrote: Right at the top of movie1.php
Thanks for your help. Your code works partially. The output is now:
array(0) { }
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Jan 14, 2014 12:23 pm
Hence the undefined index notices. Is this after having submitted the form?
fahim
Forum Commoner
Posts: 36 Joined: Sun Jan 05, 2014 7:06 pm
Location: Dhaka, Bangladesh
Post
by fahim » Wed Jan 15, 2014 11:26 am
When I submit username and password in login.php file and click SUBMIT. The output is :
[text]array(3) { ["user"]=> string(3) "Joe" ["pass"]=> string(5) "12345" ["submit"]=> string(6) "submit" } [/text]
But when I'm trying to access movie1.php directly, then output is :
[text]array(0) { } [/text]
I'm sorry for my previous post for posting partial output.
Finally, thanks again for your continuous support.