2016-12-14 1 views
-4

実際、私は "$ _SESSION ['authuser'] = 1;セッション(クッキー)のサポートが原因多くの理由にそれを必要とするため

<?php 
session_start(); 
$_SESSION['username'] = $_POST['user']; 
$_SESSION['userpass'] = $_POST['pass']; 
$_SESSION['authuser'] = 1; 
//Check username and password information 

if(($_SESSION['username'] == 'joe') and 
($_SESSION['userpass'] == '123')) { 
$_SESSION['authuser'] = 1; 
} 
else 
{ 
echo 'Sorry, but you don\'t have permission to view this page!'; 
exit(); 
} 

?> 

答えて

1

以下のようにPHPコード、私のコードインチ

です。そうしないと、ページの任意のリンクをクリックしたときに毎回ユーザー名とパスワードを入力する必要があります。

<?php 
    session_start(); 
    $_SESSION['username'] = $_POST['user']; 
    $_SESSION['userpass'] = $_POST['pass']; 
    $_SESSION['authuser'] = 0; // user is not authenticated (just a GUEST), default is 0... 


// if visitor is priviledged, show him in, let him see the page 

    if(($_SESSION['username'] == 'joe') and 
    ($_SESSION['userpass'] == '123')) { 
    $_SESSION['authuser'] = 1; // insert 1 into DB and set cookie as 1 for user not to enter username and pswd anymore during browsing 
    } 
    else 
    { 
//else, keep guest away from a page 
    echo 'Sorry, but you don\'t have permission to view this page!'; 
    exit(); // shut down 
    } 

    ?> 
1

あなたのケースでは、usernameとuserpassのSESSIONの使用は冗長であるようです。これは可能かもしれない。認証されないように、ユーザーの初期化セッション

  • を開始

    • (これはオプションです)
    • チェック、ユーザー名とパスワード
      • 場合、彼らは:

        <?php 
        session_start(); 
        /*Do not set sessions for username and userpass, only use them in the POST array 
        *Initialize authuser to 0 because by default a user is not logged in 
        */ 
        $_SESSION['authuser'] = 0; 
        //Check username and password information 
        if(($_POST['user'] == 'joe') and 
            ($_POST['pass'] == '123')) { //Check the user and set it as authenticated 
            $_SESSION['authuser'] = 1; 
        } else { //If the user is not valid, stop execution 
            echo 'Sorry, but you don\'t have permission to view this page!'; 
            exit(); 
        } 
        ?> 
        

        私はここでやっていることですユーザが認証済みであることを設定しています

      • もしそうでなければ、sto pping実行。唯一のユーザーがログインしている覚えているのではなく、ユーザーが認証されると、ユーザー名とパスワードのセッションを設定するために有用である可能性があること

    は注意してください。

  • 関連する問題