php利用cookie自动登入方法

利用cookie自动登录方法,后端PHP代码如下,html前端页面代码如下,密码验证实现登录,利用cookie登录方法,hr noshade width=”98%” size=”1″&gt,&gt

php教程应用cookie自动登陆方法

本文实例呈报了php利用cookie完毕机关登入的措施。分享给大家供大家参考。具体完毕格局如下:

<?php教程
   function authenticate_user() {
      header(‘WWW-Authenticate: Basic realm=”Secret Stash”‘);
      header(“HTTP/1.0 401 Unauthorized”);
      exit;
   }

   if (! isset($_SERVER[‘PHP_AUTH_USER’])) {
      authenticate_user();
   } else {
     
mysql教程_pconnect(“localhost”,”authenticator”,”secret”)
or die(“Can’t connect to database server!”);
     
mysql_select_db(“java2s”) or
die(“Can’t select authentication database!”);

      $query = “SELECT username,
ps教程wd FROM user WHERE
username=’$_SERVER[PHP_AUTH_USER]’ AND
pswd=MD5(‘$_SERVER[PHP_AUTH_PW]’)”;

      $result = mysql_query($query);

      // If nothing was found, reprompt the user for the login
www.111cn.net information.
      if (mysql_num_rows($result) == 0) {
         authenticate_user();
      }
   }
?>

<body bgcolor=”#FFFFFF” background=”image/bg.gif”>
<table width=”90%” border=”1″ cellspacing=”0″ cellpadding=”0″
align=”center” bordercolorlight=”#FFCC99″
bordercolordark=”#FFCC99″>
  <tr bgcolor=”#CCFF99″>  
    <td>
      <div
align=”center”><b>修改材质登陆</b></div>
    </td>
  </tr>
  <tr align=”center”>  
    <td>
      <form method=”post” action=”modify_l.php”>
        <table width=”70%” border=”0″ cellspacing=”0″
cellpadding=”0″>
          <tr>
            <td width=”38%” align=”right”>用户名:</td>
            <td width=”62%” align=”left”>
              <input type=”text” name=”name” size=”17″
maxlength=”16″>
            </td>
          </tr>
          <tr>
            <td width=”38%” align=”right”>密码:</td>
            <td width=”62%” align=”left”>
              <input type=”password” name=”password” size=”17″
maxlength=”16″>
            </td>
          </tr>
        </table>
        <input type=”submit” name=”b1″ value=”进入”>
      </form>
    </td>
  </tr>
  <tr bgcolor=”#CCFF99″>  
    <td>
      <div
align=”center”><b>论坛新闻</b></div>
    </td>
  </tr>
  <tr valign=”top”>  
    <td>  
<?php include “info.php”;?>
    </td>
  </tr>
</table>
</body>
</html>
<?php
}
?>
## oper.php
<?php
require “func.php”;
if (adminok()) {
    if (isset($add_board)) {
        if (isset($name) and isset($chinesename) and
!is_board_exits($name)) {
            create_board_table($name,$chinesename);
        }
        else {
            echo “出现谬误!(恐怕版面已经存在)”;
            $founderr=1;
        }
    }
    elseif (isset($del_board)) {
        if (isset($board)) {
            drop_board_table($board);
        }
        else {
            echo “出现错误!(请选用要刨除的版面)”;
            $founderr=1;
        }
    }
    elseif (isset($modify)) {
        if (isset($name) and isset($chinesename)) {
            update_board($name,$chinesename);
        }
        else {
            echo “出现谬误!(请填入完整)”;
            $founderr=1;
        }
    }
    elseif (isset($add_admin)) {
        if (is_user_exits($name) and isset($board)) {
            add_admin($board,$name);
        }
        else {
            echo “现身谬误!(极有十分大概率是用户空中楼阁)”;
            $founderr=1;
        }
    }
    elseif (isset($delete_admin)) {
        if (is_user_exits($name)) {
            del_admin($name);
        }
        else {
            echo “出现错误!(极有相当大可能率是用户不设有)”;
            $founderr=1;
        }
    }
    else {
        echo “你要怎么?”;
        $founderr=1;
    }
    if (!$founderr) {
        redirect2(“admin.php”);
    }
}
else {
    show_error(1);
}
?>

<html>
  <head>
  <title>enter password</title>
  </head>
  <body>
  <form name=”forml” method=”post”
action=”cookiebasedpasswordlogin.php”>
    <table>
      <tr>
       <td colspan=”2″ >
         <div align=”center”><b>please specify the
password</b></div>
       </td>
     </tr>
   <tr>>
     <td>
       <div align=”right”>customer id</div>
     </td>
     <td>
       <input type=”text” name=”username”>
     </td>
   </tr>
   <tr>
     <td>
       <div align=”right”>password</div>
     </td>
     <td>
       <input type=”password” name=”password”>
     </td>
   </tr>
   <tr>
     <td colspan=”2″>
       <center>
         <input type=”submit” name=”submit” value=”login”>
       </center>
     </td>
    </tr>
   </table>
  </form>
  </body>
  </html>
 
 
 
 
<!– cookiebasedpasswordlogin.php
<?php
    $now = getdate();
    $storetime= $now[“weekday”] . ” ” . $now[“month”] .” ” .
$now[“year”] ;
    $storetime.=” time : “;

    if ($now[“hours”] < 10) {
      $storetime.= “0” . $now[“hours”];
    } else {
      $storetime.= $now[“hours”];
    }
 
    $storetime.= “:”;
    if ($now[“minutes”]<10) {
      $storetime.= “0” . $now[“minutes”];
    } else {
      $storetime.= $now[“minutes”];
    }
   
    $storetime.= “: “;
    if ($now[“seconds”] <10) {
      $storetime.= “0” . $now[“seconds”];
    } else {
      $storetime.= $now[“seconds”];
    }
    if (isset($data)) {
       $counter=++$data[l];
        setcookie(“data[0]”,$storetime,time() + (60*60*24));
        setcookie(“data[l]”, $counter,time() + (60*60*24));
setcookie(“data[2]”,$username,time() + (60*60*24));
        echo “<b><center>hi ” . $data[2] . ” !
!</center></b><br>n”;
        echo “<b><center>last login time :” .$data[0] .
“</center></b><br>n”;
        echo “<b><center>current date :” .$storetime.
“</center></b><br>n”;
        echo “<b><center>page view count :” . $data[l].
“</center></b><br>n”;
        echo “<b><center>you have successfully logged
in!</center></b>”;
        echo (“<b><contor>you can access this area without
entering a password for the next 24
hours!</center></b>”);
   } else {
    if (isset($username) && isset($password)) {
     if ($password==”superpass”) {
          $counter=0;
          setcookie(“data[0]”,$storetime,time() + (60*60*24));
          setcookie(“data[l]”,$counter,time() + (60*60*24));
          setcookie(“data[2]”,$username,time() + (60*60*24));
          $url=”location: cookieimp.php”;
          header($url);
     }else{
          echo “<hl><center>invalid
password!!!</center></hl>”;
     }
    }
  }
  ?> 

后端PHP代码如下:

<html>
  <head>
  <title>Enter Password</title>
  </head>
  <body>
  <form name=”forml” method=”POST”
action=”CookieBasedPasswordLogin.php”>
    <table>
      <tr>
       <td colspan=”2″ >
         <div align=”center”><b>Please Specify the
Password</b></div>
       </td>
     </tr>
   <tr>>
     <td>
       <div align=”right”>Customer ID</div>
     </td>
     <td>
       <input type=”text” name=”username”>
     </td>
   </tr>
   <tr>
     <td>
       <div align=”right”>Password</div>
     </td>
     <td>
       <input type=”password” name=”password”>
     </td>
   </tr>
   <tr>
     <td colspan=”2″>
       <center>
         <input type=”submit” name=”Submit” value=”Login”>
       </center>
     </td>
    </tr>
   </table>
  </form>
  </body>
  </html>
 
 
 
 
<!– CookieBasedPasswordLogin.php
<?php
    $now = getdate();
    $storetime= $now[“weekday”] . ” ” . $now[“month”] .” ” .
$now[“year”] ;
    $storetime.=” Time : “;

    if ($now[“hours”] < 10) {
      $storetime.= “0” . $now[“hours”];
    } else {
      $storetime.= $now[“hours”];
    }
 
    $storetime.= “:”;
    if ($now[“minutes”]<10) {
      $storetime.= “0” . $now[“minutes”];
    } else {
      $storetime.= $now[“minutes”];
    }
   
    $storetime.= “: “;
    if ($now[“seconds”] <10) {
      $storetime.= “0” . $now[“seconds”];
    } else {
      $storetime.= $now[“seconds”];
    }
    if (isset($data)) {
       $counter=++$data[l];
        setcookie(“data[0]”,$storetime,time() + (60*60*24));
        setcookie(“data[l]”, $counter,time() + (60*60*24));
setcookie(“data[2]”,$username,time() + (60*60*24));
        echo “<b><center>Hi ” . $data[2] . ” !
!</center></b><br>n”;
        echo “<b><center>Last Login Time :” .$data[0] .
“</center></b><br>n”;
        echo “<b><center>Current Date :” .$storetime.
“</center></b><br>n”;
        echo “<b><center>Page View Count :” . $data[l].
“</center></b><br>n”;
        echo “<b><center>You have successfully logged
in!</center></b>”;
        echo (“<b><contor>You can access this area without
entering a password for the next 24
hours!</center></b>”);
   } else {
    if (isset($username) && isset($password)) {
     if ($password==”superpass”) {
          $counter=0;
          setcookie(“data[0]”,$storetime,time() + (60*60*24));
          setcookie(“data[l]”,$counter,time() + (60*60*24));
          setcookie(“data[2]”,$username,time() + (60*60*24));
          $url=”Location: cookieimp.php”;
          header($url);
     }else{
          echo “<hl><center>INVALID
PASSWORD!!!</center></hl>”;
     }
    }
  }
  ?>
 
 

## manage_read.php
<?php
require (“func.php”);
if (check_admin_password()) {
    $board=$jl_forum[board];
    switch ($job){
        case “setgood”:
            set_good($jl_forum[board],$uid);
            $work=1;
            break;
        case “setnogood”:
            set_no_good($jl_forum[board],$uid);
            $work=1;
            break;
        case “delete”:
            del_doc($jl_forum[board],$uid);
            $work=1;
            break;
    }
if (!$word) {
    add_one_hit($board,$id);
}
$sql=”select * from $board where id=$id”;
$sql_result=mysql_query($sql);
$sum=mysql_num_rows($sql_result);
$sql_row=mysql_fetch_array($sql_result);
$title=$sql_row[title];
$writer=$sql_row[writer];
$cont=$sql_row[cont];
$writetime=$sql_row[writetime];
$hits=$sql_row[hits];
$good=$sql_row[good];
$rootid=$sql_row[id];
$fromip=$sql_row[fromip];
$sql=”select * from $board where slaveid=$id”;
$sql_result=mysql_query($sql);
if ($sum<>0) {
?>
<html>
<head>
<title><?php echo $title;?></title>
<meta http-equiv=”Content-Type” content=”text/html;
charset=gb2312″>
<link rel=”stylesheet” type=”text/css” href=”css/index.css”>
<script language=”javascript”>
function
sendmsg(name){                window.open(“sendmsg.php?name=”+name,””,”height=200,width=500,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no”);
}
</script>

html前端页面代码如下:

上边我们要讲到二种用户登入方法一种是行使window验证加数据库教程密码验证完结登入,前者是选拔cookie记录用户消息进行登陆,有亟待的爱人能够试下。

</head>