0

Here is my code

<html>
<head>
<title>login page</title>
</head>
<body bgcolor="black" style="color:gray">
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>
<?php
session_start(); 
if(isset($_SESSION["logging"])&& $_SESSION["logged"])
{
     print_secure_content();
}
else {
    if(!$_SESSION["logging"])
    {  
    $_SESSION["logging"]=true;
    loginform();
    }
       else if($_SESSION["logging"])
       {
         $number_of_rows=checkpass();
         if($number_of_rows==1)
            {   
             $_SESSION[user]=$_GET[userlogin];
             $_SESSION[logged]=true;
             print"<h1>you have loged in successfully</h1>";
             print_secure_content();
            }
            else{
                print "wrong pawssword or username, please try again";  
                loginform();
            }
        }
     }

function loginform()
{
print "please enter your login information to proceed with our site";
print ("<table border='2'><tr><td>username</td><td><input type='text' name='userlogin' size'20'></td></tr><tr><td>password</td><td><input type='password' name='password' size'20'></td></tr></table>");
print "<input type='submit' >"; 
print "<h3><a href='registerform.php'>register now!</a></h3>";  
}

function checkpass()
{
$servername="localhost";
$username="root";
$conn=  mysqli_connect($servername,$username)or die(mysqli_error());
mysqli_select_db($conn,"test");
$sql="select * from users where name='$_POST[userlogin]' and password='$_POST[password]'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
return  mysqli_num_rows($result);
}

function print_secure_content()
{
    print("<b><h1>hi mr.$_SESSION[user]</h1>");
    print "<br><h2>only a logged in user can see this</h2><br><a href='logout.php'>Logout</a><br>"; 

}
?>

</form>
</body>
</html>  

I get this 3 errors

Notice: Undefined index: logged in C: on line 10 Notice: Undefined index: userlogin in C on line 51 Notice: Undefined index: password in on line 51

I have tryied isset() but nothing changed on line 10. End error 2 and 3 i have no clue. Thank you very much.

I have looked in Reference topic,but it didn't solve my problem.

Florin Iuonas
  • 13
  • 1
  • 4
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – kero Mar 14 '15 at 16:54
  • Looks like it should work, but a few ideas that might be causing unusual behavior. You have isset on logging but not on logged. Have you tried an isset on that? You have == instead of === on number of rows if statement, == I find has unusual behavior. Finally, perhaps you should pass the password to checkpass. I suspect one of these if causing the other problems. Those are just a few loose guesses. – Goose Mar 14 '15 at 17:15
  • you missed the quotes.$sql="select * from users where name=".$_POST['userlogin']." and password=".$_POST['password']; – Alaksandar Jesus Gene Mar 14 '15 at 17:43
  • and possibly your form field doesnot have the name attribute. Note you are using $_GET['userlogin'] which you need to confirm how you are receiving the output. – Alaksandar Jesus Gene Mar 14 '15 at 17:44
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Mar 15 '15 at 00:24

1 Answers1

1
  1. Put the session_start() function as the very first thing before the <html> tag.
  2. On line 10 you forget to put isset() on the second condition that would be like if(isset($_SESSION["logging"])&& isset($_SESSION["logged"]))
  3. Add quotes at line 28 and 29: $_SESSION['user']=$_GET['userlogin']; and $_SESSION['logged']=true;
  4. Concatenate the variable like this print("<b><h1>hi mr" . $_SESSION[user] . "</h1>");

and your final code would be like:

<?php
session_start(); 
?>
<html>
<head>
<title>login page</title>
</head>
<body bgcolor="black" style="color:gray">
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>
<?php
if(isset($_SESSION["logging"])&& isset($_SESSION["logged"]))
{
     print_secure_content();
}
else 
{
    if(!$_SESSION["logging"])
    {  
        $_SESSION["logging"] = true;
        loginform();
    }
    else if($_SESSION["logging"])
    {
        $number_of_rows = checkpass();
        if($number_of_rows == 1)
        {
            $_SESSION['user'] = $_GET['userlogin'];
            $_SESSION['logged'] = true;
            print"<h1>you have loged in successfully</h1>";
            print_secure_content();
        }
        else{
            print "wrong pawssword or username, please try again";  
            loginform();
        }
    }
}

function loginform()
{
    print "please enter your login information to proceed with our site";
    print ("<table border='2'><tr><td>username</td><td><input type='text' name='userlogin' size'20'></td></tr><tr><td>password</td><td><input type='password' name='password' size'20'></td></tr></table>");
    print "<input type='submit' >"; 
    print "<h3><a href='registerform.php'>register now!</a></h3>";  
}

function checkpass()
{
    $servername="localhost";
    $username="root";
    $conn=  mysqli_connect($servername,$username)or die(mysqli_error());
    mysqli_select_db($conn,"test");
    $sql="select * from users where name='$_POST[userlogin]' and password='$_POST[password]'";
    $result=mysqli_query($conn,$sql) or die(mysqli_error());
    return  mysqli_num_rows($result);
}

function print_secure_content()
{
    print("<b><h1>hi mr" . $_SESSION[user] . "</h1>");
    print "<br><h2>only a logged in user can see this</h2><br><a href='logout.php'>Logout</a><br>"; 

}
?>

</form>
</body>
</html>  
anhamande
  • 36
  • 5