You are here: Re: setcookie issue (solution!) « PHP Programming Language « IT news, forums, messages
Re: setcookie issue (solution!)

Posted by Snef on 10/08/06 21:49

GregoryD wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:WoCdnd7D8Lw-z7TYnZ2dnUVZ_qednZ2d@comcast.com...
>> GregoryD wrote:
>>> I'm going to try to simplify this issue as much as possible.
>>>
>>> In my public_html/ directory, I have a login script that sets a cookie to
>>> be used for authentication purposes. To set the cookie, it has something
>>> like this:
>>>
>>> <?php
>>> setcookie('code',$value, time + $lifetime, '/', '.example.com');
>>> ?>
>>>
>>> This, to my knowledge, is supposed to set a cookie named code with a
>>> value of $value and an expiration date of time + $lifetime.
>>> Additionally, it is to be read in public_html and all subdirectories over
>>> the entire domain of *.example.com
>>>
>>> Yet if I'm trying to get the value of the cookie, as in:
>>>
>>> <?php
>>> $cookie = $_COOKIE['code'];
>>> echo $cookie;
>>> ?>
>>>
>>> in a subdirectory, say public_html/auth/, the result of the second script
>>> is nothing. If I put that same script in the public_html/ directory,
>>> it'll print out my $value.
>>>
>>> I'm using php 4.3
>>>
>>> Any thoughts?
>>>
>>> GregoryD
>>>
>>>
>> Which browser are you using, and what are the security settings? Cookies
>> have to be received from the browser before your code can access them.
>
> I've tried this on firefox 1.5, ie 6, and Opera 9, and all my security
> permissions in the browsers are set to accept cookies. And yeah, I know
> that cookies have to be received by the browser, which is why I specifically
> pointed out that the code to receive the value in the cookie is in a second
> script. One page has the setcookie, the second page (after a header
> redirect) authenticates it. Sorry if that was a bit confusing.
>
> GregoryD
>
>
Hi,

When you use a header redirect, the just set cookie will not be known in the
redirect page.
You have to use another way to set the cookie before the redirect. Use this
little function to set the cookie and it will work!

function set_cookie($name, $value = '', $expires = 0, $path = '', $domain = '',
$secure = false, $http_only = false)
{
header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
. (empty($expires) ? '' : '; expires=' . gmdate('D,
d-M-Y H:i:s \\G\\M\\T', $expires))
. (empty($path) ? '' : '; path=' . $path)
. (empty($domain) ? '' : '; domain=' . $domain)
. (!$secure ? '' : '; secure')
. (!$http_only ? '' : '; HttpOnly'), false);
}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация