如何在php中创建自动注销脚本

时间:2013-05-02 18:50:25

标签: php mysql

我试图创建一个自动注销系统,当页面处于非活动状态时会自动退出系统,我是新手,当我将代码添加到我的登录页面时,我会收到这些警告

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\how are things\admin panel\login.php on line 80
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\how are things\admin panel\login.php on line 81

第80和81行的代码分别如下所示

  if($no_rows > 0){
                         while($resGetAdmin = mysql_fetch_assoc($rs_user))

这是我添加的代码

    $rs_user= mysql_query($check_login);
                 $no_rows   = mysql_num_rows($rs_user);
                 if($no_rows > 0){
                         while($resGetAdmin = mysql_fetch_assoc($rs_user)){
                          $selQuery="select * from  login where  user_id=".$resGetAdmin['user_id'] ." AND loginDate ='$current_date'";
                          $rgetData= mysql_query($selQuery);
                          $num_login   = getSqlNumber($selQuery);
                          if($num_login==0) {
                          $insQuery= "insert into login (user_id,loginTime,loginDate,count)   values (".$resGetAdmin['user_id'].",'$current_time','$current_date',1)";
                          $login_user= mysql_query($insQuery);
                          $insert_id=mysql_insert_id();
                          $_SESSION['loginTime']=$current_time;
                           }else{
                               $count = mysql_result($rgetData,0,’count’);
                               $count = $count+1;
                               $updateQuery= "update login set loginTime='$current_time' ,loginDate='$current_date' , count ='$count' where user_id=".$resGetAdmin['user_id'] ." AND loginDate='$current_date'";
                               $login_user= mysql_query($updateQuery) or mysql_error();
                               $_SESSION['loginTime']=$current_time;
                                }

                              $_SESSION['user_id']=$resGetAdmin['user_id'];
                              header("Location: index.php");
                                   exit;
                                  }
                            }

这是我在添加上面代码之前的原始登录页面

<?php
if(isset($_POST['submit'])){
     $username = $_POST['username'];
     $password =($_POST['password']);
     $hash_password = md5($password);
     $hash_password1 = sha1($hash_password);
     $hash_password2 = crypt($hash_password1,"st");
     $hash_password3 = hash("sha512",$hash_password2);

     $guess = $_POST['captcha'];
     $real = (isset($_SESSION['real'])) ? $_SESSION['real'] : "";
    if (empty($guess))
     { 
         die("Please enter the correct CAPTCHA.<br>");
     } 
if (!empty($guess) && $guess !== $real)
{
    die("Please enter the correct CAPTCHA.<br>");
}
     if(empty($username) or empty($password)){
         echo "<p>fields empty !</p>";
     }
     else {
          $check_login = mysql_query("SELECT id, type FROM users WHERE username='$username' AND password='$hash_password3'");
          if(mysql_num_rows($check_login) == 1) {
             $run = mysql_fetch_array($check_login);
             $user_id = $run['id'];
             $type = $run['type'];
             if($type == 'd'){
                  echo "<p> Your Account is deactivated byt the site admin!</p>";
             } else {
                  $_SESSION['user_id'] = $user_id;
                  header('location: index.php');
             }
          } else {
                  echo "<p> User Name or Password incorrect!</p>";

              }  }
}

?>

我将整个代码粘贴在登录页面上,但我离开了timeout.php页面,先谢谢你们了

2 个答案:

答案 0 :(得分:3)

无需通过编写自己的代码来执行此注销。每个会话都可以配置为仅持续一定时间。您可以设置maximum lifetime

答案 1 :(得分:0)

我的赌注是:

第80行:

$rs_user= mysql_query($check_login);

$ check_login 不是包含查询的字符串
$ rs_user 返回false ,因为查询失败。

(我的猜测是$ check_login是来自另一个查询的结果集)

* 警告:mysql_query()期望参数1为字符串,资源在C:\ xampp \ htdocs中给出\如何在第80行

的东西\ admin panel \ login.php >

第81行:

$no_rows   = mysql_num_rows($rs_user);

警告:mysql_num_rows()期望参数1为资源,在C:\ xampp \ htdocs中给出null。第81行上的事物\ admin panel \ login.php是怎样的

(mysql说是给出了NULL,这是因为$ rs_user返回false(参见上面对第80行的解释)