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

2層の答え:

パート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インジェクションに対して脆弱であるため、悪意のあるユーザーが追加のSQLを含むuser_grouporuser_name値を含むリクエストを作成する可能性があり、スクリプトは問題なくそれを実行します。

外部入力が常に期待どおりに含まれるとは信頼できないため、常に外部入力を検証する必要があります。

これに関する詳細情報を見つけることができます:

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で使用する必要があります。

POSTだけでなくGETでも使用する場合は、を使用します$_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フォーム$_COOKIEを取得できます

于 2011-01-20T12:54:40.503 に答える