PHP页面保护

时间:2017-01-17 17:01:01

标签: php html database

您好我正在开发这个网络应用程序,并且我有两个登录表格,供教师使用'和学生们#39;因此,当登录时,教师被重定向到home_teacher.php,学生被重定向到home.php。

基本上我必须使用表格,如果可能的话,你可以帮助我只让学生和home_teacher.php访问home.php。

这是登录表单:

<?php
session_start();
require_once("class.user.php");
$login = new USER();


if(isset($_POST['btn-login']))
{
    $uname = strip_tags($_POST['txt_uname_email']);
    $umail = strip_tags($_POST['txt_uname_email']);
    $upass = strip_tags($_POST['txt_password']);

    if($login->doLogin($uname,$umail,$upass))
    {
        $login->redirect('home.php');
    }
    else
    {
        $error = "Emaili ose fjalëkalimi ishin gabim. Ju lutem provoni përsëri";
    }
}

    if(isset($_POST['btn-login-t']))
    {
        $t_uname = strip_tags($_POST['t_txt_uname_email']);
        $t_umail = strip_tags($_POST['t_txt_uname_email']);
        $t_upass = strip_tags($_POST['t_txt_password']); 

        if($login->doLogin_teacher($t_uname,$t_umail,$t_upass))
        {
            $login->redirect('home_teacher.php');
        }
        else
        {
            $error = "Emaili ose fjalëkalimi ishin gabim. Ju lutem provoni përsëri";
        }
    }
?>
<link href="img/favicon.png" rel="shortcut icon" />
<!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>IB-Learning</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" href="style.css" type="text/css"  />
<link href="https://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Anton" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Hammersmith+One" rel="stylesheet">
</head>
<body>

<div class="signin-form" style="font-family: Bree Serif;">

    <div class="container" style="width: 500px;">


       <form class="form-signin" method="post" id="login-form">

        <h2 class="form-signin-heading" style="font-family: Bree Serif;">Student? Kyquni këtu!</h2><hr />




        <div class="form-group" style="font-family: Bree Serif;">
            <input type="text" class="form-control" name="txt_uname_email" placeholder="Username ose Emaili juaj" required />
             <span id="check-e"></span>
          </div>

            <div class="form-group" style="font-family: Bree Serif;">
            <input type="password" class="form-control" name="txt_password" placeholder="Fjalëkalimi juaj" />
        </div>

        <hr />

        <div class="form-group" style="font-family: Bree Serif;">
            <button type="submit" name="btn-login" class="btn btn-default">
                    <i class="glyphicon glyphicon-log-in"></i> &nbsp; Kyquni
            </button>
        </div>  
        <br />
                  <label style="font-family: Bree Serif;">Nuk jeni regjistruar ende? <a href="sign-up_student.php">Regjistrohuni këtu dhe filloni të mësoni!</a></label>
                  <hr /></form><br></div>
 <div id="error" style="font-family: Bree Serif;width: 470px;
    text-align: center;
    margin-left: 269px;">
        <?php
            if(isset($error))
            {
                ?>
                <div class="alert alert-danger" style="color: darkred;">
                   <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?> !
                </div>
                <?php
            }
        ?>
        </div>
<div class="container" style="width: 500px;font-family: Bree Serif; margin-bottom: 107px;">
     <form class="form-signin" method="post" id="login-form-teacher">

        <h2 class="form-signin-heading" style="font-family: Bree Serif;">Mësimdhënës? Kyquni këtu!</h2><hr />

 <div class="form-group" style="font-family: Bree Serif;">
            <input type="text" class="form-control" name="t_txt_uname_email" placeholder="Username ose Emaili juaj" required />
             <span id="check-e"></span>
          </div>

            <div class="form-group" style="font-family: Bree Serif;">
            <input type="password" class="form-control" name="t_txt_password" placeholder="Fjalëkalimi juaj" />
        </div>

        <hr />

        <div class="form-group" style="font-family: Bree Serif;">
            <button type="submit" name="btn-login-t" class="btn btn-default">
                    <i class="glyphicon glyphicon-log-in"></i> &nbsp; Kyquni
            </button>
        </div>  
        <br />
            <label style="font-family: Bree Serif;">Dëshironi të ndihmoni studentët më mësimet tuaja? <a href="sign-up_teacher.php">Regjistrohuni këtu!</a></label>

      </form>

    </div>

</div>

</body>
</html>

这是我的课程和功能:

<?php

require_once('dbconfig.php');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }


    public function register($uname,$umail,$upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO tik_students(user_name,user_email,user_pass) 
                                                       VALUES(:uname, :umail, :upass)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }
    public function register_teacher($t_uname,$t_umail,$t_upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO tik_teachers(user_name,user_email,user_pass)
                                                       VALUES(:uname, :umail, :upass)");

            $stmt->bindparam(":uname", $t_uname);
            $stmt->bindparam(":umail", $t_umail);
            $stmt->bindparam(":upass", $t_new_password);                              

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }

    public function doLogin($uname,$umail,$upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM tik_students WHERE user_name=:uname OR user_email=:umail ");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['user_pass']))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function doLogin_teacher($t_uname,$t_umail,$t_upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM tik_teachers WHERE user_name=:uname OR user_email=:umail ");
            $stmt->execute(array(':uname'=>$t_uname, ':umail'=>$t_umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($t_upass, $userRow['user_pass']))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

        public function submit_video($video_title,$video_code)
    {
        try
        {

            $stmt = $this->conn->prepare("INSERT INTO videos(video_title, video_code) 
                                                       VALUES(:video_title, :video_code)");

            $stmt->bindparam(":video_title", $video_title);
            $stmt->bindparam(":video_code", $video_code);       

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }


    public function is_t_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
            return true;
        }
    }


    public function is_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
            return true;
        }
    }
    public function redirect($url)
    {
        header("Location: $url");
    }

    public function doLogout()
    {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }
}

?>

这些是教师主页和学生主页: home.php

<?php

    require_once("session.php");

    require_once("class.user.php");
    $auth_user = new USER();


    $user_id = $_SESSION['user_session'];

    $stmt = $auth_user->runQuery("SELECT * FROM tik_students WHERE user_id=:user_id");
    $stmt->execute(array(":user_id"=>$user_id));

    $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

?>
<link href="img/favicon.png" rel="shortcut icon" />
<!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 name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Cookie">
    <link rel="stylesheet" href="assets/css/user.css">
    <link rel="stylesheet" href="assets/bootstrap/fonts/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patua+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Anton" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Hammersmith+One" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
<link rel="stylesheet" href="style.css" type="text/css"  />
<title>Mirë se erdhet - <?php print($userRow['user_email']); ?></title>
</head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<body>

<nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" style="font-family: Bree Serif;" href="index.php">IB-Learning </a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">

            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
              <span class="glyphicon glyphicon-user"></span>&nbsp;<?php echo $userRow['user_name']; ?>&nbsp;<span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="profile.php"><span class="glyphicon glyphicon-user"></span>&nbsp;Profili</a></li>
                <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span>&nbsp;Dilni</a></li>
              </ul>
            </li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>


    <div class="clearfix"></div>

<div class="container-fluid" style="margin-top:80px;">

    <div class="container">

        <label class="h5">Mirë se vini <h3 id="usernameb" style="font-family: Bree Serif"><?php print($userRow['user_name']); ?></h3></label>
    </div>

</div>
<div class="wanna" style="text-align: center;">
<h3 style="font-family: Hammersmith One;">Çfarë dëshironi të mësoni sot?</h3></div>

<div class="button1" style="margin-left: -35px;
   margin-top: 40px;">
 <div class="top" style="padding-right: 60px;"> 
   <a href="tik_m.php" class="buttonfizike" style="text-decoration: none;padding-left:156px; text-align: center;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;padding-left: 26px;padding-right:26px;">
       <h1 style="color: white; font-family: Patua One;">TIK</h1>
              </button>
   </a>

<a href="matematike.php" class="buttonfizike" style="text-decoration: none; text-align: center; padding-left: 20px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;"><h1 style="color: white; font-family: Patua One;">Matematikë</h1></button></a>
<a href="gjuheshqipe.php" class="buttonfizike" style="text-decoration: none; text-align: center;padding-left: 20px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;"><h1 style="color: white; font-family: Patua One;">Gjuhë Shqipe</h1></button></a>
<a href="anglisht.php" class="buttonfizike" style="text-decoration: none; text-align: center;padding-left: 20px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;"><h1 style="color: white; font-family: Patua One;">Anglisht</h1></button></a><br>
</div>
<div class="bottom" style="padding-top: 25px;">
<a href="kimi.php" class="buttonfizike" style="text-decoration: none; text-align: center;padding-left:156px; padding-top: 30px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;padding-left: 30px;
    padding-right: 30px;"><h1 style="color: white; font-family: Patua One;">Kimi</h1></button></a>
<a href="fizike.php" class="buttonfizike" style="text-decoration: none; text-align: center;padding-left: 20px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;padding-right: 30px;
    padding-left: 30px;"><h1 style="color: white; font-family: Patua One;">Fizikë</h1></button></a>
<a href="gjeografi.php" class="buttonfizike" style="text-decoration: none; text-align: center;padding-left: 20px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;    padding-right: 30px;
    padding-left: 30px;"><h1 style="color: white; font-family: Patua One;">Gjeografi</h1></button></a>
<a href="biologji.php" class="buttonfizike" style="text-decoration: none; text-align: center;padding-left: 20px;"><button style="border: none; background-color: dodgerblue; border-radius: 5px;padding-right: 33px;
    padding-left: 32px;"><h1 style="color: white; font-family: Patua One;">Biologji</h1></button></a>
</div>
</div>
<?php include('footer.php');?>
<script src="bootstrap/js/bootstrap.min.js"></script>

</body>
</html>

home_teacher.php

<?php 

  require_once("session.php");

  require_once("class.user.php");
  $auth_user = new USER();


  $user_id = $_SESSION['user_session'];

  $stmt = $auth_user->runQuery("SELECT * FROM tik_teachers WHERE user_id=:user_id");
  $stmt->execute(array(":user_id"=>$user_id));

  $userRow=$stmt->fetch(PDO::FETCH_ASSOC);



 ?>
<link href="img/favicon.png" rel="shortcut icon" />
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Cookie">
<link rel="stylesheet" href="css/user.css">
<link rel="stylesheet" href="bootstrap/fonts/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patua+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Anton" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Hammersmith+One" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
<link rel="stylesheet" href="style.css" type="text/css"  />
<link rel="stylesheet" type="text/css" href="css/user.css">
<title>Ngarkoni video!</title>
</head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<body>

<nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" style="
          font-family: Bree Serif;">IB-Learning </a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">

            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
              <span class="glyphicon glyphicon-user"></span>&nbsp;&nbsp;<span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="profile.php"><span class="glyphicon glyphicon-user"></span>&nbsp;Profili</a></li>
                <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span>&nbsp;Dilni</a></li>
              </ul>
            </li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>


    <div class="clearfix"></div>

<div class="container-fluid" style="margin-top:80px;">


</div>
<div class="wanna">

</div>

<?php include('footer.php');?>
<script src="bootstrap/js/bootstrap.min.js"></script>
<style type="text/css">
  h3{
    font-family: Bree Serif;
    text-align: center;
    padding-left: 20px;
  }
</style>
</body>
</html>

这是我的数据库配置:

<?php
class Database
{   
    private $host = "localhost";
    private $db_name = "tik";
    private $username = "root";
    private $password = "";
    public $conn;

    public function dbConnection()
    {

        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>

我真的需要这个工作所以感谢帮助!!!!!!♥

2 个答案:

答案 0 :(得分:0)

你可以像JustOnUnderMillions那样说,但我认为如果会话变量是$ _SESSION [user_type] =&#39; t&#39;会更好。使用&#39;#39;对于老师和&#39;为学生。然后,如果此变量是例如&#39;。

,则可以提供对相应页面的访问权限

我实际上不会将用户存储在两个不同的表中,您可以使用单个表&#39;用户&#39;以及使用user_type列表示&#39; t(#teacher)或& #39; S&#39;对于学生,然后通过从DB获取用户,您可以检查它的类型并允许或拒绝访问页面。

答案 1 :(得分:0)

通过设置新的会话变量来更改教师的登录功能:

 public function doLogin_teacher($t_uname,$t_umail,$t_upass)
        {
            try
            {
                $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM tik_teachers WHERE user_name=:uname OR user_email=:umail ");
                $stmt->execute(array(':uname'=>$t_uname, ':umail'=>$t_umail));
                $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
                if($stmt->rowCount() == 1)
                {
                    if(password_verify($t_upass, $userRow['user_pass']))
                    {
                        $_SESSION['teacher_session'] = $userRow['user_id'];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();
            }
        }

当您登录教师时,请在home_teacher.php中更改此内容:

  $user_id = $_SESSION['teacher_session'];

  $stmt = $auth_user->runQuery("SELECT * FROM tik_teachers WHERE user_id=:user_id");
  $stmt->execute(array(":user_id"=>$user_id));

我不建议为用户类型使用两个单独的表。例如,将所有用户放入一个表中并创建一个新列。学生的列值可以是0,教师的列值可以是1。