2017-05-29 35 views
1

我正在努力解決最近有關Sessions的一些問題,現在我需要幫助才能使用XAMPP獲得小頁面的工作。

我使用4頁進行測試:
1)index.php是用戶放置其ID和PW的頁面。
2)logon.php檢查數據庫以查找匹配條目,並重定向到einter或index.phpprofile.php
3)profil.php呼應所有相關用戶數據
4)logout.php

試驗No.1:現在作爲用戶我做了以下幾點:我從index.php開始,輸入我的ID和PW。假設我的ID是1234.Logon說我很好,並將我重定向到profil.php,顯示用戶1234的數據。現在我可以註銷,會話被刪除並且一切似乎都正常。

測試2:作爲一個新的用戶我開始index.php,但現在我的ID是5678 logon.php告訴我,一切都很好,並重定向我profil.php,但它顯示了從第一次登錄的數據(1234) 。我也可以移動到其他頁面並回到profil.php,但數據總是用於用戶1234,直到我刷新頁面(F5或CTRL + SHIFT + R)。無論哪種方式現在它說明正確的5678數據。我可以註銷,會話將被再次銷燬。

這是我如何處理會話:

index.php

<?php 
    @session_start(); 
?> 

logon.php

<?php 
    @session_start(); 
    session_regenerate_id(true); 
?> 

profile.php

<?php 
    @session_start(); 
    if(!isset($_SESSION['personal_id'])){ 
     header('Location: /index.php'); 
     exit; 
    } 
?> 

logout.php

<?php 
    @session_start(); 
    $_SESSION = array(); 
    setcookie(session_name(), "", time()-3600, session_save_path()); 
    session_destroy(); 
    session_write_close(); 
?> 

所有標題是這樣的:

<head> 
    <title>Scheffel Shoptime</title> 
    <link href="style.css" rel="stylesheet" type="text/css"> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <meta http-equiv="cache-control" content="max-age=0" /> 
    <meta http-equiv="cache-control" content="no-cache" /> 
    <meta http-equiv="expires" content="0" /> 
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> 
    <meta http-equiv="pragma" content="no-cache" /> 
    <meta http-equiv="Refresh" content="5;url=./index.php"> 
</head> 

可你已經看到我的出問題,還是需要進一步的信息? 在此先感謝所有嘗試提供幫助的人!

+0

你爲什麼使用'session_regenerate_id(true);'?沒有很大的意義,沒有執行,看起來可能是問題。 – FMashiro

+0

你爲什麼要這樣做@session_start();' – Akintunde007

+0

我仍在學習如何使用會話。我瀏覽了stackoverflow上的其他頁面,指出我應該使用session_start();在每一頁上。 – Flo

回答

0

我實際上發現緩存問題的解決方案。我使用這個代碼在頭:

<meta http-equiv="cache-control" content="max-age=0" /> 
<meta http-equiv="cache-control" content="no-cache" /> 
<meta http-equiv="expires" content="0" /> 
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> 
<meta http-equiv="pragma" content="no-cache" /> 

,並把它改爲:

<?php 
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); 
    header("Cache-Control: post-check=0, pre-check=0", false); 
    header("Pragma: no-cache"); 
?> 

我不知道哪個版本的HTML部分是錯誤的,但PHP版本正常工作,從而解決了我的問題。非常感謝您的想法和意見!

相關問題