cookie value not shown - php

This is my code:
<?php
$friendid = 10;
$friendname = "enco";
$max=count($_COOKIE['rooms']);
$i = $max + 1;
setcookie("rooms[$i]['type']", "1on1", time() + 3600, "/", ".mywebsite.com");
setcookie("rooms[$i]['name']", $friendname, time() + 3600, "/", ".mywebsite.com");
?>
The code below is in another page:
<?php
$max=count($_COOKIE['rooms']);
$k = 0;
for($k = 0; $k<$max; $k++) {
echo "Cookie 1 show: " . $_COOKIE['rooms'][$k]['type'] . "<br /><br />";
echo "Cookie 2 show: " . $_COOKIE['rooms'][$k]['name'] . "<br /><br />";
}
?>
But it does not work.
When I try to echo the cookies like I did in the example above, nothing appears.
My question is:
Are these structures correct:
setcookie("rooms[$i]['type']", "1on1", time() + 3600, "/", ".mywebsite.com");
setcookie("rooms[$i]['name']", $friendname, time() + 3600, "/", ".mywebsite.com");
in order to show these in another page (not in the same page where the cookies are written):
echo "Cookie 1 show: " . $_COOKIE['rooms'][$i]['type'] . "<br /><br />";
echo "Cookie 2 show: " . $_COOKIE['rooms'][$i]['name'] . "<br /><br />";
Thanks

PHP's superglobals _GET, _POST, _REQUEST, _COOKIE are all created at script startup, and then NEVER modified by PHP for the duration of the script's execution.
The cookie you create with setcookie() will therefore NOT be available in _COOKIE until the NEXT time you run this code.

Related

How to set random cookie value using rand function in PHP

How to set random cookie value using rand function in PHP
I don't want to change the value while page refreshed when it once assigned.. until cookie destroy
My code is as follows
<?php
global $random;
$random= rand(0, 9999999);
if(!isset($_COOKIE[$random])) {
setcookie('user_cookie',$random, time() + (1), "/"); echo $_COOKIE[$random];
}
else {
echo "Cookie '" . $_COOKIE[$random] . "' is set!<br>";
}
exit();
?>
if(!isset($_COOKIE['lg'])) {
setcookie('lg', rand(1,10000), time() + (86400 * 30), "/"); // 86400 = 1 day
}
echo $_COOKIE['lg'];
You can check if it is not set then set it.
<?php
define('COOKIE_KEY', 'COOKIE_KEY');
if (!array_key_exists(COOKIE_KEY, $_COOKIE)) {
setcookie(COOKIE_KEY, mt_rand(1, 10), time()+3600);
}
Your set cookie code is incorrect and the cookie time is only 1 second
change your code as follows...
<?php
//cookie_start();
global $random;
$random= rand(0, 9999999);
if(!isset($_COOKIE['user_cookie'])) {
setcookie('user_cookie',$random, time() + (86400), "/");//86400 = 1 day
}
else {
echo "Cookie '" . $_COOKIE['user_cookie'] . "' is set!<br>";
}
exit();
?>

Retain the value of a random generated number

I am learning php atm and i decided to make a simple game but now im confronted with a problem. I have the following code:
<form action="wolf.php" method="POST">
<input type="submit" value="Attack" name="submit"/> <br />
<?php
$hp = 100;
if(isset($_POST['submit'])) {
$attack = $_POST['submit'];
$damage = mt_rand(5, 30);
$newhp = $hp - $damage;
if ($attkdamage = $hp - $damage ) {
echo "Your HP is: ". $newhp . "<br / >";
echo "You took: " . $damage . " damage!";
}
}
?>
As you can see I have a variable with an integer (100) and a simple mt rand. What I want is that after I submit and get a $newhp (100 - the random number), that number to replace $hp. and the next time I submit the button I want the value of $damage to be subtracted from the previous action, so basically to save the $newhp as $hp.
If possible storing this value in a session variable that will be saved in the browser you use, for more information read here $_SESSION
<?php
session_start();
if(!isset($_SESSION["hp"]))
$_SESSION["hp"]=100;
if(isset($_POST['submit'])) {
$attack = $_POST['submit'];
$damage = mt_rand(5, 30);
$newhp = $_SESSION["hp"] - $damage;
if ($attkdamage = $_SESSION["hp"] - $damage ) {
echo "Your HP is: ". $newhp . "<br / >";
echo "You took: " . $damage . " damage!";
}
echo "<pre>previus hp " .$_SESSION["hp"];
$_SESSION["hp"]= $newhp;
echo "next hp " . $_SESSION["hp"]."</pre>";
}
?>

Can Someone help me resolving this increment/decrement issue [duplicate]

This question already has answers here:
What's the difference between ++$i and $i++ in PHP?
(15 answers)
Closed 7 years ago.
I am new to php, for some reason the increment/decrement works inversely. Can someone tell me the reason behind this?
<?php
$var1 = 3;
echo "Addition = " . ($var1 += 3) . "<br>" ;
echo "Subtraction = " . ($var1 -= 3) . "<br>" ;
echo "Multiplication = " . ($var1 *= 3) . "<br>" ;
echo "Divison = " . ($var1 /= 3) . "<br>" ;
echo "Increment = " . $var1++ ;
echo "Decrement = " . $var1-- ;
?>
If you know how the increment and decrement operators work, you'll get the answer.
echo "Increment = " . $var1++ ; //Prints $var1 and then increments the value
echo "Decrement = " . $var1-- ; // Prints the incremented value from previous operation and then decrements the value
To achieve what you are trying to do, use --$var1

Why doesn't the html br break line tag doesn't work in this code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can some one tell why my php line break not working ( echoing ) ?
I know i can write the code in a different way to make the line break work, but i want to know the reason behind this ?
<?php
$var1 = 3;
echo "Addition = " . $var1 += 3 . "<br>";
echo "Subtraction = " . $var1 -= 3 . "<br>";
echo "Multiplication = " . $var1 *= 3 . "<br>";
echo "Division = " . $var1 /= 3 . "<br>";
?>
Well seems like I have to clean some things up here.
Let's take a look at the operator precedence, which says:
. has a higher precedence, than +=, -=, *=, /=
. is left associative
=, +=, -=, *=, /= is right associative
We also take a look at the note at the bottom of the manual:
Note:
Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
Means that even tough = has a lower precedence than . it gets evaluated first. You can also see this if you do something like this:
$xy = "HERE";
echo "I am " . $xy = "NOT HERE";
Now you would think that . has a higher precedence than = and will get evaluated first, but as from the note in the manual, the assignment is first and you end up with this:
echo "I am " . ($xy = "NOT HERE");
output:
I am NOT HERE
So if we put all these information's together, we can say, that the assignment gets evaluated first, but it's right assocative. Means this:
$var1 = 3;
echo "Addition = " . ($var1 += 3 . "<br>");
echo "Subtraction = " . ($var1 -= 3 . "<br>");
echo "Addition = " . ($var1 *= 3 . "<br>");
echo "Addition = " . ($var1 /= 3 . "<br>");
So this code will end up in this:
echo "Addition = " . ($var1 += "3<br>");
echo "Subtraction = " . ($var1 -= "3<br>");
echo "Addition = " . ($var1 *= "3<br>");
echo "Addition = " . ($var1 /= "3<br>");
Which then through the arithmetic operator gets convert to an integer we end up with this:
echo "Addition = " . ($var1 += 3);
echo "Subtraction = " . ($var1 -= 3);
echo "Addition = " . ($var1 *= 3);
echo "Addition = " . ($var1 /= 3);
And after the assignment is done the concatenation gets evaluated, which looks like this:
echo "Addition = " . 6;
echo "Subtraction = " . 3;
echo "Addition = " . 9;
echo "Addition = " . 3;
With this you end up in this output:
Addition = 6Subtraction = 3Addition = 9Addition = 3
And now how to solve this? Simply wrap your assignment in parentheses, so that the <br> tag doesn't get into the assignment. E.g.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) . "<br>";
echo "Multiplication = " . ($var1 *= 3) . "<br>";
echo "Division = " . ($var1 /= 3) . "<br>";
//^ ^ So the br tag doesn't get in the assignment of the variable.
This is happening because of the type casting issues. 3 . "<br>" will be converted to number while the operation will be performed. Wrap the inside () so that the operations are performed first then the concatenation.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
You can use commas,
echo "Addition = " . $var1 += 3 , "<br>";
echo "Subtraction = " . $var1 -= 3 ,"<br>";
echo "Addition = " . $var1 *= 3 , "<br>";
echo "Addition = " . $var1 /= 3 ,"<br>";
Or wrap it in brackets:
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Otherwise the 3 number is concatenated with <br>.
Your PHP means:
echo "Addition = " . $var1 += (3 . "<br>");
echo "Subtraction = " . $var1 -= (3 ."<br>");
echo "Addition = " . $var1 *= (3 . "<br>");
echo "Addition = " . $var1 /= (3 ."<br>");
And number + 3 . '<br>' is number + (int)(3 . '<br>') which is number + 3. No <br> exists now due to retyping to number(converting to number).
Use brackets around equations.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Try this..
"." is used for php variable to concate not for numbers
<?php
$var1 = 3;
echo "Addition = ". ($var1 += 3) ."</br>";
echo "Subtraction = ". ($var1 -= 3) ."</br>";
echo "Addition = ". ($var1 *= 3) ."</br>";
echo "Addition = ". ($var1 /= 3) ."</br>";
?>
Try this way.
<?php
$var1 = 3;
echo "Addition =" . ($var1 += 3 ).'<br>';
echo "Subtraction =" . ($var1 -= 3).'<br>';
echo "Addition =" . ($var1 *= 3 ).'<br>';
echo "Addition =" . ($var1 /= 3 ).'<br>';
?>

My php code not getting add correct

I making one counter, I am using PHP and almost everything was going fine but i found one small bug in it.
My first two step on counter is going fine that is $bid and $stax.
My last result $pay should be : 8138 + 814 + 448 = 9400 but it is giving me $9,399
My Output is:
Value $8,138 bid $814 tax $448 You Pay $9,399
Value $8,952 bid $895 tax $492 You Pay $10,339
Value $9,847 bid $985 tax $542 You Pay $11,373
Here is my php
<?php
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = $v / 10; // output : $814
$ftax = $v + $bid;
$stax = $ftax / 20; // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = $v * 1.1;
} while ($i <= 2);
?>
Thanks in advance :)
Change your echo line to not drop decimals:
echo "Value $" . $v . " bid $" . $bid . " tax $" . $stax . " You Pay $" . $pay . "<br />";
Value $8138 bid $813.8 tax $447.59 You Pay $9399.39Value $8951.8 bid $895.18 tax $492.349 You Pay $10339.329Value $9846.98 bid $984.698 tax $541.5839 You Pay $11373.2619
So 8138 + 813.8 + 447.59 = 9399.39, which rounds down to 9399.
On the other hand, if you round immediately, 813.8 rounds up to 814 and 447.59 rounds up to 448, but you've just added 0.61 to your calculation before even starting, which obviously results in a higher number (9400).
This is why math and science teachers tell you not to round until the very end, since each time you do it, your answer gets less accurate.
if you dont want to use decimals, round off all division calculations by using round(). Also, multiplications with decimals. See it in action here
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = round($v / 10); // output : $814
$ftax = $v + $bid;
$stax = round($ftax / 20); // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = round($v * 1.1);
Lack of casting/decimals be ye problem matey. har har har

Categories