2、登录检测页 login.php
-
- @mysql_connect("localhost", "root","1981427") //选择数据库之前需要先连接数据库服务器
- or die("数据库服务器连接失败");
- @mysql_select_db("test") //选择数据库mydb
- or die("数据库不存在或不可用");
- //获取用户输入
- $username = $_POST['username'];
- $passcode = $_POST['passcode'];
- $cookie = $_POST['cookie'];
- //执行SQL语句
- $query = @mysql_query("select username, userflag from users "
- ."where username = '$username' and passcode = '$passcode'")
- or die("SQL语句执行失败");
- //判断用户是否存在,密码是否正确
- if($row = mysql_fetch_array($query))
- {
- if($row['userflag'] == 1 or $row['userflag'] == 0) //判断用户权限信息是否有效
- {
- switch($cookie) //根据用户的选择设置cookie保存时间
- {
- case 0: //保存Cookie为浏览器进程
- setcookie("username", $row['username']);
- break;
- case 1: //保存1天
- setcookie("username", $row['username'], time()+24*60*60);
- break;
- case 2: //保存30天
- setcookie("username", $row['username'], time()+30*24*60*60);
- break;
- case 3: //保存365天
- setcookie("username", $row['username'], time()+365*24*60*60);
- break;
- }
- header("location: main.php"); //自动跳转到main.php
- }
- else
- {
- echo "用户权限信息不正确";
- }
- }
- else
- {
- echo "用户名或密码错误";
- }
- ?>
复制代码
3. Login successful verification page
-
- session_start();
- if(isset($_COOKIE['username']))
- {
- @mysql_connect("localhost", "root","1981427") //Select You need to connect to the database server before the database
- or die("Database server connection failed");
- @mysql_select_db("test") //Select the database mydb
- or die("The database does not exist or is unavailable");
- //Get Session
- $username = $_COOKIE['username'];
- //Execute the SQL statement to obtain the value of userflag
- $query = @mysql_query("select userflag from users "
- ."where username = '$username'")
- or die("SQL statement execution failed");
- $row = mysql_fetch_array($query);
- //Obtain user permission information
- $flag = $row['userflag'];
- //Output different welcomes based on the value of userflag Information
- if($flag == 1)
- echo "Welcome administrator".$_SESSION['username']."Log in to the system";
- if($flag == 0)
- echo "Welcome user".$_SESSION[ 'username']."Log in to the system";
- echo "Logout";
- }
- else
- {
- echo "You You do not have permission to access this page";
- }
- ?>
Copy code
4. Log out and log in
-
- setcookie("username");
- echo "Logout successful";
- ?>
Copy code
|