0

私はMVCスタイルのプログラミングに非常に慣れていません。ユーザー資格情報をブラウザー アプリケーションに統合できるようにする管理スクリプトがあります。ユーザー名、電子メール、名前などのユーザー情報。このシステムのドキュメントには、この情報を生成するための明確な説明が記載されています。私は次のスクリプトでこれを実行しましたが、これは正常に機能しますが、ユーザーがログインしてこの情報を取得することを許可する方法がなく、それが私の問題であるため、常に「AUTH_NO_SESSION」が返されます。

ユーザー情報 (user_cred.php)

include_once("includes.php"); 
$auth = new TAuthentication();    
$accept_roles = array('plugin');
$auth_result  = $auth->validateSession($accept_roles);

if ($auth_result->auth_code == AUTH_NO_SESSION) {
    header('Access-Control-Allow-Origin: *');
    echo "AUTH_NO_SESSION";
    // means that no session was found, therefore the page is being accessed anonymously.
} elseif ($auth_result->auth_code == AUTH_OKAY) {
    header('Access-Control-Allow-Origin: *');
    echo "AUTH_OKAY";
    // means that there was a session and the user owns all the required roles to access this content.
} elseif ($auth_result->auth_code == AUTH_INSUFFICIENT_ROLES) {
    header('Access-Control-Allow-Origin: *');
    echo "AUTH_INSUFFICIENT_ROLES";
    // means that a session exists, but the user does not own the required roles to access this content.
} else {
    // no code here
}

user_cred.phpブラウザ アプリケーションは、上記のリッスン ファイルからユーザー データを取得します。このphpファイルから情報を要求する限り、すべてが正常に機能します。私が直面している問題は、実際にユーザー情報を取得することです。これを行う唯一の方法は、ユーザーが自分のアカウントにログインすることです。そうでなければ、何も与えられません。

ブラウザ アプリケーション

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","user_cred.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>Using the XMLHttpRequest object</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

管理システムにはview、次のログイン フォームを持つファイルがあります。そのため、ユーザーはウェブサイトにアクセスします。また、ログイン コードを含むメイン インデックス ファイルもあります。私の限られた知識でこれを調べたところ、これらの 2 つのファイルがスクリプトの作成に役立ち、ユーザーがブラウザー アプリケーションからログインしてユーザー資格情報を取得できるようになると信じています。私の考えは、index.phpファイルからuser_cred.phpファイルにコードを追加して、このようなURLhttp://website.com/user_cred.php?username=admin&pass=test&signin=Loginをjavascript httprequestに追加し、その方法でユーザー情報を取得できるようにすることです

ログインビュー

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <ul>
        <li class="listitem">
            <div class="row">
                <label>Username:</label>
                <input class="textbox" type="text" name="username" value="" maxlength="80"/>
            </div>
            <div class="row">
                <label>Password:</label>
                <input class="textbox" type="password" name="password" value="" maxlength="80"/>
            </div>
        </li>
        <li class="listitem">
            <div class="row">
                <input class="form-button" type="submit" name="signin" value="Signin"/>
                <a class="loginoptions indentmore" href="signup.php">Signup</a>
                <a class="loginoptions" href="resetpassword.php">Forgot your password?</a>
            </div>
        </li>
    </ul>
</form>

index.php

include_once("includes.php");

class TSigninController extends TAbstractController {

    public function run($allowedRoles = null)
    {
        $this->allowedRoles = $allowedRoles;
        $this->execute();
    }

    protected function execute() 
    {
        $this->auth_result = parent::validateSession(null);

        if ($this->auth_result->auth_code == AUTH_OKAY)
        {
            $this->goToAfterSignInPage($this->auth_result->roles);
        }
        else if (!$this->getUserAction())
        {
            $this->loadview("signin");
        }
        else
        {
            $this->signin();        
        }
    }

    protected function signin()
    {
        $input   = $this->getUserInput();
        $model   = $this->loadmodel("Users");
        $account = $model->getUser($input["username"], $input["password"]);

        if ($account == null || sizeof($account) == 0) 
        {
            $data = array("error" => "Could not sign you in");
            $this->loadview("signin", $data);
            return;
        } 

        if ($account["disabled"] == 1 || $account["admin_disabled"] == 1) 
        {
            $data = array("error" => ($account["admin_disabled"] == 0) ? "This account is disabled." : "This account is been locked by the admin. Please contact the site admin!");
            $this->loadview("signin", $data);
            return;
        } 

        $this->createNewSession($account);
        $this->goToAfterSignInPage($account["roles"]);
    }

    protected function createNewSession($account) {
        $model     = $this->loadmodel("Sessions");
        $sessionid = crypt($account["username"] . date('now'));

        $_SESSION['SESSIONID'] = $sessionid;
        $model->createNewSession($sessionid, $account["id"]);
    }

    public function goToAfterSignInPage($roles)
    {
        foreach($roles as $role)
        {
            if ($this->utils->stringsEqual($role["name"], "admin", false))
            {
                $this->redirect(SITE_URL . "/admin/dashboard.php");
                return;
            }
        }

        $this->redirect(SITE_URL . "/user/userprofile.php");
    }

    protected function getUserAction()
    {
        if ($this->post("signin"))
            return "signin";
        else     
            return null;            
    }

    protected function getUserInput()
    {
        return array(
            "username" => $this->post("username"),
            "password" => $this->post("password")
        );
    }
}

$controller = new TSigninController();
$controller->run();

user_cred.php結論として、私は、ユーザーが自分のブラウザー アプリケーション内から資格情報にアクセスできるようにする php スクリプトを作成できるように、助けを求めています。だから、MVC と PHP の知識を持っている人なら誰でも、私はとても素晴らしいと思います。

4

1 に答える 1

0

SharpUMS によって提供された MVC の説明は、非常に恐ろしいものでした。そして、SharpUMS のソースを入手するために必要な労力を考えると、これはオープンソース プロジェクトではないと思います。

が である理由$auth_result->auth_code === AUTH_NO_SESSIONは2 つありtrueます。

  • $_SESSION['SESSIONID']空です
  • セッション ID がデータベースに見つかりませんでした:

    から: sharpums/_application/models/Session.php

    SELECT 
        s.userid, 
        s.id, 
        s.started_on, 
        ( 
            DATE_ADD(
                 s.started_on, 
                 INTERVAL $this->sessionlength SECOND
            ) < NOW()
        ) expired 
    FROM sessions s 
    WHERE s.id = '$sessionid'
    

基本的に、ボットの理由はindex.phpまでさかのぼります。このメソッドは決して実行されないと思います:

protected function createNewSession($account) {
    $model     = $this->loadmodel("Sessions");
    $sessionid = crypt($account["username"] . date('now'));

    $_SESSION['SESSIONID'] = $sessionid;
    $model->createNewSession($sessionid, $account["id"]);
}

どの時点でsignin()メソッドが終了するかを調べる必要があります。

補足事項

無料のアドバイス:次の理由により、MVC 全体でのアプリケーションまたは研究の基礎としてSharpUMS を使用しないでください。

  • 単一の DB 接続TDatabaseがグローバル状態を使用して接続を保持するようにする
  • mysql_*非推奨のプロセスにある古代の API に密接にバインドされています。
  • これTAbstractModelは実際には抽象的ではなく、コンストラクターで新しい DB インスタンスを作成します
  • 設計上の問題: コア クラスはモデル (コアの外にある) に依存します
  • TUtilitiesクラスは巨大なゴミ捨て場です ( 2.1 採用者パターンを参照)
  • パスワードは単純な MD5 ハッシュとして保存されます..LinkedIn の事件について聞いたことがありますか?
  • SQL インジェクションに対する弱い保護: 利用preg_*addslashes()機能
于 2012-06-13T12:50:20.153 に答える