SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    best practice to pass variable

    is it a good practice to pass variables from pages to pages with session variables? or is it better to pass variables through $_GET url variables?

    i am in a situation where say the users can do some filters and stuff to a table of books. then when the user clicks to see more info about the book from the table and return back to it, the filters still stay and is not reset.

    similarly for the pagination, when the user clicks on the other pages, the filters have to stay or it wouldn't be right.

    so in these situations, i guess there is definitely a need to pass data from one page to the other. using session variables seem so much easier for me rather than keep recording the GET variables. however, i am wondering what happens if the user's browser does not have cookies enable, then wouldn't the session variables will not be working too...

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, its not good practice to pass "messages" from one page to the next using sessions; although sometimes it may be nessecary.

    When considering this try to think about how the users will experience your site if they used multiple windows/tabs (sessions are shared between tabs/windows so can they "steal" messages from eachother?); how bookmarking a page will work (hint: session state is not bookmarked), how the browser "back" button will work, how page refreshes will work etc.

    Session state should be used for truly session-wide state such as logged-in status etc.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the situation you describe, you should definitely use GET parameters, or you will break a lot of the browsers capabilities, such as bookmarking, back/forth button, multiple windows etc. etc.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am using the mod_rewrite for my urls and so the queries are actually passed through the url's name(so call nicer paths in the url). so when i want to continue to bring the present queries to the next page by using $_SERVER['QUERY_STRING'], it seems to be quite difficult to put the variables to form the "nicer" url.
    i need to form the url into...
    http://www.domain.com/gallery/galler...e/many/filters instead http://www.domain.com/gallery.php?id...1=1&filter2=0&......

    but to form the variables into the "nice" url from the present get variables seems difficult to me. is there a way to do this in a more clever manner?

    thanks.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can deal with this by writing a function, which generates URLs for you. This function would obviously know about the rewrite rules. Just a simple example, to illustrate:
    PHP Code:
    function url($href$args = Array()) {
      foreach (
    $args as $key => $value) {
        if (!
    is_null($value)) {
          if (
    is_integer($key)) {
            
    $params[] = rawurlencode($value);
          } else {
            
    $params[] = rawurlencode($key)."=".rawurlencode($value);
          }
        }
      }
      if (
    count($params) > 0) {
        if (
    strpos($href"?") === FALSE) {
          return 
    "?".implode("&"$params);
        } else {
          return 
    "&".implode("&"$params);
        }
      }
    }
    // http://www.domain.com/gallery.php?id=1&pg=2&filter1=1&filter2=0&
    url("gallery.php", Array('id' => 1'pg' => 2'filter1' => 1'filter2' => 0)); 
    This will generate a "normal URL. You can then modify url() to generate it in your rewrite-scheme.

    That all said, I think you clearly went overboard with the URL-rewriting here. If the parameters are optional, and non-hierarchical in nature, they don't really belong in the path-part of your URL. They rightfully belong as queryString parameters (GET-params in PHP-lingo). A good measure is to look at if the order of the parts of the path is random, or has meaning. If the order doesn't matter, then you should probably have used the queryString instead.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To answer your first question - passing variable in GET seems to make sense for the case(s) you outlined.

    To answer your second question about nicer URLS...

    My company has been coding page "normally" - creating ugly urls like:
    example.com/blog/view.php?blog_id=123&comment_added=true

    ...that url represents how we'd display a success message after a user added a comment to blog ID #123.

    The PHP code might look for $_GET['blog_id'] or $_GET['comment_added'] variables.

    Then, we just throw in some rewrite rules:
    Code HTML4Strict:
    RewriteRule /blog/view/(\d+)/([^/]+)/comment_added  /blog/view.php?blog_id=$1&comment_added=TRUE [L,QSA]

    Now, when we send the user to /blog/view/123/this_is_the_title_of_my_blog/comment_added - Apache redirects behind the scenes without ever changing the URL to the user. All the PHP code that looked for $_GET['blog_id'] still works --- just make sure you call the QSA rewrite flag.

  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how does the QSA flag work? i googled for it and it says something like query string append but i am still not very sure how it works.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'qsappend|QSA' (query string append)
    This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
    http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
    (search the page for 'flags')

    It allows you to place rewrite matches in the query string.

    Your URL might look like this to the browser:
    /users/edit/123/dave

    But mod_rewrite makes it look like this to PHP:
    /users.php?function=edit&user_id=123

    Once you start moving URL parts to the query string, I believe, that you need the QSA flag.

  9. #9
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does that mean i could have this now...
    index.php?user=123&function=edit

    then i can pass and append these query strings into another url..
    users.php?view=profile&user=123&function=edit

    is this right?

  10. #10
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by x[E]nOn View Post
    does that mean i could have this now...
    index.php?user=123&function=edit

    then i can pass and append these query strings into another url..
    users.php?view=profile&user=123&function=edit

    is this right?
    You sure can, but in an abstract sense, when you start passing it from page to page a lot, it's a primitive kind of session handling in my opinion. Compared to standard PHP session handling, it has the advantage that you don't get interference if there are different windows and and tabs open on the same application.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  11. #11
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but i still don't see how i could do this appending of query strings with the QSA flag?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •