Forum Moderators: coopster

Message Too Old, No Replies

cookies in subdirectories

         

ayushchd

5:47 am on Nov 24, 2007 (gmt 0)

10+ Year Member



Hi, I set a Cookie in the following way :

setcookie("user", $username, $time+3600, '/');

This Cookie is very easily accessible by all the files which are in the root directories..

but when i try to access them from

www.example.com/discussions

These are not accessible.
But on my local server (Apache), it works fine..

Any suggestions?

jatar_k

1:39 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try setting the domain explicitly and see if that helps

setcookie("user", $username, $time+3600, '/', '.example.com');

ayushchd

2:44 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



still didn't work :(

There has been no problem what so ever on my local server!

jatar_k

3:31 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do you get an error?

what code are you using to access them?

ayushchd

12:23 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Hi..now this is really driving me crazy..especially coz it works perfectly on my local server but not on the web server..

The way I set up my cookie..from the file : www.example.com/login.php

setcookie("user", $username, $time+3600, '/', '.example.com');
session_register("password");

Now, there is a forum kind of a thing in : www.example.com/discussion
and the following is a part for navigation settings in the header file in the discussion folder :

if ( (isset($_COOKIE['user'])) && (session_is_registered("password"))) {
echo "

<li>
<a href='../main.php' title='See your portfolio status as on today'>Portfolio</a>
</li>....

Well, if I login and then open this...there is no change in the navigation area...
I even tried print_r($_COOKIE)..nothing was echoed...
All my files in the discussion are to be accessible only if the user is logged in...and as it is..i can't access anything...please help..its driving me nuts!

Thanks.

Ayush

jatar_k

2:02 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



setcookie returns boolean, test to see if it works

be sure that the session_is_registered call is returning true

try changing the name of your cookie to see if there is a conflict

check the config differences between local and web

aside form that, don't worry about that test, just get the cookie set. Once you can confirm it is being set properly.

always remember

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

ayushchd

2:20 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



The cookie is set for sure...because the cookies are accressible from the root directory...and no differences between the web and local config..

ayushchd

4:35 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Well..after a lot of time..there is a bit of success..when i did this
print_r($_COOKIE);

it echoed the set cookie and a PHPSESSID..but this phpsessid does not have the value, i want to have.

however, i wrote print_r($_COOKIE); in the header.php which is included in the index.php in the following way :

ob_start();
session_start();

include('../mysql.php');
//declare header variables
$title = 'Discussion Board';
$script = 'js.js';

include('header.php');

It echoed :

Array ( [PHPSESSID] => 057f6a4dedb0d2e057a4554abf98b9fa [user] => ayush )

But, when I ran header.php individually, it echoed :

Array ( [PHPSESSID] => c52de6392ae187719ac9a782e618be35 [user] => ayush )

The value of second PHPSESSID is correct but i dont know where the first one is from..

jatar_k

5:37 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could be logged in seperately
it could have chaged the session id
you may have more than one cookie, which could be part of the problem

I am not sure why you are setting one value in the session and one in the cookie

why not put both values in the session? (makes me think logic confusion is causing the issue here)