index.php

<form action="proseslogin.php" method="post" name="login" target="_self" id="login" style="style.css" title="login"><br />
        user    
          :  
          <input type="text" name="username" id="username" />
          <label>password:          </label>
          <input type="text" name="password" id="password" />
          <label>
          <input type="submit" name="Login" id="Login" value="Login" />
        </label>
      </form>

proseslogin.php

<?php

//function
function periksa ($username, $password){
		if (($username=="user") and ($password=="123456")){
			return true;
		}else{
			return false;
		}
	}	
	
// cek		
if (periksa($username, $password)) {
		$login=true;	
}
else {
		echo "User ID atau password salah!";
}
if ($login) {
	echo "<br>Di sini blok aplikasi setelah login dilakukan";
	echo "<br>Anda berhasil menjalankan!";
}


?>

The following error appears:

Notice: Undefined variable: username in C:\xampp\htdocs\php_template2\proseslogin.php on line 15

Notice: Undefined variable: password in C:\xampp\htdocs\php_template2\proseslogin.php on line 15
User ID atau password salah!
Notice: Undefined variable: login in C:\xampp\htdocs\php_template2\proseslogin.php on line 21


line 15:if (periksa($username, $password)) {

line 21: if ($login) {

Why is it?

Thanks.

Because these variables have not received a value, before you are using them. Add to line 3:

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$login = isset($_POST['login']) ? $_POST['login'] : '';

$username and $password don't have any values in your code.
since you already created a function, insert this to proseslogin.php line 3:

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

Thanks. I fixed the codes:

<?php


$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$login = isset($_POST['login']) ? $_POST['login'] : '';

//function
function periksa ($username, $password){
		if (($username=="user") and ($password=="123456")){
			return true;
		}else{
			return false;
		}
	}	
	
// cek		
if (periksa($username, $password)) {
		$login=true;	
}
else {
		echo "User ID atau password salah!";
		
		header("Location: http://localhost/php_template2/index.php");
}
if ($login) {
	echo "<br>Di sini blok aplikasi setelah login dilakukan";
	echo "<br>Anda berhasil menjalankan!";
	header("Location: http://localhost/php_template2/admin.php");
	Exit();
}


?>

Just one more problem, I would like the code if I enter the user and password incorrectly to shows me an error message in index.php and if I enter correctly it brings me to admin.php.

How to do so?

First of all, put the parameter 'error=1' to line 26

header("Location: http://localhost/php_template2/index.php?error=1");

And, index.php

$error = isset($_GET['error']) ? $_GET['error'] : '';
if($error) {
     echo 'Failed login ! Try again...';
}

Put the above code any place you like to display. You can specify the error type with array format.

$error_message = array(
                 'Invalid username',
                 'Invalid password',
                 'User name and password doesn\'t match',
                 );

//show the error
if($error) {
     echo 'Failed login ! Try again...<br />Error: ' . $error_message[$error];
}

You can identify what error type you return via loginprocess.php by passing the parameter 'error=1/2/3/....'.

Hope this help.

how to view the error type in index.php? I would like the error type to be displayed in index.php (I have the code in my first post above).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.