2012-08-02 36 views
0

我正在製作一個簡單的oauth網站。我應該何時開始並銷燬PHP中的會話

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']重置爲默認值。但我不能在index.php開頭重置$_SESSION,因爲oauth.php中的函數有header()重定向到此頁面。

如何處理?

回答

0

您必須開始會話每一頁都需要訪問$_SESSION。只有在明確要求時才銷燬它,例如在註銷。

+1

什麼關於刷新? – DrXCheng 2012-08-02 21:47:33

+0

*在頂部 – Don 2012-08-02 21:48:03

+1

刷新與它有什麼關係? – 2012-08-02 21:50:56