2012-08-12 43 views
0

可能重複:
PHP Can't read cookies?我不能讓這些cookie在PHP中設置

我增加了登錄和記住我的功能,我的網頁。我使用$ _SESSION變量和$ _COOKIE變量,但無法使用setcookie()設置cookie。我通過www.phpcodechecker.com運行了代碼,發現沒有錯誤。我的瀏覽器(Chrome和IE)已設置爲接受Cookie。不管我做什麼,這些cookies都不會設置。有人可以幫我嗎?

<?php 

$page_title = 'Log In'; 
$css_link = 'login.css'; 
require_once('includes/db_connection.php'); 
require_once('includes/header.php'); 
require_once('includes/navlinks.php'); 



// Start the session 
    require_once('includes/startsession.php'); 

    // Clear the error message 
    $error = ''; 

    // If the user isn't logged in, try to log them in 
    if (!isset($_SESSION['beecharmer_user_id'])) { 
    if (isset($_POST['submit'])) { 
     // Connect to the database 
     $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
    // Grab the user-entered log-in data 
    $username = mysqli_real_escape_string($dbc, trim($_POST['username'])); 
    $password = mysqli_real_escape_string($dbc, trim($_POST['password'])); 

    if (!empty($username) && !empty($password)) { 
    // Look up the username and password in the database 
    $query = "SELECT salt FROM beecharmer_user WHERE username = '$username'"; 
    $data = mysqli_query($dbc, $query); 
    $row = mysqli_fetch_array($data); 
    $password_salt = hash("sha512", $password.$row['salt']); 
    $query = "SELECT user_id, username, access_level FROM beecharmer_user WHERE username = '$username' AND password = '$password_salt'"; 
    $data = mysqli_query($dbc, $query); 

    if (mysqli_num_rows($data) == 1) { 
     // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page 
     $row = mysqli_fetch_array($data); 
     $_SESSION['beecharmer_user_id'] = $row['user_id']; 
     $_SESSION['beecharmer_username'] = $row['username']; 
     $_SESSION['beecharmer_access_level'] = $row['access_level']; 
     setcookie('beecharmer_user_id', $row['user_id'], time() + 525960); // expires in 365.25 days 
     setcookie('beecharmer_username', $row['username'], time() + 525960); // expires in 365.25 days 
     setcookie('beecharmer_access_level', $row['access_level'], time() + 525960); // expires in 365.25 days 

     // The next 4 lins just show what vars are set and what vars are not set. 
     // They serve no other purpose. 
     echo ('These are cookies '.$_COOKIE['beecharmer_user_id'].' '.$_COOKIE['beecharmer_username'].' '.$_COOKIE['beecharmer_access_level'].'<br/>'); 
     echo ('these are session vars '.$_SESSION['beecharmer_user_id'].' '.$_SESSION['beecharmer_username'].' '.$_SESSION['beecharmer_access_level'].'<br/>'); 
     echo ('These are query vars '.$row['user_id'].' '.$row['username'].' '.$row['access_level']); 
     echo ($row['user_id'].' '.$row['username'].' '.$row['access_level']); 

     $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php'; 
     //header('Location: ' . $home_url); 
    } 
    else { 
     // The username/password are incorrect so set an error message 
     $error = 'Sorry, you must enter a valid username and password to log in.'; 
    } 
    } 
    else { 
    // The username/password weren't entered so set an error message 
    $error_msg = 'Sorry, you must enter your username and password to log in.'; 
    } 
} 


} 
?> 

<div class= "info"> 
<?php 
    // If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in 
    if (empty($_SESSION['beecharmer_user_id'])) { 
     echo '<p class="error">' . $error . '</p>'; 
    }else { 
     // Confirm the successful log-in 
     echo('<p class="login">You are logged in as ' . $_SESSION['beecharmer_username'] . '.</p>'); 
    } 
?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <fieldset> 
     <legend>Log In</legend> 
     <label for="username">Username:</label> 
     <input type="text" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br /> 
     <label for="password">Password:</label> 
     <input type="password" name="password" /> 
    </fieldset> 
    <input type="submit" value="Log In" name="submit" /> 
    </form> 
    <?php //if (isset($_SESSION['beecharmer_user_id'])) echo $_COOKIE['beecharmer_username'].' '.$_COOKIE['beecharmer_access_level']; ?> 
</div> 
+0

www.phpcodechecker.com - OH MY GOSH。請讓我看不見它 – zerkms 2012-08-12 22:29:16

+0

@zerkms根據phpcodechecker一切都好嗎? ;-) – PeeHaa 2012-08-12 22:31:10

+0

@Petra如果accroding phpcodechecker一切都好,那麼我必須有一些非常可怕的代碼 – beewrangler 2012-08-12 23:05:24

回答

3

我不知道這是否是你的情況,但setcookie必須拿出任何輸出到瀏覽器(包括echo和<?PHP之外的HTML代碼...?>)前。確保你沒有輸出任何東西。這是因爲HTTP頭必須位於響應主體之前。您發佈的網頁看起來並不完整(只是一個div),所以我無法看到是否是這種情況,但可能是您在PHP代碼之前發送了HTML <頭>。

此外,正如佩特拉所說,$_COOKIE已加載在頁面請求上,所以新的設置爲setcookie的cookie尚未存儲在其中,但他們只在下一頁請求時纔到達那裏。

+0

你是對的。移動我的標題後,鏈接包括頁面底部的cookie設置爲預期。 – beewrangler 2012-08-13 00:19:44