我正在尝试为我的网站制作登录页面
我目前正在使用 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 } ?>