php Smarty - 发布/会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7254679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Smarty - Getting Posted / Session Variables
提问by StuBlackett
I'm working on an "In My Basket" Feature on a shopping site. I'm pairing it on productid. I've done a tamper data on the post when you add to basket. The variable that is getting posted is productid.
我正在购物网站上开发“In My Basket”功能。我正在将它与 productid 配对。当您添加到购物篮时,我已经在帖子上进行了篡改数据。发布的变量是productid。
I've been using Thisas a guide to output the productid variable. But I'm just not getting a response from the page at all.
我一直在使用This作为输出 productid 变量的指南。但我根本没有收到页面的回复。
The code I am using is {$smarty.request.productid}
我使用的代码是 {$smarty.request.productid}
The TPL file I am using is HEREI am working on the < div class="inbasket"> which is line 69.
我正在使用的 TPL 文件在这里我正在处理第 69 行的 <div class="inbasket">。
It seems to output with the SERVER_NAME example. But I need that equivalent PHP $_POST VAR.
它似乎以 SERVER_NAME 示例输出。但我需要等效的 PHP $_POST VAR。
Does anyone have any idea on what I need to do to pull through the productid and show it on screen, Then I can do an IF statement based on that.
有没有人知道我需要做什么才能通过 productid 并将其显示在屏幕上,然后我可以基于此执行 IF 语句。
Also worth noting I am using Version 2.6.20 of Smarty
另外值得注意的是我使用的是 Smarty 的 2.6.20 版
Hoping someone can help me out with this. It appears that smarty is just not showing the session variables at all...
希望有人能帮我解决这个问题。看来 smarty 根本没有显示会话变量......
回答by tom
Using {$smarty.request.productid} will only get values that were are in the $_POST array or the $_GET array. For session vars you would simply use "session" as in {$smarty.session.productid}. With smarty the same applies to
使用 {$smarty.request.productid} 只会获取 $_POST 数组或 $_GET 数组中的值。对于会话变量,您只需使用 {$smarty.session.productid} 中的“会话”。使用 smarty 同样适用于
- $_POST -- {$smarty.post.productid}
- $_GET -- {$smarty.get.productid}
- $_REQUEST -- {$smarty.request.productid} (request will get vars from both $_POST and $_GET)
- $_SESSION -- {$smarty.session.productid}
- $_POST -- {$smarty.post.productid}
- $_GET -- {$smarty.get.productid}
- $_REQUEST -- {$smarty.request.productid}(请求将从 $_POST 和 $_GET 获取变量)
- $_SESSION -- {$smarty.session.productid}
Put this at the top of your tpl file and it will popup with all assigned smarty vars
把它放在你的 tpl 文件的顶部,它会弹出所有分配的 smarty vars
{debug}
Want to see what is in the session? Put this at the top of you tpl file
想看看会话中有什么?把它放在你的 tpl 文件的顶部
{php}
print_r($_SESSION);
{/php}
回答by Ankit
If you fail to debug, You've another alternative to do.
如果您无法调试,您还有另一种选择。
In PHP file,
在 PHP 文件中,
$smarty->assign('request_var',$_REQUEST['var1']);
$smarty->assign('request_var',$_REQUEST['var1']);
In Smarty TPL,
在 Smarty TPL 中,
Use {$request_var}
使用 {$request_var}
回答by pencil
BTW: When printing user input, always use the "escape" modifier. Otherwise your application is vulnerable (XSS etc).
顺便说一句:打印用户输入时,请始终使用“转义”修饰符。否则你的应用程序很容易受到攻击(XSS 等)。
{$smarty.post.productid|escape}
{$smarty.post.productid|escape}