我正在使用MySqli准备好的语句来做这个。这没有密码。我正在为一位老师做这件事。我希望学生能够在没有密码的情况下输入他们的ID,以便访问可以查看书籍,退还书籍,查看状态等的页面。 这是我的代码:
的functions.php:
<?php
function check_login() {
$host = "localhost";
$user = "root";
$pass = "password";
$data = "login";
$con = mysqli_connect($host, $user, $pass, $data);
if ($stmt = $con -> prepare("SELECT * FROM students WHERE id=?")) {
$stmt -> bind_param("s", $sid);
$stmt -> execute();
$stmt -> store_result();
if($stmt -> num_rows() == "1") {
$_SESSION['sid'] = $sid;
$_SESSION['logged'] = "yes";
$stmt -> close();
header("Location: index.php");
}
else
{
echo "Wrong Username/Password Combo";
}
}
}
login.php:
<?php
if(isset($_POST['sid'])) {
$sid = $_POST['sid'];
include "functions.php";
session_start();
check_login();
}
else {
echo ' <form role="form" action="login.php" method="post">
<div class="form-group"><label for="sid">Student ID</label>
<input type="text" class="form-control" id="sid" name="sid" placeholder="Enter Student ID"></div>
<button type="submit" class="btn btn-default">Login</button>
</form>';
}
P.S。很多echo和html只是剪辑它,我只是不想粘贴整个HTML回声。
答案 0 :(得分:2)
$sid
超出了check_login()
功能的范围。要使其在范围内,您需要将其作为参数传递:
check_login($sid);
您也可以在函数内部使用global
关键字,但不鼓励编程错误。
答案 1 :(得分:1)
您需要使用$ sid作为参数声明check_login函数
function check_login($sid){
...
}
然后在调用时传入值
$sid = $_POST['sid'];
include "functions.php";
session_start();
check_login($sid);