2013-04-29 63 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