Cookie problems

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

Post Reply
sclozza
Forum Newbie
Posts: 14
Joined: Wed Oct 09, 2002 5:11 am

Cookie problems

Post by sclozza »

I have a small HTML form that is used for searching. This sends the inputted data to a PHP script, using POST (Dont know if that is relevant).

What I want it to do is to record the string that I sent to search, and when someone goes to the search page again, it displays their last entered search.

I have got the script creating a cookie fine, but I am having troubles reading it back.

I am using:

Code: Select all

<? 
$search = $_POST&#1111;"searchEntry"];
setcookie("previousSearch", $search, time()+36000, "/30055351/"); 
?>
to create the cookie (The 30055351 is a statically unique number to me). searchEntry is the field from the form. It is before the <html> tags :P

Then in the html page, with the form I have

Code: Select all

<?
if (isset ($previousSearch)) &#123;
echo "<p>" . $previousSearch . ": Your last search</p>"; 
&#125; 

else &#123;
echo "<p>Welcome to this page for the first time.</p>"; 
&#125;
?>
I am probably over looking something very obvious, but I just cant seem to get it to work.

I am using IE6, with Xitami (Windows, here) and Apache(Solaris, remote server).

Thanks for any help
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try accessing the cookie as $_COOKIE['previousSearch'] rather than as $previousSearch.

Mac
sclozza
Forum Newbie
Posts: 14
Joined: Wed Oct 09, 2002 5:11 am

Post by sclozza »

Thank you for your prompt reply!

I changed it to $_COOKIE and it still isnt returning.

Would these 2 factors influence it:

a) The script is in ../scripts/search.php and the form is in ../html/search.html, does the 2 in different directories matter?

b) Does the positioning in the HTML document matter? I know for the php script it is vital before the <html> tags, I currently have it after the submit and reset buttons, near to the bottom of the page.

Thanks.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

yep, cookies will only read a cookie from the same root directory

so if you set a cookie on one dir, you have to read it in that same dir

someone here might know a way around that however
Post Reply