日期:2014-05-17  浏览次数:20413 次

PHP登录跳转
<?php
session_start();
include("config.php");//连接数据库
$username=$_POST['user'];
$word=$_POST['password'];
$userword=md5(trim($word));//MD5转换密码
$id=$_POST['user_id'];
if($id=="student")
{
$result_psword=mysql_query("select S_PS from STUDENT where S_ID='$username'");
/*while ($rows=mysql_fetch_array($result_psword)){}*/
if(!$result_psword)
{
echo "用户不存在,请先注册";
echo "<script>setTimeout(function(){window.location.href='../regist.php'}, 2000);</script>";
exit;
}
else if($result_psword==$userword)
{

$_SESSION['login']='学生';
echo "<script>window.location.href='../student/student.php';</script>";
}
}
登录后发现没有跳转,直接停在login.php页面里。
$username,$userword,$id都测试了有传进去值,我想就是
$result_psword=mysql_query("select S_PS from STUDENT where S_ID='$username'");
这句的问题了。
$reslt_psword我想测试里面的值都测试不出来,这个里面到底包含着什么东西?
($result_psword==$userword)这句话直接判断为什么会不行?
做毕设中,分不敢给多啊··留点以后用。

------解决方案--------------------
PHP code
<?php
session_start();
include("config.php");//连接数据库
$username=$_POST['user'];
$word=$_POST['password'];
$userword=md5(trim($word));//MD5转换密码
$id=$_POST['user_id'];
if($id=="student")
{
$result_psword = mysql_query("select S_PS from STUDENT where S_ID='$username'");
if(! $result_psword) 
   echo "mysql error message:". mysql_error();

$row = mysql_fetch_assoc($result_psword);

   if($rows) {
      echo "用户不存在,请先注册";
      echo "<script>setTimeout(function(){window.location.href='../regist.php'}, 2000);</script>";
      exit;
   }
   else if($row['S_PS'] == $userword ) {
      $_SESSION['login']='学生';
      header("Location: ../student/student.php");
   }else
      echo "用户密码错误";
}
else
   echo "id {$id} 不是 student";

------解决方案--------------------
mysql_query()返回的是资源集。因此不能那么判断。
if(!$result_psword)
{
echo "用户不存在,请先注册";
echo "<script>setTimeout(function(){window.location.href='../regist.php'}, 2000);</script>";
exit;
}
==》改为:
if(!mysql_num_rows($result_psword))
{
echo "用户不存在,请先注册";
echo "<script>setTimeout(function(){window.location.href='../regist.php'}, 2000);</script>";
exit;
}
------解决方案--------------------
http://dev.mysql.com/doc/refman/5.1/zh/index.html
------解决方案--------------------
判断逻辑写反了
你的
if(!$_SESSION['login']||$_SESSION['login']!='管理员'||$_SESSION['login']!='教师'||$_SESSION['login']!='学生')
表示只要有有一个成立就进入
但是,比如当 $_SESSION['login'] = '管理员' 时,
$_SESSION['login']!='教师' 和 $_SESSION['login']!='学生'
都会成立的
应写作

if(!$_SESSION['login'] || ($_SESSION['login']!='管理员' && $_SESSION['login']!='教师' && $_SESSION['login']!='学生'))