Welcome to WebmasterWorld Guest from 3.226.251.205

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

setcookie() : not setting

     
1:50 am on Nov 26, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Can you see any reason why this code would not create a cookie called 'auth' and give it a value of 'ok'?

Here is the set cookie code:
* authuser.php *

if ($num!=0){
$cookie_name ="auth";
$cookie_value ="ok";
$cookie_expire ="0";
$cookie_domain ="127.0.0.1";

setcookie($cookie_name,$cookie_value,$cookie_expi
re,"/", $cookie_domain,0);

$display_block ="
<p><strong>Secret Menu:</strong></p>
<ul>
<li><a href=\"secretA.php\">secret page A</a>
<li><a href=\"secretB.php\">secret page B</a>
</ul>";
---------------------------------------------
* secretA.php and secretB.php *
<?

if ($_COOKIE[auth] == "ok") {

$msg = "<P>Welcome to secret page A, authorized user!</p>";

} else {

echo "<p>Not Working</p>";
}

?>

When I click the links for either page I get 'Not Working' rather than the message. I've checked browser settings. This code is straight from the author- so I'm not too sure what's going on.

Thanks-

8:09 am on Nov 26, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


Well, I'm not a cookie 'expert' but you appear to be telling the cookie to expire immediately.

try $expire=(time()+3600); // set for one hour

Nick

6:13 pm on Nov 26, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Yeah, I thought that might be it as well. Setting it to zero means it resets with each visit though right?

I guess I'll have to try another way to finish the example.

Thanks-

7:26 pm on Nov 26, 2002 (gmt 0)

New User

10+ Year Member

joined:Oct 29, 2002
posts:28
votes: 0


Setting expire to 0 means it resets when you close the browser.

Try it with just
setcookie($cookie_name,$cookie_value, 0);

That's a basic cookie. Your domain main be throwing it off.

Also, if you use Mozilla, you can view cookies very easily. Whenever I'm working with cookies, I use mozilla to test.

hiker_jjw

7:55 pm on Nov 26, 2002 (gmt 0)

Inactive Member
Account Expired

 
 


I'm not a PHP expert, but a common cookie mistake is trying to set the cookie after the content/type header.

Content-type: text/html\n\n

You might want to make sure the content type header is after your cookie sets.

9:07 pm on Nov 26, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


hiker_jjw could you please cite any authority that would suggest that the order of the header fields matters?

Neither the PERSISTENT CLIENT STATE HTTP COOKIES Specification [wp.netscape.com], RFC2109 - HTTP State Management Mechanism (obsolete) [faqs.org], RFC2965 - HTTP State Management Mechanism [faqs.org], nor RFC2616 - Hypertext Transfer Protocol -- HTTP/1.1 [faqs.org] seem to suggest that. Quite to the contrary the latter explicitly states:

The order in which header fields with differing field names are received is not significant.

Andreas

12:19 am on Nov 27, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


aaronc- removing the domain variable did it- So now it looks like:

setcookie($cookie_name,$cookie_value,$cookie_expire,"/",0);

Could you tell me why that threw it off? Just want an idea of how this is working.

Thanks for your help

M

12:22 am on Nov 27, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 6, 2002
posts:1092
votes: 0


.domain.com. should end with a .
1:26 am on Nov 27, 2002 (gmt 0)

New User

10+ Year Member

joined:Oct 29, 2002
posts:28
votes: 0


I'm not exactly sure.
I think it's a bug in IE5 where it doesn't handle IP's correctly.

Read the function notes for setcookie at php.net. Someone posted a similar issue.

Lisa: I don't believe that's true, unless I'm misunderstanding your statement.

2:38 pm on Nov 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


Just a note, but I don't think you can get the contents of a cookie you jsut set, it has to be on the next page I think.

Could be wrong tho.

AFAIK the domain does not have to end with a . (never had a problem anyway)

Allen

2:04 pm on Nov 29, 2002 (gmt 0)

New User

10+ Year Member

joined:Aug 22, 2002
posts:38
votes: 0


Try doing a var_dump of the cookie variables in your secret area scripts...

var_dump(_$COOKIE);

Actually, are you sure your version of php supports the new _$COOKIE variable? (They only became available in version 4.1.0 onward)

Try looking at the older $HTTP_COOKIE_VARS array too,

var_dump ($HTTP_COOKIE_VARS);

4:01 pm on Dec 11, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Using setcookie($cookie_name, $cookie_value, 0);
makes everything work out, but why wouldn't I be able to use the other three parameters? If I add back even just $cookie_expire to the mix it will not set.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members