|
Cookbook /
HttpVariablesSummary: Access http variables in the page, such as Get variables, Post data, and Cookies.
Version: 1.3 2008-03-20
Prerequisites:
Status: Feature Complete
Maintainer: MartinFick
Categories: PageVariables
Questions answered by this recipeHow can I insert GET, POST, COOKIE or SESSION values into my pages and how can I set or unset cookies and session variables? How can I customize a user's site viewing preferences? AnswerPut httpvariables.phpΔ in your cookbook/directory. Add the following line to local/config.php @include_once("Use the following markup extensions to access GET, POST, REQUEST, COOKIE and SESSION variable values:
Note: that is a tilde, not a dash for the session_var.
Use this directive to set/unset cookies:
Use this directive to set/unset session variables:
BackgroundGET variables are variables that appear after the ? in URLS. They are often used to alter queries or the way that a page looks. They are convenient because they can be added to a link and therefor do not require a form. Naturally this convenience means that GET variables should never be used to affect things permanently (i.e save/delete a document), especially since a crawler is likely to 'click' on every link on your site! GET Example: you may link to the current page while assigning Bar to the GET variable Foo like this:
You may then embed the value of Foo your wiki page with POST variables on the other hand may to be used to permanently modify or post things to a web site. They are normally associated with forms but javascript can also post data to a website. While forms that modify things can use POST, if a form simply performs a search it should be OK to use the GET method in the form instead. POST example: you can create a form in pmwiki with a text field named Username and use the POST method like this: (:input form method=post:) (:input text name=Username:) You may then embed the username in your wiki page with COOKIES are variables that maintain state on the client (browser) side and can be used to create a form of session. Every time a user loads a page from the same place he got a cookie, the cookie is sent back to the server. This is allow a web site to keep track of information about a user. While this can be evil, it can also allow a user to customize the way he interacts with a web site. SESSIONS are similar to cookies but some of the burden of managing session data is transfered to the server side. But sessions do not exist on their own, for sessions to work they need to have at least as small key that is managed on the client side. This is usually done with a cookie, but could also be done by appending GET variables to every link. PHP can manage sessions transparently. REQUEST variables are an all inclusive PHP specific way of referring to variables provide by any of the four methods mentioned above. You can use them if you don't care how the information you seek was submitted. Notes
ExamplesLive exampleYou can see (and edit) a live example of this recipe here. Using Get to divide a long page into separate parts to read:
(:if !equal {$?partToRead} "part2":)
This is part 1 of what could be a long page.
[[{$FullName}?partToRead=part2|View part 2...]]
(:if equal {$?partToRead} "part2":)
You just clicked on part 2 and so are seeing another section of the page.
[[{$FullName}?partToRead=part1|Back to part 1...]]
(:ifend:)
Release NotesVersion 1.3 - 2008-03-20
Version 1.2 - 2008-02-15
Version 1.1 - 2006-07-02
Version 1.0 - 2006-06-06
Comments06-22-2008 - RandyB - Could you give an example of setting a cookie that expires 30 seconds after creation? I tried (:cookie foo bar expires="+30 seconds":) but that didn't work. use singulars in time units: second, minute, day, week, month: (:cookie foo bar expires="+30 second":), and note that each time you reload the page with the cookie directive, the cookie is set afresh with a new expire time! - HansB
09-01-2007 - Renato - So, say that the input for $_GET (maybe others) is "Foo Bar"... It gets \\\"Foo Bar\\\". Any work around for that? Fixed in the latest release, check it out. Martin Fick February 15, 2008, at 02:46 PM
Warning: Cannot modify header information - headers already sent by (output started at /var/www/virtual/netstreams.org/htdocs/devel/cms/pmwiki.php:1070) in /var/www/virtual/netstreams.org/htdocs/devel/cms/cookbook/httpvariables.php on line 39 Any Idea whats wrong? nos? March 20, 2008, at 01:42 PM I don't quite understand what you mean by "in somwhere else than in the main page". If any script/recipe/markup outputs text directly with an echo statement (I don't think that this should happen with a normal pmwiki install) before the (:cookie:) markup is processed than this will be a problem. Could you perhaps setup an example of this problem on the example site? Thanks! Martin Fick March 20, 2008, at 02:40 PM
See link below. I simply added a cookie markup in the sidebar. Link
See Also
Contributors |