SetCookie() Problem

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
User avatar
alsaffar
Forum Newbie
Posts: 16
Joined: Sun Sep 29, 2002 12:03 pm

SetCookie() Problem

Post by alsaffar »

Hi there,

I Have the following code:

<?
$loginUserName = "Ali";
setcookie ("CkUserName", $loginUserName, time()+3600, "/", ".127.0.0.1",1);
$CkUserName = $HTTP_COOKIE_VARS["CkUserName"];
echo "UserName: $CkUserName";
?>

The cookie set successfully, but I can't retrieve the information from it! (echo is not showing the data from the cookie I set).

Please any help?
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Try this

Code: Select all

<?php

echo "$_COOKIE["CkUserName"]";


?>
Yor need that $_COOKIE.
User avatar
alsaffar
Forum Newbie
Posts: 16
Joined: Sun Sep 29, 2002 12:03 pm

Post by alsaffar »

Thank you oldtimer for your help,

Its working fine only the first time the cookie is created with this code:

<?
$loginUserName = "Ali";
setcookie ("CkUserName", $loginUserName, time()+3600, "/", ".127.0.0.1",0);
$CkUserName = $HTTP_COOKIE_VARS["CkUserName"];
echo "UserName: $_COOKIE[CkUserName]";
?>

But if I change the code for the variable $loginUserName to:

$loginUserName = "Ali123";

STRANGE!!! Still give me the old cookie value "Ali".

Also if I delete the cookie from Cookies folder (WinXP) still it gave me the old value, although I checked that data in the new cookie is Ali123 but still with echo it gave me only Ali!!!

Any Idea?

Thank you
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you read the documentation on setcookie()?
PHP manual wrote: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).
Mac
Post Reply