Page 1 of 1

Unwanted error message

Posted: Tue Jan 14, 2014 1:14 am
by fahim
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.

Re: Unwanted error message

Posted: Tue Jan 14, 2014 7:46 am
by Celauran
$_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;

Re: Unwanted error message

Posted: Tue Jan 14, 2014 9:51 am
by fahim
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.

Re: Unwanted error message

Posted: Tue Jan 14, 2014 9:58 am
by Celauran
And if you var_dump($_POST) inside movie1.php?

Re: Unwanted error message

Posted: Tue Jan 14, 2014 11:24 am
by fahim
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 :( .

Re: Unwanted error message

Posted: Tue Jan 14, 2014 11:27 am
by Celauran
Right at the top of movie1.php

Code: Select all

<?php
var_dump($_POST); exit;

Re: Unwanted error message

Posted: Tue Jan 14, 2014 11:40 am
by fahim
Celauran wrote:Right at the top of movie1.php

Code: Select all

<?php
var_dump($_POST); exit;
Thanks for your help. Your code works partially. The output is now:
array(0) { }

Re: Unwanted error message

Posted: Tue Jan 14, 2014 12:23 pm
by Celauran
Hence the undefined index notices. Is this after having submitted the form?

Re: Unwanted error message

Posted: Wed Jan 15, 2014 11:26 am
by fahim
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.