0

我正在使用 post 变量,并且我正在尝试使用来自 url 的 get 变量来登录用户。

这是过程:

用户获得自己的 url,即 kayden.domain.com

当他们创建自己的帐户时,他们拥有自己的用户名并通过

当他们来到 kayden.domain.com 时,他们会使用这些凭据。

为了验证,我正在检查帖子变量(用户名和密码)并尝试使用 $_GET['user_group'] 进行验证。

该脚本在我只使用 post 变量时有效,但在 GET 时它不起作用。下面是代码:

PHP:

    $SUBDOMAIN = mysql_real_escape_string($_GET['user_group']);


      // Process the POST variables
         $username = $_SESSION["user_name"];
       //$password = $_POST["password"];


        // Set up the session variables
       $_SESSION["user_name"] = $username;


         $secret = $info['password'];

            //Checks if there is a login cookie

          if(isset($_COOKIE['ID_my_site']))


       //if there is, it logs you in and directes you to the members page

        { 
        $username = $_COOKIE['ID_my_site']; 

       $pass = $_COOKIE['Key_my_site'];

       $check = mysql_query("SELECT user_name, password FROM accounts WHERE user_name = '$username' and user_group='$user_group'")or die(mysql_error());

    while($info = mysql_fetch_array( $check )) 



      {

      if (@ $info['password'] != $pass) 
       {

          }

        else

       {

             header("Location: members.php");



       }

      }

     }


          //if the login form is submitted 

      if (isset($_POST['submit'])) { // if form has been submitted



          // makes sure they filled it in

          if(!$_POST['user_name'] | !$_POST['password']) {

            die('You did not fill in a required field.');

        }

          // checks it against the database



        if (!get_magic_quotes_gpc()) {

        $_POST['user_name'] = addslashes($_POST['user_name']);
       $_GET['user_group'] = addslashes($_GET['user_group']);

         }

        $check = mysql_query("SELECT user_name,password FROM accounts WHERE user_name = '".$_POST['user_name']."' and user_group='".$_GET['user_group']."'")or die(mysql_error());



          //Gives error if user dosen't exist

           $check2 = mysql_num_rows($check);

       if ($check2 == 0) {

           die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

      }

            while($info = mysql_fetch_array( $check ))  

         {

       $_POST['password'] = md5($_POST['password']);
        $_POST['password'] = $_POST['password'];



      //gives error if the password is wrong



        if (@ $_POST['password'] != $info['password']) {

        die('Incorrect password, please try again');


        }

          else 

        { 


            // if login is ok then we add a cookie 

          $_POST['user_name'] = stripslashes($_POST['user_name']); 

           $hour = time() + 3600; 

             setcookie(ID_my_site, $_POST['user_name'], $hour); 

             setcookie(Key_my_site, $_POST['password'], $hour);  



          //then redirect them to the members area 

         header("Location: members.php"); 

          } 

             } 

           } 

      else 

       {  



          // if they are not logged in 

     ?> 

       <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

          <table border="0"> 

       <tr><td colspan=2><h1>Login</h1></td></tr> 

        <tr><td>username:</td><td> 

          <input type="text" name="user_name" maxlength="40"> 

          </td></tr> 

           <tr><td>Password:</td><td> 

           <input type="password" name="password" maxlength="50"> 

           </td></tr> 

           <tr><td colspan="2" align="right"> 

               <input type="submit" name="submit" value="Login"> 

           </td></tr> 

          </table> 

           </form> 


    <?php 

       } 



       ?> 
4

3 回答 3

4

两层答案:

第1部分

您可以使用 PHP $_REQUEST 变量从 GET(查询字符串)、POST 和 COOKIE 中获取详细信息。

例如:

$ugData = $_REQUEST['user_group'];
$unData = $_REQUEST['user_name'];

可以在此处找到更多信息:

http://php.net/manual/en/reserved.variables.request.php

第2部分

您的代码中的这一行:

$check = mysql_query("SELECT user_name,password FROM accounts WHERE user_name = '".$_POST['user_name']."' and user_group='".$_GET['user_group']."'")or die(mysql_error()); 

容易受到 SQL 注入的影响,恶意用户可以制作包含一个user_groupuser_name包含附加 SQL 的值的请求,并且您的脚本将毫无疑问地执行它。

您应该始终验证任何外部输入,因为您不能相信它总是包含您所期望的。

可以找到有关此的更多信息:

http://php.net/manual/en/security.database.sql-injection.php http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php

如何解决这个问题的一个简单例子是:

$ugData = mysql_real_escape_string($_REQUEST['user_group']);
$unData = mysql_real_escape_string($_REQUEST['user_name']);

这将逃避$_REQUEST输入,从而阻止任何制作恶意请求的人。但是,它不会验证给定的 user_group / user_name 是有效值。

于 2011-01-20T12:53:23.323 回答
2

您正在使用 POST 方法提交表单。所以你需要将它与 $_POST 一起使用。

如果您想将它与 GET 和 POST 一起使用,请使用$_REQUEST['varName'].

于 2011-01-20T12:41:50.863 回答
0
if ( isset ( $_GET['user_group'] ) {
  $SUBDOMAIN = mysql_real_escape_string($_GET['user_group']);
}
if ( isset ( $_POST['user_group'] ) {
  $SUBDOMAIN = mysql_real_escape_string($_POST['user_group']);
}

使用 $_REQUEST 可以获取 var form $_COOKIE

于 2011-01-20T12:54:40.503 回答