PHP Record the number of login users

Function to record how many times the user logs in

Connect to the database first:
you can create a new php file :
The following code:

php file name is conn.php
<?php
    header("Content-type:text/html;charset=utf-8");
    $conn = mysql_connect('localhost','root','') or die('Server connection failed');
    mysql_select_db('database name') or die('database nonexistence');;
    mysql_query('set names utf-8');
?>

you can create a new php file :
The following code:

php file name is login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>login</title>
</head>

<body>
    <form action="logindo.php" method="post">
       users: <input name="name" type="text"/><br/>
       cipher:<input name="password" type="password"/></br>
       <input type="submit" name="sub" value="submit"/>
    </form>
</body>
</html>

you can create a new php file :
The following code:

file named :logindo.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>login handling</title>
</head>

<body>
<?php
    if(isset($_POST['sub'])){
       include('conn.php');
       $name = $_POST['name'];
       $password = $_POST['password'];
       $sql = "select * from tb_name where name = '$name' and password = '$password'";
       $r = mysql_query($sql);
       if($row = mysql_fetch_array($r)){
           session_start();
           $_SESSION['name']=$row['name'];
           $ac = 0;
           if(file_exists("acc.txt")){
               $ac = file_get_contents("acc.txt");
           }
           $ac++;
           file_put_contents("acc.txt",$ac);
           echo '<script>alert('login success');location.href="index.php";</script>';
           }else{
           echo '<script>alert("user name or password error");location.href="login.php";</script>';
           }
        }else{
        echo '<script>alert("user name or password error");location.href="login.php";</script>';
     }else{
     echo '<script>alert("user name or password error");location.href="login.php";</script>';
     }
?>  
</body>
</html>

you can create a new php file :
The following code:

index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User registration login procedure</title>
</head>

<body>
<?php
  session_start();
  if(isset($_SESSION['name'])){
    echo 'welcome'.$_SESSION['name'];
    echo "<br>";
    $ac = file_get_contents("acc.txt");
    echo "you are".$ac."visitor";
    }else{
    echo "<script>alert('please login');location='loginl.php';</script>";
    }
?>
<a href="exit.php">Logged out</a>
</body>
</html>

you can create a new php file :
The following code:

file name is exit.php
<?php
  header("Content-type:text/html;charset=utf-8");
  session_start();
  unset($_SEEION['name']);
  echo '<script>alert("exit the success");location="login.php";</script>';
?>

The datadase is as follows:

create database (database);
use (database);
create table (table_name);
create table 'table_name'  ( 'id' int(4) not null auto_increment primary key,'name' varchar(50) character set utf8 collate utf8_unicode_ci not null,'password' varchar(50) character set utf8 collate utf8_unicode_ci not null,'createtime' datetime not null ) engine = myisan character set utf8 collate utf8_unicode_ci;

welcome to view

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
原文地址:https://www.cnblogs.com/dashucoding/p/11932785.html