PHP中的cookie和会话

I've tried everything and nothing works, so, I've created a login page. Now when I log in first I get an error:

Notice: Undefined index: login in C:\wamp\www\ucp\index.php on line 39

and then when i login it wont at first change the page look:

enter image description here

Other images: http://imgur.com/a/xdLO0

and this is the whole code

<?php 

$con = mysqli_connect("localhost","root","","samp");

if (mysqli_connect_errno())
{
    echo "Failed to connect to the database: " . mysqli_connect_error();
    die();
}

session_start();

if(isset($_GET['logout']))
{
    setcookie("login", '',time()-86400); # delete cookie
    exit(); # stop executing here
    header('index.php');
}


if(!empty($_POST['username']) && !empty($_POST['password']))
{

    $userName = isset($_POST["username"]) ? $_POST["username"] : null;
    $userPass = isset($_POST["password"]) ? $_POST["password"] : null;

    $hashedPass = hash('whirlpool', $userPass);
    $query = "SELECT Ime FROM Igraci WHERE Ime = '$userName' AND Lozinka = '$hashedPass'";

    $result = mysqli_query( $con, $query);

    $row = mysqli_fetch_array($result);

    if($row)
    {
        $session = md5($userName.$hashedPass);
        mysqli_query($con, "UPDATE Igraci SET session = '$session' WHERE Ime = '$userName' AND Lozinka = '$hashedPass'");
        setcookie("login", $session,time()+3600);
        echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';

    }
    else
    {
        die('Account has not been found.');
    }
}

if(isset($_COOKIE["login"]) && mb_strlen(isset($_COOKIE["login"]) == '32'))
{
    $session = $con->real_escape_string($_COOKIE["login"]);
    $query = "SELECT Ime FROM Igraci WHERE session = '$session' LIMIT 1";
    $result = mysqli_query( $con, $query); $row = mysqli_fetch_array($result);

    if($row['Ime']) 
    { 
        echo "User is already logged in with username ".$row['Ime']. " and hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';
        exit(); 
    }

}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Roleplay Factory User Control Panel</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href='https://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
</head>
<body>

<h1>Welcome, please login to your account.</h1>

<form action="index.php" method="POST">

    <input type="text" placeholder = "Username" name="username">
    <input type="password" placeholder = "Password" name="password">
    <input type="submit" value="Login">
</form> 

<div class="footer">
<p>roleplay factory &copy; 2016 all rights reserved</p>
</div>

</body>
</html>

You created the cookie after the global variable $_COOKIE (which contains cookie information that PHP received with the HTTP request) was filled with data so you can't access it yet.

Check if the cookie exists. If not, it's first login and output just the value.

if ( isset( $_COOKIE[ "login" ] ) ) {
  // your original line of code
  echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';
} else {
  echo "You are now logged in with hash: ".htmlspecialchars($session). '<a href="index.php?logout=1">logout</a>?';
}

Your line 39, as pointed out by the error message, looks like:

echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';

You don't check if $_COOKIE["login"] is set.