Cookies in folders...

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
ojeffery
Forum Newbie
Posts: 4
Joined: Mon Apr 24, 2006 9:56 am

Cookies in folders...

Post by ojeffery »

Hi all,

I've set a cookie, which is working fine, but it's only accessable from the folder of the page I set it in (in this case the admin folder). I didn't specifically tell it to set in that folder, but it only works on files in there. I need to be able to access it from all folders in the site. What am I doing wrong? :cry:

Cheers, Oli.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Read the PHP manual: $_SERVER['DOCUMENT_ROOT'] and use it in the code to indicate from where (the admin folder) to read the cookie. You must check the user rights for that folder. You might change to session and create a special directory for the sessions.
ojeffery
Forum Newbie
Posts: 4
Joined: Mon Apr 24, 2006 9:56 am

Post by ojeffery »

Thanks, but I still can't get it to work. I've set the cookie as follows:

Code: Select all

setcookie ("id", $get_info[0], 0, '/');
It's set in a file called login.php within the /admin folder.

I've set up a test page to find out if the cookie has worked properly, which just picks up the cookie and prints it. I've installed it with the same code in the /admin folder and the root folder, and it only works in the former.

The code for test.php is as follows. Is there perhaps another way of accessing it?

Code: Select all

<?php 

$id=$_COOKIE['id'];

print "$id";

?>
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

You can retrieve information from the directory using fopen or get_content

Code: Select all

$path=$_SERVER['DOCUMENT_ROOT']  ."/admin/cookie.txt" ;
fopen($path, "r");
//or
obj=get_content ($path);
I suggest you read the comments on this page: http://php.net/fopen
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Cookie information isn't stored on the server.
Post Reply