doudeng1870 2014-04-11 12:36
浏览 47
已采纳

PHP - URL查询字符串参数与会话变量

Which is the more reliable, better and safer method to use for URL variables passed to different pages: 1). Using SESSION variables as URL parameters, or

2). Regular query string parameters.

"More reliable, better, and safer" with regard to: 1. Preventing problems deriving from the client side, such as when a user disables cookies 2. Browser compatibilities 3. Making API calls (as some API's have limitations and compatibility issues) 4. Resource and memory usage, and processing speed

I'm creating a site where the amount of query string parameters in the URLs may vary (potentially 9 values carried in the URL) - based on user input. It would seem easier to store the variable values in session variables than to carry all of them (possibly 9) in URL parameters. But because of the 4 concerns mentioned above, I'm hesitant to use SESSION variables.

Thanks for any advice!

PS. The URL parameters are being built dynamically into the $url variable, like this:

$keyword = trim($_GET["Keyword"]);

$url = "webpage.php?";
$url .= "&Keyword=$keyword";

$shopByStore = $_GET["store"];
if (!empty($shopByStore)) {
$url .= "&store=$shopByStore";
}
// with 7 more GET methods potentially retrieving values for the URL parameters 

The URL's will look like this:

<a href="<?php echo $url; ?>">anchor text</a><br>

And of course if I go the SESSION variable route, user input values would be obtained from URL clicks and stored in SESSION variables until the session is over.

if (isset($_GET["store"])) {
$_SESSION["shopByStore"] = $_GET["store"];
}  
  • 写回答

1条回答 默认 最新

  • du6333137 2014-04-11 12:42
    关注

    Of course it is possible to save for example a submitted keyword for a search in a session variable.

    But is it useful? No.

    The session variable will forget everything, when the session get's destroyed. Maybe the user wants to save the search or give them to another user?

    With GET-parameters it is not problem, just copy the full URL and safe it.

    But with SESSION parameters? Not possible. Each user has it's own session and the next time the user visits the site, he will get another session (for example if the users closed the browser before).

    In sessions you should only store user specific information. For example the userid, username or basket items.

    That are only some points for SESSION vs GET.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?