About to Toss my Cookies

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Buck Turgidson

    About to Toss my Cookies

    I am trying to capture and store the value of a sort parameter as one hops
    around various web pages. When they get back to main page, I want the data
    from a database to be sorted the same way as the left it.

    I was thinking to use cookies to retain the sort value, but it is not
    working as I would expect. Another problem is that all the documentation I
    find is either too old or too new for my version (PHP Version 4.3.4).

    Here is what I have tried, but it is not working.


    <?php
    session_start() ;
    if (empty($sort_ke y)) {
    setcookie("sort _key","est_star t");
    }
    ?>

    <HTML>
    <BODY>
    <HEAD>
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    </HEAD>
    <?php

    $sort_key = $_POST['sort'];
    $_SESSION["sort_key"] = $sort_key;
    $sort_key = $_SESSION['sort_key'];



    if (empty($sort_ke y)) {
    $sort_key="est_ start";
    }


  • Kevin

    #2
    Re: About to Toss my Cookies

    Is register_global s on? If not, how does $sort_key get to be not empty in
    the 3rd line?

    Here's one way to do it:
    <?
    session_start() ;
    if($_REQUEST['sort_key'])
    {
    $_SESSION['sort_key']=$_REQUEST['sort_key'];
    }
    $sort_key = $_SESSION['sort_key'];
    if(empty($sort_ key))
    {
    $sort_key='est_ start';
    }
    /* ... Do your database stuff here (don't forget to validate
    $sort_key!)...*/
    ?>

    - Kevin

    "Buck Turgidson" <jc_va@hotmail. com> wrote in message
    news:2mpj92-9j7.ln1@turf.tu rgidson.com...[color=blue]
    >I am trying to capture and store the value of a sort parameter as one hops
    > around various web pages. When they get back to main page, I want the
    > data
    > from a database to be sorted the same way as the left it.
    >
    > I was thinking to use cookies to retain the sort value, but it is not
    > working as I would expect. Another problem is that all the documentation
    > I
    > find is either too old or too new for my version (PHP Version 4.3.4).
    >
    > Here is what I have tried, but it is not working.
    >
    >
    > <?php
    > session_start() ;
    > if (empty($sort_ke y)) {
    > setcookie("sort _key","est_star t");
    > }
    > ?>
    >
    > <HTML>
    > <BODY>
    > <HEAD>
    > <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    > </HEAD>
    > <?php
    >
    > $sort_key = $_POST['sort'];
    > $_SESSION["sort_key"] = $sort_key;
    > $sort_key = $_SESSION['sort_key'];
    >
    >
    >
    > if (empty($sort_ke y)) {
    > $sort_key="est_ start";
    > }
    >
    >[/color]


    Comment

    • Chung Leong

      #3
      Re: About to Toss my Cookies


      "Buck Turgidson" <jc_va@hotmail. com> wrote in message
      news:2mpj92-9j7.ln1@turf.tu rgidson.com...[color=blue]
      > I am trying to capture and store the value of a sort parameter as one hops
      > around various web pages. When they get back to main page, I want the[/color]
      data[color=blue]
      > from a database to be sorted the same way as the left it.
      >
      > I was thinking to use cookies to retain the sort value, but it is not
      > working as I would expect. Another problem is that all the documentation[/color]
      I[color=blue]
      > find is either too old or too new for my version (PHP Version 4.3.4).
      >
      > Here is what I have tried, but it is not working.
      >
      >
      > <?php
      > session_start() ;
      > if (empty($sort_ke y)) {
      > setcookie("sort _key","est_star t");
      > }
      > ?>[/color]

      If you're using session, don't mess with the cookie. To initialize a session
      variable just do

      $_SESSION['sort_key'] = 'something';

      What you want is a "sticky variable." That is, its value changes when the
      variable is present in the request, but is otherwise preserved. The logic
      should be as so:

      if(isset($_REQU EST['sort_key'])) {
      $_SESSION['sort_key'];
      }
      else if(!isset($_SES SION['sort_key'])) {
      $_SESSION['sort_key'] = 'default';
      }


      Comment

      Working...