0

簡単なoauthサイトを作成しています。

index.php

<?php
session_start();
if (empty($_SESSION['authentication']))
    $_SESSION['authentication'] = 'pending';
?>
<html>
<form action="oauth.php" method="post">
    <span>
    <?php
        echo $_SESSION['authentication'];
    ?>
    </span>
    <input type="hidden" name="action" value="authenticate">
    <input type="submit" value="authenticate">
</form>
</html>

oauth.php

<?php
session_start();
if (isset($_POST['action']) and $_POST['action'] == 'authenticate') {
    $url = $serverAuth ... ;
    header('Location: ' . $url); //google oauth, it will come back to oauth.php
    exit();
}

if (isset($_GET['code'])) {
    $ch = curl_init($serverToken);
    $result = curl_exec($ch);
    $tokens = json_decode($result, true);

    if (isset($tokens['access_token'])) {
        $_SESSION['authentication'] = 'good';
        $_SESSION['access_token'] = $tokens['access_token'];
    } else {
        $_SESSION['authentication'] = 'error';
    }

    header('Location: ./');
    exit();
}

if (isset($_GET['error'])) {
    if ($_GET['error'] == 'access_denied')
        $_SESSION['authentication'] = 'denied';
    else
        $_SESSION['authentication'] = 'error';
    header('Location: ./');
    exit();    
}
?>

次のようなサイトを作成したいと思います。デフォルトで$_SESSION['authentication']は、「保留中」です。ページを更新すると、すべてのセッション変数がなくなり、$_SESSION['authentication']デフォルトにリセットされます。ただし、の関数はこのページにリダイレクトする必要があるため$_SESSION、の先頭でリセットすることはできません。index.phpoauth.phpheader()

どのように対処しますか?

4

1 に答える 1

0

へのアクセスが必要なすべてのページで$_SESSIONセッションを開始する必要があります。ログアウト時など、明示的に要求された場合にのみ破棄してください。

于 2012-08-02T21:46:50.043 に答える