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!
Ehh.. I'm trying to make sure the person logging in and viewing logged in features keeps cookies on the entire time. To check, im making a random generated cookie and cookie value each page load. The problem is its not picking up the previous page's randomly generated variables. I know they are matching and passing through the page because i echoed the variables and all came out as they were on the previous page. this is the code i am using:
Pass the setcookie() method all the params it needs (except the secure param, unless you need it), some browsers are very picky about how cookies are set.
Although it's probably best to insert all of the information the cookie needs, its not needed.. I just tested using basic values "1" and "2" and it worked..
<?php
setcookie("1","2");
if ($_COOKIE['1'] == "2") {
echo "cookie was stored";
}else{
echo "Cookies must be enabled to login";
die();
}
?>
I dont think thats the problem...
I even tested using " print_r($_COOKIE); " again after copying some of the generated variables, and they did match on the array... Everything seems to be fine, i dunno =/
Could it have been because of the double =? because it isnt equal to, its value is... I removed one equal sign and it worked, but is that what was wrong?
If I understand you correctly, in that scenario you will be setting "$_COOKIE[$prevnm]" to the value of " $prevve".
You are also creating a var called $chars containing a string, then later on, you are creating an array with the same var name and trying to use the string as one of the elements in the array:
Well, i already know the value of $chars is "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" because i identify it at the beginning, but ill test anyways. Okay, thats what i got "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"... The other 2 variables, $cookname and $cookvar are randomly generated variables that are always something different... They are carried over to the next page as the new variables "prevnm" and "prevve"..These are checked in the if statement and if they match the previous setcookie then it works, and if they dont, it doesnt... Im pretty sure i got it working though, i removed a = from the if statement: if ($_COOKIE[$prevnm] = $prevve) { and now it gives me the proper messages..
Removing an equal sign means that you set $_COOKIE[$prevnm] to be equal to $prevve and didn't compare the two values. It will now work no matter what $_COOKIE[$prevnm] is equal to. To test whether one value is the same as another you have to use the equality operator (==).
Ohh.. I wasn't aware that using a single equal sign in an if statement would actually give it that value, anyways, after using the print_r i noticed the array was giving a '0' on some of the values, i dont know what i changed, but it works with the double equal sign so it must be... Also, sometimes i see stuff like "PHPSESSID=945bfa09980940902781479afd74c416" in the url, and i dont understand why. Its only sometimes, not always. I have cookies enabled... Anyways heres the working code, im pretty sure its doing everything right