By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

About to Toss my Cookies

P: n/a
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_key)) {
setcookie("sort_key","est_start");
}
?>

<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_key)) {
$sort_key="est_start";
}
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Is register_globals 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***@hotmail.com> wrote in message
news:2m************@turf.turgidson.com...
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_key)) {
setcookie("sort_key","est_start");
}
?>

<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_key)) {
$sort_key="est_start";
}

Jul 17 '05 #2

P: n/a

"Buck Turgidson" <jc***@hotmail.com> wrote in message
news:2m************@turf.turgidson.com...
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_key)) {
setcookie("sort_key","est_start");
}
?>


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($_REQUEST['sort_key'])) {
$_SESSION['sort_key'];
}
else if(!isset($_SESSION['sort_key'])) {
$_SESSION['sort_key'] = 'default';
}
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.