READ CFCOOKIE with PHP

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
webacadie
Forum Newbie
Posts: 6
Joined: Mon Jan 23, 2006 10:59 am

READ CFCOOKIE with PHP

Post by webacadie »

Is there a way to read a cookie made with CFCOOKIE in php?

I'd try

$user = $HTTP_COOKIE_VARS["webacadie"];

<?php print "our cookie says $user";?>

but I always have a blank like if my cookie is empty

If I try

<?php print_r($_COOKIE); ?>

The result is Array ( [WEBACADIE] => dskit )

and "dskit" is the value I'm trying to get

Any suggestion??

The fun of working on a system that's using CF and PHP is really something .... The website is all CF but client both a plugin that is made with PHP, I have to pass some value from CF to PHP and I can't do it via form .....
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

$HTTP_COOKIE_VARS is deprecated.... use $_COOKIE[] instead ;)

Code: Select all

if (!empty($_COOKIE['WEBACADIE']))
{
    echo 'Cookie says '.$_COOKIE['WEBACADIE'];
}
webacadie
Forum Newbie
Posts: 6
Joined: Mon Jan 23, 2006 10:59 am

Post by webacadie »

Thanks a lot, it is working :D
webacadie
Forum Newbie
Posts: 6
Joined: Mon Jan 23, 2006 10:59 am

Post by webacadie »

Now if I want to put my cookie resutl in my query?

$sql = 'SELECT id, nom, client
FROM client
where datasource = $_COOKIE['WEBACADIE'];

or even

$conn->open('dswebacadie');
$user = $_COOKIE['WEBACADIE'];
$sql = 'SELECT id, nom, client
FROM client
where datasource = $user';
$rs = $conn->Execute($sql);

It doesn't work that way
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can't put variables in single quotes directly. PHP treats single quotes (string literal) as literal strings and so won't parse any variables inside them. If you use double quotes however PHP will parse variables.

It gets a bit confusing when you are referring to associative array values in double quotes strings though:

Quick demo....

Code: Select all

$foo = 'bar';

echo 'sheep say $foo'; //sheep say $foo
echo "sheep say $foo"; //sheep say bar

$foo = array();
$foo['bar'] = 'cheese';

echo "the moon is made of $foo['bar']"; //Gives error due to named key
echo "the moon is made of $foo[bar]"; //The moon is made of cheese (notice lack of single quotes on key?)
echo "the moon is made of {$foo['bar']}"; //the moon is made of cheese
As you can see there are different way to play with strings. If I'm putting variables inside a double quoted string I'll usually opt for the curly brace syntax {} since it doesn't bail in method calls or arrays. Otherwise I'll just concatenate....

Code: Select all

echo 'the moon is made of '.$foo['bar']; //the moon as made of cheese
What was the question again? :P
Post Reply