2014-05-01 101 views
0

我是新來的PHP,在我的學生結果在線系統中完成我的最後一年項目。會話不能正常工作,刷新頁面時會丟失自動會話或內容丟失頁面

在我的系統中,我在會話中出現錯誤,一旦用戶/學生使用他們的用戶名和密碼登錄,儀表板會正確顯示他的詳細信息。

但是,只要頁面重新加載或您點擊查看其他內容或者只是簡單地點擊儀表板內的學生工具欄,該頁面就會丟失其所有相關內容並變爲空閒或空白。

我想防止自動會話破壞或丟失,而用戶仍然在同一頁面上,直到他/她註銷。任何幫助將不勝感激。有趣的是,這些代碼在Localhost(Wamp Server)中運行得非常好,但它在Online(cpanel)中丟失了會話。

我的代碼頁是: 的login.php

<?php 
include('dbcon.php'); 
if (isset($_POST['login'])){ 
    session_start(); 
    $student_no = $_POST['student_no']; 
    $password = $_POST['password']; 
    $query = "SELECT * FROM students WHERE student_no='$student_no' AND password='$password' and status = 'active' "; 
    $result = mysql_query($query)or die(mysql_error()); 
    $num_row = mysql_num_rows($result); 
    $row=mysql_fetch_array($result); 
    if($num_row > 0) { 
     header('location:dasboard.php'); 
     $_SESSION['id']=$row['student_id']; 
    } 
    elae{ 
     header('location:access_denied.php'); 
    } 
} 
?> 

session.php文件

<?php 
session_start(); 
if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) { 
    header("location: index.php"); 
    exit(); 
} 
$session_id=$_SESSION['id']; 
?> 

dasboard.php

<?php include('session.php'); ?> 
<?php include('header.php'); ?> 
<?php include('navbar.php'); ?> 
<?php 
    $query=mysql_query("select * from students where student_id='$session_id'")or die(mysql_error()); 
$row=mysql_fetch_array($query); 
$year_level = $row['year_level']; 
$term = $row['term']; 
$status = $row['student_status']; 
$school_year = $row['year_level'];?> 
<div class="container"> 
<div class="margin-top"> 
<div class="row"> 
<?php include('head.php'); ?> 
<div class="span12"> 
<div class="grade"> 
<?php include('grade_option.php'); ?> 
</div> 
</div> 
<div class="span2"> 
<?php include('user_sidebar.php'); ?> 
</div> 
<div class="span10"> 
<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered" id="example"> 
<thead> 
<tr> 
<th width="100">Code</th> 
<th width="300">Subject</th> 
<th width="50">Units</th> 
<th>Gen Ave.</th> 
<th>Term</th> 
<th>Year Level</th> 
<th>Remarks</th> 
</tr> 
</thead> 
<tbody> 
<?php $user_query=mysql_query("select * from grade where student_id = '$session_id' and school_year = '$year_level' 
           and semester = '$term' 
           ")or die(mysql_error()); 
while($row=mysql_fetch_array($user_query)){ 
           $id=$row['grade_id']; 
$remarks = $row['remarks']; 
$subject_id = $row['subject_id']; 
$subject_query = mysql_query("select * from subject where subject_id = '$subject_id'")or die(mysql_error()); 
while($subject_row=mysql_fetch_array($subject_query)){ 
?> 
<tr> 
<td> 
<?php echo $subject_row['code']; ?></td> 
<td><?php echo $subject_row['title']; ?></td> 
<td><?php echo $subject_row['unit']; ?></td> 
<td><?php echo $row['gen_ave']; ?></td> 
<td><?php echo $row['semester']; ?></td> 
<td><?php echo $row['school_year']; ?></td> 
<?php if ($remarks == 'Very Good'){ ?> 
<td><span class="very_good"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'Excellent'){ ?> 
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'Satisfactory'){ ?> 
<td><span class="sat"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'Fair'){ ?> 
<td><span class="fair"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'Failed'){ ?> 
<td><span class="failed"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'Incomplete'){ ?> 
<td><span class="failed"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'Officially Dropped'){ ?> 
<td><span class="drop"><?php echo $row['remarks']; ?></span></td> 
<?php }else if($remarks == 'PASS'){ ?> 
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td> 
<?php }else if ($remarks == ''){ ?> 
<td><?php echo $row['remarks']; ?></td> 
<?php } ?> 
</tr> 
<?php }} ?> 
</tbody> 
</table> 
</div> 
<?php include('units_table.php'); ?> 
<?php include('gwa_table.php'); ?> 
<?php include('cwa_table.php'); ?> 
</div> 
<?php include('grading_system.php') ?> 
</div> 
</div> 
</div> 
<?php include('footer.php') ?> 
+0

請使用最新的代碼更新您的問題中的login.php腳本。當試圖找出錯誤發生的位置時,會產生混淆。另外,包含'var_dump($ _ SESSION,$ session_id);在'session.php'結尾處,並且在header.php中的$ query之前,順便說一句,如果您將它稱爲'$ _SESSION ['student_id']',那麼稍後您就會更容易混淆。 –

+0

你可以檢查,我發現這個代碼內的問題從dasboard.php頁面: <?php $ query = mysql_query(「select * from students where student_id ='$ session_id'」)或死(mysql_error()) ; $ row = mysql_fetch_array($ query); $ year_level = $ row ['year_level']; $ term = $ row ['term']; $ status = $ row ['student_status']; $ school_year = $ row ['year_level']; ?> 我認爲,我必須設置cookie來存儲會話在dasboard.php的某個地方,但我不知道。如果可以的話,會很棒。如果我收到你的電子郵件,我可以給你發鏈接,看看有什麼確切的錯誤?! –

+0

我可以聯繫的電子郵件地址可以在這裏找到[Pastebin](http://pastebin.com/1taYrDSA)。它非常公開,但每隔幾個小時就會刪除任何電子郵件。 –

回答

1

在你的下面的代碼:

if($num_row > 0) { 
    header('location:dasboard.php'); 
    $_SESSION['id']=$row['student_id']; 
} 

你做

header('location:dasboard.php'); 

之前設置會話:

$_SESSION['id']=$row['student_id']; 

含義重定向第一和會話永遠不會設置。

By the way header.php, includes header.php?

而且

elae{ 
    header('location:access_denied.php'); 
} 

應該是當然的else

+0

Tim Dev先生,感謝您的回覆,但仍然有同樣的問題,您的建議不能解決我的問題。我私下提到,我的代碼適用於第一次登錄。唯一的問題是在重新加載或刷新dasboard.php頁面。 (如果我刷新頁面,或點擊此頁面中的其他鏈接,數據將會丟失,並保留爲靜態值)。可能是我沒有正確理解你的解決方案。你告訴我要更正Login.php頁面的session.php頁面?我做了兩個,但沒有解決。 Ya,我從不在header.php中包含header.php。雅,我也改正其他。 我是我後天的最後一次演講。 –

+0

親愛的@Tim Dev,非常感謝您對我的問題的持續幫助。 正如你所建議的那樣,我的確沒有工作。我認爲,dasboard.php代碼中存在錯誤。因爲在登錄時,它會將我帶到顯示數據庫中所有相關內容的dashboard.php頁面,但只要單擊儀表板上的任何鏈接或刷新或重新加載dashboard.php頁面,它就會丟失其所有內容。我希望你明白我的問題是。否則,我可以更多地解釋。問候 –

+0

還有一個線索,我現在找到了。管理員可以添加新學生。 之後,管理員會爲該特定學生明智地添加標記。直到沒有記錄插入爲該學生(他的dasboard.php(登錄後),顯示他的個人詳細信息),dasboard.php顯示他的信息,但插入主題和他/她(管理員)獲得的標記後,在這種情況下,首次登錄後,他/她會在dasboard.php中獲得所有詳細信息,但在刷新或重新加載或點擊dasboard.php上的任何鏈接時,所有信息都會丟失/丟失/丟失。所以,我很難理清這個錯誤。 –