2013-04-29 60 views
0

我在XP上啓用了Opera 12.15,並啓用了在XAMPP和本地主機上運行的Cookie。沒有.htaccess。通過表單提交創建的PHP會話變量不會持久

1)我不明白爲什麼下面的會話變量不會在Opera中保留,而在其他主流瀏覽器中。僅限於Opera,如果在表單被接受後重新訪問頁面(通過鏈接),會話變量已經消失,表單再次顯示。沒問題(即變量仍然存在),如果我只刷新頁面。

2)我也有一個第二個問題,你可以看到下面我已經打開了一個php標籤並開始了一個'if'語句,然後關閉了php標籤,輸入了一些html,打開了一個新的php標籤,關閉了'if'並最終關閉了第二個php標籤。這是有效的代碼,我最初被教會在'if'內回顯html,並且只有一組php標籤?前者更簡單,更有效,我看到它在其他地方使用。

在此先感謝。

<?php 
// Turn on error reporting 
ini_set('display_errors', 1); 
error_reporting(E_ALL); 

session_start(); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<title>Opera Session Variable</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 

<body> 
<?php 
// create a test variable to confirm other session variables outside of Form are persisting 
$_SESSION['test'] = 'Test'; 

// function to print session variables 
function print_array($_SESSION) 
{ 
echo '<pre>$_SESSION<br />' . "\n"; 
print_r($_SESSION); 
echo "</pre>\n"; 
} 

// process the submitted form 
if($_SERVER['REQUEST_METHOD'] == 'POST') { 
if (isset($_POST['formaccept'])) { 
$_SESSION['formaccepted'] = $_POST['formaccept']; 
} 
} 

// print the session variables 
print_array($_SESSION); 

// only display the form if it has not previously been accepted in this session 
if (!isset($_SESSION['formaccepted'])) { 
?> 
<p><b>This parargraph should only display if the form has not been accepted in the current session.</b></p> 
<br /> 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<input type="submit" name="formaccept" value="Accept" /> 
</form> 
<?php 
} 
?> 

</body> 

</html> 

回答

0

必須是Opera處理緩存的方式,我看不到任何代碼錯誤。

至於你的第二個問題,該語法的作品,但通常不建議,因爲它使佈局變得髒滿片段。

+0

謝謝,這是否意味着我可以用.htaccess做些事情來克服這個問題,或者有可能是另一種方法,我只是不希望表單再次出現在同一個會話中?歌劇肯定有辦法做到這一點? – martin 2013-04-29 11:09:37

+0

我縮小到Opera版本11和12,只在XP上。不知道爲什麼。會話變量確實使用Windows7保持。 – martin 2013-05-10 15:28:25

+0

我認爲你可以將它作爲Opera的一個錯誤提交,以便他們可以看一下它,或者我假設已經有了一個更新的版本,所以你不必擔心這一點。 – Celestz 2013-05-11 06:36:13