[英]Php - Search a string in cookie
I set cookie with php, using 我用PHP设置cookie,使用
setcookie("name", "John Mayer", time()+31556926 , "/");
After, I want to search Cookie value with php to check if it includes a value, using 之后,我想使用php搜索Cookie值以检查它是否包含值,使用
if (strpos($_COOKIE["name"],"Mayer")) {
However, it always returns false. 但是,它始终返回false。 Is it not allowed to check cookie value with php ? 是否不允许使用php检查cookie值? If so is there anyway to check it ? 如果可以的话,有没有检查一下?
You edit your code. 您编辑代码。
if(isset($_COOKIE["name"]) && !empty($_COOKIE["name"])) {
if (strpos($_COOKIE["name"], 'Mayer') !== false) {
echo 'exists';
}
}
check if its set and proceed 检查其设置并继续
if(isset($_COOKIE["name"])) {
if (strpos($_COOKIE["name"], 'Mayer') !== false) {
// it exists
echo 'true';
}
}
Actually strpos returns a index so,if string matched from initial then it returns 0 which is zero and considered as false. 实际上strpos返回一个索引,因此,如果字符串与初始字符串匹配,则返回0,该值为零,并被视为false。 It returns -1 if search text didnot matched, so you can make a condition like- 如果搜索文本不匹配,则返回-1,因此您可以将条件设为-
if (strpos($_COOKIE["name"],"Mayer")>=0)
{
}
// or strict comparison with false
if (strpos($_COOKIE["name"],"Mayer")!==false)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.