我创建了6个php文件teacher.php
,student.php
,login.php
,logout.php
,index.php
和config.php
我做了一些编码,它显示了:
username:
password
我可以输入我在数据库中注册的帐户名和密码。
但输入我的帐户名和密码或密码错误或输入后。只需按Enter键
显示空白页面,不显示任何内容。
它有什么问题,我错过了一些编码或错误吗?
很抱歉,因为我是php新手,希望你能给我一些建议:)
我认为主要问题在于login.php,这些是代码:
<?php
include "config.php";
if(isset($_POST['log'])){
$user = $_POST['user'];
$pass = md5($_POST['pass']);
$hsl = mysql_query("select * from login where user='$user' and pass='$pass'");
$data = mysql_fetch_array($hsl);
$username = $data['user'];
$password = $data['pass'];
$type = $data['type'];
$name = $data['name'];
if($user==$username && $pass=$password){
session_start();
$_SESSION['name']=$name;
if($type=='student'){
echo "<script>location.assign('student.php')</script>";
}else if($type=='teacher'){
echo "<script>location.assign('teacher.php')</script>";
} else{
echo "<script>location.assign('wrong page.php')</script>";
}
}
}
?>
答案 0 :(得分:0)
注意: -
mysql_ *在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除。 相反,应使用MySQLi或PDO_MySQL扩展名。
试试这段代码: -
// add this line always first
session_start();
include "config.php";
if(isset($_POST['log'])){
$user = $_POST['user'];
$pass = md5($_POST['pass']);
$hsl = mysql_query("select user,type,name from login where user='$user' and pass='$pass'");
if (!$hsl) {
// Debug query result by below code
//echo 'Could not run query: ' . mysql_error();
//exit;
echo '<script language="javascript">';
echo 'alert("Username / Password Seems Wrong !")';
echo '</script>';
}else{
$row = mysql_fetch_row($hsl);
$username = $row[0];
$type = $row[1];
$name = $row[2];
$_SESSION['name'] = $name;
if($type=='student'){
echo "<script>location.assign('student.php')</script>";
}else if($type=='teacher'){
echo "<script>location.assign('teacher.php')</script>";
} else{
echo "<script>location.assign('wrong page.php')</script>";
}
}
如果您想重定向到文件,请按以下方式更改您的条件: -
if($type=='student'){
//echo "<script>location.assign('student.php')</script>";
header("Location: student.php");
}else if($type=='teacher'){
//echo "<script>location.assign('teacher.php')</script>";
header("Location: teacher.php");
} else{
//echo "<script>location.assign('wrong page.php')</script>";
header("Location: wrong page.php");
}
}
希望它会对你有所帮助:)。