duannaikuang1301 2018-01-07 06:25
浏览 109

为什么在省略或添加空白空格('')作为setcookie()函数中的'expire'参数后没有设置cookie值?

I'm using PHP 7.2.0

I want to omit the expire parameter in setcookie() function while setting the cookie, so I tried below code and got Parse error in output.

<!DOCTYPE html>
<?php
$cookie_n = "user";
$cookie_value = "John Doe";
setcookie($cookie_n, $cookie_value, , "/"); 
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_n])) {
     echo "Cookie named '" . $cookie_n . "' is not set!";
} else {
     echo "Cookie '" . $cookie_n . "' is set!<br>";
     echo "Value is: " . $_COOKIE[$cookie_n];
}
?>

<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>

</body>
</html> 

Output : Parse error: syntax error, unexpected ',' in ... on line 5

Then I tried with below setcookie() code but I got Warning and the cookie didn't get set:

setcookie($cookie_n, $cookie_value, '' , "/");

Output :

Warning: setcookie() expects parameter 3 to be integer, string given in ... on line 5
Cookie named 'user' is not set!

Note: You might have to reload the page to see the value of the cookie.

From manual consider below text about expire parameter,

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

So, according to this I tried the code but it's not working and generating parse error and notice. Please someone help me and correct the mistake I'm making in my code.

I also want to know, whether the cookie value set by omitting expire parameter value will be alive after closing the respective browser tab only and not the entire web browser?

Can I set cookies from Command Line? If yes, how? If no, why?

Thank You.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看