我的代码在执行时中断 - 不期待 T_ELSE,期待 T_FUNCTION

发布于 2024-10-04 10:46:07 字数 4181 浏览 0 评论 0原文

我已经在 lamp、wamp 和 xampp 上尝试过我的代码 - 它只适用于 lamp。然而,如果我将 php.ini 文件从 lamp 换成 xampp,它就可以工作 - 因此,我猜测我在 LAMP 上以一种草率的方式编码,而我的 php.ini 却令人恼火地允许。

目前,我的 LAMP php.ini 破坏了我的 xampp mysqli,在我看来,我的 lamp 代码在任何情况下都一定是脏的,所以我想知道你们是否能看到这里需要清理什么?

class datamanagement{


 protected $mysql_host = "localhost";
 protected $mysql_username = "root";
 protected $mysql_password = "";
 protected $mysql_database = "data";

 protected $security_table = "users";

 function __construct($security_level = 0)
 {
   $this->security($security_level);
 }

 protected function mysql_connect_func(){ 
          // ...standard mysql connect stuff
 }

 protected function security($security_level){


  session_start();

  if(isset($_GET['logout'])){

   session_unset();
  }


  if($security_level > 0)
  {


   if(!isset($_SESSION['initiated'])){

    if(!isset($_POST['username']) || empty($_POST['username']) || empty($_POST['password']))
    {

     if(isset($_GET['logout']))
     {
     $string = rtrim($_SERVER['PHP_SELF'], '?logout');
     }


     ?>

     <div class="main_container">
    <div class="form_container">

    <?php

    if(isset($_GET['logout']))
    {
     echo "<p>successfully logged out</p>";

    }else
    {

     echo "<p>Access to this section require logging in</p>";

    }
    ?>

       <form method="post" action="<?php echo $string ?>"><input
        type="hidden" name="login" value="true"></input>

       <div><label for="title">Username:</label> <input name="username"
        type="text" value="<?php echo $_POST['username']; ?>"></input><?php if(isset($_POST['username']) && $_POST['username'] == ''){echo "username required";}?><br>
       </div>

       <div><label for="post">Password:</label> <input name="password"
        type="password"></input><?php if(isset($_POST['username']) && $_POST['password'] == ''){echo "password required";}?><br>
       </div>

<input type="submit" value="Sign in" name="submit"></input></form>

     </div>
    </div>

    <?


     exit();


    } // end if - no username or password were posted
    else{

     $this->mysql_connect_func();

     $sql = "SELECT * FROM " . $this->security_table . " WHERE  username='" . $_POST['username'] . "'";


     $result = mysql_query($sql) or die(mysql_error());
     $row = mysql_fetch_assoc($result);


     if(mysql_num_rows($result) != 0)
     {


      if(sha1($_POST['password']) == $row['password'])
      {
      session_regenerate_id();
      $_SESSION['initiated'] = "true";
      $_SESSION['username'] = $row['username'];
      $_SESSION['authority'] = $row['authority'];

      } // end if sha1 of $_POST password == $row password
      else {
       ?>

        <div class="main_container">
        <div class="form_container">


       <?php
       echo 'Incorrect password<br><a href="' . $_SERVER['PHP_SELF'] . '">Please try again</a>';
       ?>
       </div>
       </div>


       <?php

       exit();


      }  // if password is wrong


     } // end if no rows with username returned
     else{
?>

        <div class="main_container">
        <div class="form_container">


       <?php
       echo 'Incorrect username <br><a href="' . $_SERVER['PHP_SELF'] . '">Please try again</a>';
       ?>
       </div>
       </div>


       <?php

       exit();

     } // if username not found


    } // end else - no username or password were posted

   } // end if - check the session !initiated

   else {   //*THIS IS THE LINE THAT THROWS THE ERROR IN XAMPP AND WAMP*

    if($_SESSION['authority'] < $security_level)
    {

     die("security clearance insufficient");
    }


   } // end else - check the session !initiated


  } // end if $security_level <= 0

 } // end of function security()

}

我很抱歉,这是一大堆代码,我想不出一种合乎逻辑的方法来分解它,而不损害某人帮助我找到错误的能力。如果你们对将野兽的大小缩小到更易读的程度有任何建议,请说出来!

I have tried my code on lamp, wamp and xampp - with it only working on lamp. However, if I swap out the php.ini file from lamp into xampp, it works - hence, I surmise I'm coding in a sloppy way on LAMP that my php.ini is irritatingly permissive of.

Currently, my LAMP php.ini breaks my xampp mysqli, and it seems to me that my lamp code must be dirty in any case, so I was wondering if you guys could see what needs cleaning here?

class datamanagement{


 protected $mysql_host = "localhost";
 protected $mysql_username = "root";
 protected $mysql_password = "";
 protected $mysql_database = "data";

 protected $security_table = "users";

 function __construct($security_level = 0)
 {
   $this->security($security_level);
 }

 protected function mysql_connect_func(){ 
          // ...standard mysql connect stuff
 }

 protected function security($security_level){


  session_start();

  if(isset($_GET['logout'])){

   session_unset();
  }


  if($security_level > 0)
  {


   if(!isset($_SESSION['initiated'])){

    if(!isset($_POST['username']) || empty($_POST['username']) || empty($_POST['password']))
    {

     if(isset($_GET['logout']))
     {
     $string = rtrim($_SERVER['PHP_SELF'], '?logout');
     }


     ?>

     <div class="main_container">
    <div class="form_container">

    <?php

    if(isset($_GET['logout']))
    {
     echo "<p>successfully logged out</p>";

    }else
    {

     echo "<p>Access to this section require logging in</p>";

    }
    ?>

       <form method="post" action="<?php echo $string ?>"><input
        type="hidden" name="login" value="true"></input>

       <div><label for="title">Username:</label> <input name="username"
        type="text" value="<?php echo $_POST['username']; ?>"></input><?php if(isset($_POST['username']) && $_POST['username'] == ''){echo "username required";}?><br>
       </div>

       <div><label for="post">Password:</label> <input name="password"
        type="password"></input><?php if(isset($_POST['username']) && $_POST['password'] == ''){echo "password required";}?><br>
       </div>

<input type="submit" value="Sign in" name="submit"></input></form>

     </div>
    </div>

    <?


     exit();


    } // end if - no username or password were posted
    else{

     $this->mysql_connect_func();

     $sql = "SELECT * FROM " . $this->security_table . " WHERE  username='" . $_POST['username'] . "'";


     $result = mysql_query($sql) or die(mysql_error());
     $row = mysql_fetch_assoc($result);


     if(mysql_num_rows($result) != 0)
     {


      if(sha1($_POST['password']) == $row['password'])
      {
      session_regenerate_id();
      $_SESSION['initiated'] = "true";
      $_SESSION['username'] = $row['username'];
      $_SESSION['authority'] = $row['authority'];

      } // end if sha1 of $_POST password == $row password
      else {
       ?>

        <div class="main_container">
        <div class="form_container">


       <?php
       echo 'Incorrect password<br><a href="' . $_SERVER['PHP_SELF'] . '">Please try again</a>';
       ?>
       </div>
       </div>


       <?php

       exit();


      }  // if password is wrong


     } // end if no rows with username returned
     else{
?>

        <div class="main_container">
        <div class="form_container">


       <?php
       echo 'Incorrect username <br><a href="' . $_SERVER['PHP_SELF'] . '">Please try again</a>';
       ?>
       </div>
       </div>


       <?php

       exit();

     } // if username not found


    } // end else - no username or password were posted

   } // end if - check the session !initiated

   else {   //*THIS IS THE LINE THAT THROWS THE ERROR IN XAMPP AND WAMP*

    if($_SESSION['authority'] < $security_level)
    {

     die("security clearance insufficient");
    }


   } // end else - check the session !initiated


  } // end if $security_level <= 0

 } // end of function security()

}

I apologise that it's such a hefty chunk of code, I couldn't think of a logical way to break it up without compromising someone's ability to help me find the error. If you guys have any suggestions about bringing the size of the beast down to a more readable amount, please do say!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-10-11 10:46:07

我不确定这是否对您有帮助 - 但是在 第 67 行中,您在 $string 变量之后缺少终端 ;

更轻松一点 - 标签不是 HTML 严格标记的一部分...

使用

i am not sure if this will help you - however in line 67 you are missing a terminal ; after the $string variable.

and on a lighter note - </input> tags are not part of the HTML strict markup...

use <input type="submit" value="Sign in" name="submit" /> instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文