简体   繁体   English

php session和while-loop:未定义的索引

[英]php session and while-loop: undefined index

I am trying to pass a variable through a session. 我试图通过会话传递变量。 I use a while loop to run through a database, which works perfectly. 我使用while循环来运行一个完美的数据库。 If $username and $password are correct I always get these error notices: 如果$username$password是正确的,我总是得到这些错误通知:

Notice: Undefined index: GELD in login.php on line 注意:未定义索引:登录时login.php中的GELD

Notice: Undefined index: NIVEAU in login.php on line 注意:未定义的索引:登录时的login.php中的NIVEAU

Why do I get these error notices and how could I resolve them? 为什么我会收到这些错误通知,我该如何解决?

index.html 的index.html

<form id="loginform" method="post" action="login.php">
    <img src="afbeelding/logo.png">
    <label for="username">
        Username
    </label>
    <input id="username" type="text" name="username"/>
    <label for="password">Password</label>
    <input id="password" type="password" name="password"/>
    <input type="submit" value="Login">
</form>

login.php 的login.php

<?php
    session_start();
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    $dbhost = 'sql7.xxx.net';
    $dbuser = 'xxx';
    $dbpass = 'xxx';
    $db = 'xxx';

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

    $con = mysql_connect($dbhost, $dbuser, $dbpass);
    $db_found = mysql_select_db($db, $con);
    $result = mysql_query("SELECT ID, USERNAME, PASSWORD FROM Game");

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

    while ($row = mysql_fetch_array($result)) {
        if($row{'USERNAME'} == $username){
            if($row{'PASSWORD'} == $password){
                $_SESSION['geld'] = $row{'GELD'};
                $_SESSION['niveau'] = $row{'NIVEAU'};
            }
            else{
            }
        }
    }
?>

SELECT ID, USERNAME, PASSWORD FROM Game - there you didn't select the GELD and NIVEAU columns in your query. SELECT ID, USERNAME, PASSWORD FROM Game - 您没有在查询中选择GELDNIVEAU列。 and don't use a deprecated MySQL API. 并且不要使用已弃用的MySQL API。

Use mysqli with prepared statements , or PDO with prepared statements . mysqlimysqli准备语句一起使用,或将PDO与 mysqli准备语句 一起使用

Passwords 密码

If you're live with this or intend on going live with plain text password, STOP right there. 如果您对此有所了解或打算使用纯文本密码上线,请在此处停止。

For password storage, use CRYPT_BLOWFISH or PHP 5.5's password_hash() function. 对于密码存储,请使用CRYPT_BLOWFISH或PHP 5.5的password_hash()函数。

For PHP < 5.5 use the password_hash() compatibility pack . 对于PHP <5.5,请使用password_hash() compatibility pack

Also consult the manual on password_verify() . 另请参阅password_verify()手册。

Sidenote about using password_hash() and column length. 关于使用password_hash()和列长度的旁注。

If and when you do decide to use password_hash() or crypt, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). 如果您决定使用password_hash()或crypt,请务必注意,如果您当前的密码列的长度低于60,则需要将其更改为(或更高)。 The manual suggests a length of 255. 手册建议长度为255。

You will need to ALTER your column's length and start over with a new hash in order for it to take effect. 您需要更改列的长度并重新开始使用新哈希才能使其生效。 Otherwise, MySQL will fail silently. 否则,MySQL将无声地失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM