0

我正在尝试为我的网站制作登录页面

我目前正在使用 xampp 进行测试,然后将其上传到我的主机并制作了一个包含一些测试数据的 mysql 数据库。

当我转到本地 localhost/xampp/Login.php 时,出现错误: 注意:未定义索引:第 41 行 C:\xampp\htdocs\xampp\Login.php 中的用户名

Notice: Undefined index: password in C:\xampp\htdocs\xampp\Login.php on line 41

Notice: Undefined index: logout in C:\xampp\htdocs\xampp\Login.php on line 43

Notice: Undefined index: test_account in C:\xampp\htdocs\xampp\Login.php on line 25

这里的第一行是第 41 行

if($_POST['username'] !='' || $_POST['password'] != '') {
$login_status = login($_POST['username'], $_POST['password']);
} else if($_GET['logout']) {
logout();
}
$userid = status();

我的表单发送数据,所以我不明白为什么它会出错

<form action="Login.php" method="POST">
<input type=text name=username>
<input type=password name=password>
<input type=submit value="Log In">
</form>

当我单击登录时没有任何反应,这是我正在使用的完整 php 文件

<?php

    $link = mysql_connect("localhost", "admin", "1234");
    mysql_select_db("test_database", $link);

function login($username, $password) {
        $username = addslashes($username);
        $password = md5($password);
    $query = mysql_query("SELECT * FROM user_accounts WHERE username='$username' AND password='$password'");
    if(mysql_num_rows($query) == 1) {
        $info = mysql_fetch_array($query);
        $userid = $info[userid];
        $sessionid = md5($userid . time());
        $time = time();
        @setcookie ('test_account', $sessionid, $time+3600, '/', '');
        mysql_query("DELETE FROM user_sessions WHERE userid='$userid'");
        mysql_query("INSERT INTO user_sessions (sessionid,userid,timestamp) VALUES('$sessionid','$userid','$time')");
        return $userid;
    } else {
        return 0;
    }
}

function status() {
    $sessionid = $_COOKIE[test_account];
    $oldtime = time() - 3600;
    $query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime");
    if(mysql_num_rows($query) == 1) {
        $info = mysql_fetch_array($query);
        return $info[userid];
    }
    return 0;
} 

function logout() {
    $sessionid = $_COOKIE[test_account];
    @setcookie ("test_account",'', time()-99999, '/', '');
    mysql_query("DELETE FROM user_sessions WHERE sessionid='$sessionid'");
}

if($_POST[username] !='' || $_POST[password] != '') {
    $login_status = login($_POST[username], $_POST[password]); 
} else if($_GET[logout]) {
    logout();
}
$userid = status();


if($userid > 0) { echo "Welcome to our site, user #$userid (<a href='?logout'>Click here to logout</a>)"; } else {

if($login_status != '' $login_status == 0) { echo "Invalid username/password combo.<br>"; }
?>

<form action="sample.php" method="POST">
<input type=text name=username>
<input type=password name=password>
<input type=submit value="Log In">
</form>

<?php } ?>
4

3 回答 3

1

你应该检查是否有$_POST,然后登录系统,如果没有,则显示表格。如果用户已通过身份验证,则无需向他显示表单。

<?php
$userid = status();
if($userid > 0) {
    if(isset($_GET['logout'])){
        logout();
        header('Location: Login.php');
        exit();
    }
    echo "Welcome to our site, user #$userid (<a href='?logout'>Click here to logout</a>)";
    exit();
}

if (isset($_POST['username']) && isset($_POST['password'])){
    $login_status = login($_POST[username], $_POST[password]);
    //if success login
    if($login_status) {
        header('Location: Login.php');
        exit();
    }
    echo "Invalid username/password combo.<br>"; 
}
?>
<form action="sample.php" method="POST">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Log In">
</form>
于 2012-07-06T09:25:23.343 回答
1

如果变量存在,您必须尝试:

if(isset($_POST['username']))

因为当你进入你的页面时,表单还没有提交,所以变量不存在。

于 2012-07-06T08:47:51.040 回答
1

您需要首先检查 $_POST 是否设置如下:

<?php
    if(isset($_POST['username'])&& isset($_POST['password']))
    {
        if(!empty($_POST['username']) && !empty($_POST['password'])) {
            $login_status = login($_POST['username'], $_POST['password']); 
        } else if($_GET['logout']) {
            logout();
        }
    }
?>

在您的表单中,您应该在项目名称周围使用额外的 ",如下所示:

<form action="sample.php" method="POST">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Log In">
</form>
于 2012-07-06T08:55:54.707 回答