post/redirect/get并在SESSION中保存数据库查询


post/redirect/get and saving database query in SESSION

我是post/redirect/get的新手。我正在编写第一个真正的网站,它让我发现了对PRG的需求。因此,我编写并获得了如下工作代码:

1) user enters a search string
2) we search the database and find their desired search results
3) if we found their search results successfully, we alter the database -- 
   a 'frequency of lookups' -- to indicate the user searched and found what 
   he was looking for
4) then display the results he searched on

我发现,在浏览器中刷新页面会导致用户再次看到相同的搜索结果,但我们增加了"查找频率"——用户在数据库中的查找频率有限——因此,如果用户刷新页面的次数过多,他们就会用完每小时的数据库查找量。几天前,我就在这里发现了Post/Rerect/Get。

我现在正在浏览这个网站,更改所有更改数据库和显示结果的页面,并将它们切换到PRG。以下是新流程:

1) user enters a search string
2) we search the database and find their desired search results
3) if we found their search results successfully, we alter the database -- 
   a 'frequency of lookups' -- to indicate the user searched and found 
   what he was looking for
4) PRG to a 'results' page
5) then display the results he searched on

我遇到了一个问题。我需要以某种方式通过上面步骤(2)中的搜索结果到我为实现PRG而创建的步骤(5)中的新"结果"页面。

所以我的问题是——有"最常见"的方法吗?在阅读过程中,我看到了"将结果保存在.CSV文件中"以及"将数据库搜索结果保存在SESSION中"

我想将搜索结果保存在SESSION中,然后在我为PRG添加的"GET"页面上显示结果,我将从会话变量中读取搜索结果并显示它们。

我的意思是我想这样做:

$result = mysql_query($query, $theDbServer);
$_SESSION['theSearchResults'] = $result.

然后在"显示"页面上,从$_SESSION["搜索结果"]中读回搜索结果并使用显示

 $result = $_SESSION['theSearchResults'];
 $row = mysql_fetch_row($result);

这是正常的方法吗?我怀疑我无法将原始$result保存在如上所述的会话变量中——但我也不确定如何将上面的$result放入$_session中——通常是如何做到的?

假设您不想让最终登录页进行查询(当然不需要减少配额),那么您将需要使用会话。

保存到CSV不是标准的,也不会很好地扩展。我会将查询结果解析为一种更用户友好的形式(一个简单的类或列表或您需要的任何东西)。然后,我会将类而不是读者存储到会话中。当他们离开结果页面时(尤其是在数据量巨大的情况下),您可能希望清除会话的这一部分。

这是假设您使用的是原始php。有许多框架都具有针对这种情况的功能(您希望将一段数据从一个页面穿梭到下一个页面)。