如何在一个标记中插入此php脚本并建立会话登录

如何在一个标记中插入此php脚本并建立会话登录,php,session,login,Php,Session,Login,我有一个php登录脚本,其中包含三个用户(Root、Secretary和accounter),这非常好 但正如你所看到的,每个帐户都有它的php标记,允许某些用户登录,我的目标是只使用一个php标记 <?php ....?> 对于这两个帐户,例如(登录必须检查用户名或密码是否不正确,他/她将被定向到同一个登录页面,消息将是红色文本的“插入正确的用户名或密码”),并且我想介绍PHP登录会话,这样,如果用户未登录,他/她将被定向到登录页面(index.PHP),我想避免试图绕过系统

我有一个php登录脚本,其中包含三个用户(Root、Secretary和accounter),这非常好

但正如你所看到的,每个帐户都有它的php标记,允许某些用户登录,我的目标是只使用一个php标记

<?php ....?>

对于这两个帐户,例如(登录必须检查用户名或密码是否不正确,他/她将被定向到同一个登录页面,消息将是红色文本的“插入正确的用户名或密码”),并且我想介绍PHP登录会话,这样,如果用户未登录,他/她将被定向到登录页面(index.PHP),我想避免试图绕过系统的用户

任何帮助,请,我只是新的php

这就是代码

    <?php


      // Connect to server and select databse.
           $link=mysql_connect("localhost","root", "mcl")or die("cannot connect");
             mysql_select_db("mcl",$link)or die("cannot select DB");
              $sql="SELECT * FROM admin WHERE fname=('$_POST[fname]') and
                password=('$_POST[password]')";
           $result=mysql_query($sql);
         // Mysql_num_row is counting table row
         $count=mysql_num_rows($result);
     // If result matched $myusername and $mypassword, table row must be 1 row
    if($count>0){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    header("location:dashboard.php");
      }
   ?>     



    <?php

           $fname="fname";
            $link=mysql_connect("localhost","root", "mcl")or die("cannot connect");
             mysql_select_db("mcl",$link)or die("cannot select DB");
          $sql="SELECT * FROM acco_untant  WHERE fname=('$_POST[fname]') and
          password=('$_POST[password]')";
          $result=mysql_query($sql);
        // Mysql_num_row is counting table row
            $count=mysql_num_rows($result);
      // If result matched $myusername and $mypassword, table row must be 1 row
        if($count>0){
      //Register $myusername, $mypassword and redirect to file "login_success.php"
    header("location:dash_accou.htm");
 }
      else {
       echo "Invalid username or password <a href=index.php><input name=Click here to reload  
       type=button              
       disabled value= <<<Reload>";
  }
  ?>    
 <?php
 // Connect to server and select databse.
   $link=mysql_connect("localhost","root", "mcl")or die("cannot connect");
   mysql_select_db("mcl",$link)or die("cannot select DB");
   $sql="SELECT * FROM secretary WHERE fname=('$_POST[fname]') and
   password=('$_POST[password]')";
    $result=mysql_query($sql);
     // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
   // If result matched $myusername and $mypassword, table row must be 1 row
  if($count>0){
  // Register $myusername, $mypassword and redirect to file "login_success.php"

   header("location:dash_secretary.htm");
   }
 ?>

如果我理解正确,您可以根据用户登录($\u POST['fname'])更改交换机中的位置

<?php
       session_start();
       // Connect to server and select databse.
       $link=mysql_connect("localhost","root", "mcl")or die("cannot connect");
       mysql_select_db("mcl",$link)or die("cannot select DB");
       $sql="SELECT * FROM admin WHERE fname=('$_POST[fname]') and
       password=('$_POST[password]')";
       $result=mysql_query($sql);
       // Mysql_num_row is counting table row
       $count=mysql_num_rows($result);
       // If result matched $myusername and $mypassword, table row must be 1 row
       if($count>0){
           $_SESSION['username'] = $_POST['fname'];
           switch($_POST['fname']) {
                case 'Root': header("Location: dashboard.php"); break;
                case 'Accountant': header("Location: dash_accou.htm"); break;
                case 'Secretary': header("Location: dash_secretary.htm"); break;
           }
           exit;
       }
       else {
            echo "Invalid username or password";
       }
?>
在SQL查询中传递原始POST数据,以防止SQL注入


散列用户密码。

您应该改用PDO或mysqli,这方面已经有很多posst了

此外,您还应该将所有用户放在同一个表中,并使用一个字段来标识用户的类型,或者使用一个具有其权限的表

如果要继续沿着要走的路径前进,可以使用UNION并添加如下用户类型:

$query = "SELECT *,'admin' as 'user_type', FROM admin WHERE fname=('$_POST[fname]') 
          and password=('$_POST[password]')

          UNION 

          SELECT *,'acco_untant' as 'user_type', FROM acco_untant  WHERE fname=('$_POST[fname]') and
          password=('$_POST[password]'

          UNION

          SELECT *,'secretary' as 'user_type', FROM secretary WHERE fname=('$_POST[fname]') and
   password=('$_POST[password]')")

"; 

您的脚本易受SQL注入攻击。你应该接受。我如何在这个脚本中设置mysql注入?请帮助我将此会话放置在何处开始();如果(!isset($_SESSION['username'])|$_SESSION['username']!='Root'){header('Location:index.php');exit;}我试图将其放入登录页面,但无论我在何处插入正确的密码和用户名,它都会返回到每个页面中的同一登录页面,该页面只能访问授权用户、dashboard.php和其他破折号。
$query = "SELECT *,'admin' as 'user_type', FROM admin WHERE fname=('$_POST[fname]') 
          and password=('$_POST[password]')

          UNION 

          SELECT *,'acco_untant' as 'user_type', FROM acco_untant  WHERE fname=('$_POST[fname]') and
          password=('$_POST[password]'

          UNION

          SELECT *,'secretary' as 'user_type', FROM secretary WHERE fname=('$_POST[fname]') and
   password=('$_POST[password]')")

";