Newbie Cookie Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Newbie Cookie Question

Post by theoph »

What to take the form's action from a Get Method response and put it in a Cookie.

How do I do that?

My purpose is to navigate to the URL (my found set) once I'm done editing one of the found records.

-Dave
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

first off, you should think about the size it can get to.
All the arguments except the name argument are optional. You may also replace an argument with an empty string ("") in order to skip that argument. Because the expire and secure arguments are integers, they cannot be skipped with an empty string, use a zero (0) instead. The following table explains each parameter of the setcookie() function, be sure to read the Netscape cookie specification for specifics on how each setcookie() parameter works and RFC 2965 for additional information on how HTTP cookies work
and what i don't see is that netscape's original specification maxed at 4kb. that's what cookies are based on. that's better than the limitations of get, but may require multiple cookies for your purpose.

i suggest reading more here: http://us3.php.net/manual/en/function.setcookie.php

if you don't know how to access the post variables, i suggest using the search function on this site ot search for book reccommendations. it's at the beginning of every book i've seen.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Code: Select all

<?php
$var = $_GET['var'];
$var = trim($var);
setcookie("variable", $var);
?>
Reference material: http://www.php.net/setcookie , http://www.php.net/trim , & http://www.php.net/manual/en/reserved.v ... iables.get
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I might not have understood you but it sounds like you have a page with a list, you click a link to edit an item in the list page, then you want to redisplay the list.

You could add the url for the list page to the form in a hidden field and then, in the form processor, use a header call to return to that page.

If the form always returns to the same list, ie the url is constant, just add the header call in the form processor.
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Clarification

Post by theoph »

Page 1 has a form with a search textfield that holds the client's search entry. When the "Search" button is pressed the form sends the following get action to page 2.

Code: Select all

index.php?laosSearch=<?php echo $row_rsLaos&#1111;'last'];?>
On page 2 a list is supplied from this get action from the form on page 1. On each line on the list is a link that takes me to another page to do an update for that data, which then takes me to another page that tells me whether or not my update was successful.

From that page I want to have a link that will take me back to the list on page 2 so I can select another entry to edit and update.

That's why I believe I need to use a cookie. It appears then I need to use the $_Get function and have it to insert my form action command into a cookie.

-Dave
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

he was using get as an example.

i'm getting the feeling you've never accessed the arrays before.

try looking up that stuff in your books. it should help.

there's not much we can do witout seeing more
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Got what I needed

Post by theoph »

Got what I needed using the $HTTP_GET_VARS predefined variable. For some reason $_GET didn't work.

Once I retrieved that data I put into a string that I then put into a cookie.

Not I can use that cookie's data and come back to my found set.

-Dave
Last edited by theoph on Thu Jul 31, 2003 4:40 pm, edited 1 time in total.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

what version of php? if you're using $HTTP_GET_VARS instead of $_GET then it's most likely pre 4.1, so you should talk to the host about upgrading to a newer version... some of the upgrades since then have been security related
User avatar
guinness
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2003 1:59 pm
Location: VA

setcookie() has to be called before anything else

Post by guinness »

I've never been able to use $HTTP_POST_VARS or $HTTP_GET_VARS because you have to do something like this:

setcookie ("fname",$HTTP_GET_VARS["variable"],$expiry,"/","domain.com",0);

setcookie() doesn't work. However using $_SESSION["variable"] does. So I just store my variables in the session and pass them along like that.

Hope this helps.

btw, use $HTTP_POST_VARS. You can get more info in your form.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

If $_SESSION works for you, guinness, then you're probably using PHP 4.2+
Don't use $HTTP_POST_VARS, use $_POST and $_GET or $_REQUEST - as demonstrated in this sticky.
User avatar
guinness
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2003 1:59 pm
Location: VA

Saw that post earlier from Jason

Post by guinness »

Thanks. Yeah, saw that post from Jason after I posted my response.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

umm... warning about $_REQUEST[]
it uses the last thing that has something by that ame sot he order of Environment, Get, Post, Cookies, Server matters
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Still Need Help with Cookie

Post by theoph »

I'm working on a web page using Dreamweaver MX where I'm using a form to do a search which redirects the result back to the same page. This it does fine. On each of the rows of the find, I have two links. One takes me to an Update page and the other takes me to a Delete page. Once I'm done editing for example, I have the page to take me to a third page where it shows the updated data for my review. It is from this page, I'm wanting to to have a link that take be back to my "found set" Search page. This is why I want to put the URL & query into a cookie, which can be access from the Update display page.

I using the following script to set my cookie on the Seach page:

Code: Select all

<?php setcookie ("laostest",$PHP_SELF."?".$_SERVER&#1111;'QUERY_STRING']);?>
Works fine when I click on the "Find" button on my Search page form, however, it is giving me my last URL address instead of the current. All I want is for the cookie to equal what is in the current URL address field of my browser.

Can anyone help this poor newbie?
Last edited by theoph on Thu Aug 07, 2003 12:42 pm, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I would think that that might have to do with the nature of cookies. You can't write and read them on the same page (see manual:setcookie), but I might be wrong.
It depends entirely on how you have arranged the PHP-code and whether a page is a PHP-code page or an outputted page (created by the same PHP-code page).
Post Reply