<?php
setcookie("Auction_Item", "Luxury Car", time()+2*24*60*60);
if(isset($_COOKIE["Auction_Item"])){
echo "Auction Item is a " . $_COOKIE["Auction_Item"];
} else{
echo "No items for auction.";
}
?>
Okay so this php code simply sets a cookie and gives the value, I need to write a function that after accepting a boolean parameter as TRUE returns the sum of length of all cookies presented into the request.
if ($val = true){
$cookies = $_COOKIE
$strVal = "";
$countVal = count($cookies);
for ($x = 0; $x < $countVal; $x++) {
$strVal = $strVal . $cookies[$x].$val
}
echo 'Sum of length of all cookie values' . strlen($strVal)
I've tried this, but it doesn't seem to work.
The answer from MaryNfs is correct but missing one important thing from your original code. Try the following:
setcookie("Auction_Item", "Luxury Car", time()+2*24*60*60);
if(isset($_COOKIE["Auction_Item"])){
echo "Auction Item is a " . $_COOKIE["Auction_Item"];
} else {
echo "No items for auction.";
}
$val = true; //Set the variable $val
//You had $val = true, setting $val to true.
//You need to evaluate it as true or false so use === to compare
//Using = sets the variable's value
if($val===true) {
$joinedCookies = "";
foreach ($_COOKIE as $key=>$value) {
$joinedCookies = $joinedCookies . $value;
}
$joinedCookiesLength = strlen($joinedCookies);
echo 'Sum of length of all cookie values ' . $joinedCookiesLength;
}
Once the $_COOKIE
is set, the above should return:
Auction Item is a Luxury Car
Sum of length of all cookie values 36
Your code was missing ;
after three lines which would break things so keep an eye out for that as well.