一个登陆界面有3个ID(学生,教师,管理员),发送表单信息到login.php
在login.php中进行身份的判断,然后登录到不同界面。session在这之间要怎么使用,
一开始开启session会话,然后$_SESSION['login']=ID,
红色字体那语句是干嘛的?跳转到相应的页面之后,session该怎么用?有什么用?什么时候结束这个会话?
<?php
session_start();
include("config.php");//连接数据库
$username=$_POST[user];
$word=$_POST[password];
$userword=md5(trim($word));//MD5转换密码
$id=$_POST[uer_id];
if($id=="学生")
{
$result_psword=mysql_query("select S_PS from STUDENT where S_ID=$username"); if(!$result_psword)
{
echo "用户不存在,请先注册";
echo "<script>window.location.href='../regist.php';</script>";
exit;
}
else if($result_psword==$userword)
{
$_SESSION['login']='学生';
echo "<script>window.location.href='../student/student.php';</script>";
}
}
else if ($id=="教师")
{
$result1_psword=mysql_query("select T_PS from STUDENT where T_ID=$username");
if(!$result1_psword)
{
echo "权限不够或教师不存在";
echo "<script>window.location.href='../index.php';</script>";
exit;
}
else if ($result1_psword==$userword)
{
$_SESSION['login']='教师';
echo "<script>window.location.href='../teacher/teacher.php';</script>";
}
}
else if ($id=="管理员")
{
$result2_psword=mysql_query("select password from ADMIN where name=$username");
if(!$result2_psword)
{
echo "权限不够或管理员不存在";
echo "<script>window.location.href='../index.php';</script>";
exit;
}
else if ($result2_psword==$userword)
{
$_SESSION['login']='管理员';
echo "<script>window.location.href='../admin.php';</script>";
}
}
mysql_close($db);
?>
在login.php中进行身份的判断,然后登录到不同界面。session在这之间要怎么使用,
一开始开启session会话,然后$_SESSION['login']=ID,
红色字体那语句是干嘛的?跳转到相应的页面之后,session该怎么用?有什么用?什么时候结束这个会话?
<?php
session_start();
include("config.php");//连接数据库
$username=$_POST[user];
$word=$_POST[password];
$userword=md5(trim($word));//MD5转换密码
$id=$_POST[uer_id];
if($id=="学生")
{
$result_psword=mysql_query("select S_PS from STUDENT where S_ID=$username"); if(!$result_psword)
{
echo "用户不存在,请先注册";
echo "<script>window.location.href='../regist.php';</script>";
exit;
}
else if($result_psword==$userword)
{
$_SESSION['login']='学生';
echo "<script>window.location.href='../student/student.php';</script>";
}
}
else if ($id=="教师")
{
$result1_psword=mysql_query("select T_PS from STUDENT where T_ID=$username");
if(!$result1_psword)
{
echo "权限不够或教师不存在";
echo "<script>window.location.href='../index.php';</script>";
exit;
}
else if ($result1_psword==$userword)
{
$_SESSION['login']='教师';
echo "<script>window.location.href='../teacher/teacher.php';</script>";
}
}
else if ($id=="管理员")
{
$result2_psword=mysql_query("select password from ADMIN where name=$username");
if(!$result2_psword)
{
echo "权限不够或管理员不存在";
echo "<script>window.location.href='../index.php';</script>";
exit;
}
else if ($result2_psword==$userword)
{
$_SESSION['login']='管理员';
echo "<script>window.location.href='../admin.php';</script>";
}
}
mysql_close($db);
?>
../student/student.php
../teacher/teacher.php
../admin.php
那么在这三个程序中要分别加以验证,以防他人直接进入$dict = array(
'student.php' => '学生',
'teacher.php' => '教师',
'admin.php' => '管理员',
);
session_start();
if(!$_SESSION['login'] || $_SESSION['login'] != $dict[basename($_SERVER['PHP_SHLF'])]) {
echo '你无权进入此页面';
exit;
}
进入相应页面后,也许会用到session全局变量,如登录后存储用户名:$_SESSION['user']=$username;
该全局变量可以在任何页面调用:session_start();echo "username-->>".$_SESSION['user'];
当页面关闭时,session会自动销毁。session_destroy();
相关应用,请参考php手册:session会话处理函数。
http://download.csdn.net/detail/dmtnewtons/4122897
session_start();??