php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24285 COOKIE BUG - VAR NAME IN NAME FIELD CUTS OFF AT A VALUE IF ANY WHEN OUTPUT.
Submitted: 2003-06-22 08:21 UTC Modified: 2003-06-23 06:40 UTC
From: webmaster at microwebsolutions dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.3.2 OS: Windows NT 5 (IIS 4+)
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
40 - 17 = ?
Subscribe to this entry?

 
 [2003-06-22 08:21 UTC] webmaster at microwebsolutions dot com
Description:
------------
Cookies:

I found that you can use multiple cookies, but a bug is preventing me from numbering my cookies using a counter.

Example:

setcookie("This_1_cookie","Testing 1");

As you can see here, the "1" represents a counter, but in this case, there are no $variables in its place; this will output perfect.

Now, take a number or any string with a number in it, and pop it in there.

$i = 1;
$MyCookieName="This_$i_cookie";
setcookie($MyCookieName,"Testing 1");

If you have done exactly as above, you will see the following name in your Cookie file:   This_

It cuts off right before the number..

But if you tried the one at the top, you will notice the entire string is there.

Why is this an issue?



Reproduce code:
---------------
$NewCount=CartCount+1;

# NewCount should be 1 now.. if not, you can make it 1.

setcookie ("CartCount", $NewCount, time()+259200); 

$newcookie="Quantity[1] ItemName[$ItemName] ItemDescription[$ItemDescription] ItemPrice[$ItemPrice] ItemTax[$ItemTax] ItemShipping[$ItemShipping]"; 

$CookieName="Cookie_$NewCount_Name";

# Even if $NewCount was empty, it should still fill in the cookie name with "Cookie__Name" But it doesn't.. It cuts off before the numeric.. BUT ONLY WHEN USING A VAR! I HATE THIS!

setcookie ($CookieName, $newcookie, time()+259200); 

echo "NEW CART ITEM [$NewCount] <br>$newcookie";

}

Expected result:
----------------
In your temporary internet files, you will notice that the COOKIE'S NAME will be cut off before the number, making it impossible to write a number field to a string without SetCookie realizing (I have no idea why, but I can't get it to work..) that it is not something other than a typed string.

Can you guys figure out why I get this bug?

Is there a fix?

Actual result:
--------------
Cookie_
Quantity%5B1%5D+ItemName%5Bnnnnnnn%5D+ItemDescription%5BThe+Item+is+Tall%5D+ItemPrice%5B45%5D+ItemTax%5B5%5D+ItemShipping%5B15%5D%BB
localhost/
1024
533338368
29571867
2694675152
29571263
*


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-22 08:30 UTC] webmaster at microwebsolutions dot com
> Note:  $NewCount=CartCount+1;

The bug still exists; I just wanted you to know that the $ in front of CartCount accidently got erased while editing the script for notations. The script is working and in tact. I haved tested the output and more; the bug is still there.

Nathan
 [2003-06-22 08:37 UTC] webmaster at microwebsolutions dot com
I want to close my submission. 

I was able to find a solution.

Sorry for the report; KEEP ON PHPin! I love it!

I am making a complete shopping cart muahaha!
 [2003-06-22 08:38 UTC] webmaster at microwebsolutions dot com
Closing, read my last message; there was no bug;

It turns out you cant do this with a number:


$MyNumber=1;

$MyStringWithANumber="Testing_$MyNumber_String";

it comes out with:  Testing_

:P  sorry. I fixed it.
 [2003-06-23 06:40 UTC] mgf@php.net
This is really a support issue, but I'm posting here just to set the record straight:

This has *nothing* to do with the value being a number -- it's to do with how PHP parses interpolated variable names.  When PHP sees a $ in a double-quoted string, it collects as many following valid characters as it can to make the variable name; this means that in the string "Testing_$MyNumber_String", it's looking up the value of $MyNumber_String, and replacing that with the correct empty value of the undefined variable $MyNumber_String.

Use {} in your string to delimit the variable name: "Testing_${MyNumber}_String" will give you the right answer.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 13 19:01:27 2024 UTC