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.php,因为函数oauth.php必须header()重定向到这个页面。

如何处理?

4

1 回答 1

0

您必须在需要访问$_SESSION. 仅在明确要求时销毁它,例如在注销时。

于 2012-08-02T21:46:50.043 回答