简体   繁体   English

setcookie 在 PHP 中从一个页面到另一个页面不起作用

[英]setcookie doesn't work in PHP from a page to another one

Goodmorning, I'm a university student and I'm trying to do a basic exercise about PHP, in particular I'm trying to set a cookie (Country) with the value IT.早上好,我是一名大学生,我正在尝试做一个关于 PHP 的基本练习,特别是我正在尝试设置一个值为 IT 的 cookie(国家)。 The task is to open the page a, then click on the link "pagina successiva" (next page in English).任务是打开页面a,然后点击链接“pagina successiva”(英文下一页)。 The browser will open the page b which should read the value of the cookie Country and and visualize (if exists).浏览器将打开页面 b,该页面应读取 cookie Country 的值并可视化(如果存在)。 In professor's request it is written that I don't have to set an expire time.在教授的要求中,写着我不必设置过期时间。 Here is my code of page a:这是我的页面a代码:

<?php
    $value = 'IT';
    setcookie('Country',$value, 0, "", "", TRUE);
?>
<!doctype html>
<html lang="it">
    <head>
        <meta charset="utf-8">
        <title>Esercizio 10.1 pagina A</title>
        <meta name="author" content="Pippo Baudo" >
        <link rel="stylesheet" type="text/css" href="../sol10_css/lab10_style.css"> 
    </head>
    <body>
        <h1>Esercizio 10.1a</h1>
        <p>Italia!</p>
        <p><a href='10_1b.php'>Pagina successiva</a></p>
    </body>
</html>

Here is my code of page b:这是我的b页代码:

<!doctype html>
<html lang="it">
    <head>
        <meta charset="utf-8">
        <title>Esercizio 10.1b</title>
        <meta name="author" content="Pippo Baudo" >
        <link rel="stylesheet" type="text/css" href="../sol10_css/lab10_style.css">
    </head>
    <body>
        <h1>Esercizio 10.1 pagina B</h1>
        <?php
            if(isset($_COOKIE["Country"])){
                $nazione = $_COOKIE["Country"];
                echo"<p>Il valore del cookie COUNTRY &egrave; $nazione </p>";}
            else{
                 echo"<p class='err'> ERRORE: Cookie \"Country\" assente</p>";
                 echo"<p><a href='10_1a.php'>Pagina precedente</a></p>";}
        ?>
</body>
</html>

The output on the page b says that the cookie is not set, so it is absent. b页上的output说cookie没有设置,所以不存在。

I can't figure out what I'm doing wrong.我无法弄清楚我做错了什么。 Can anyone please halp me?谁能阻止我?

Edit: the error says when I open page a is: Parse error: syntax error, unexpected 'setcookie' (T_STRING) in /app/lab10/10_1/10_1a.php on line 3编辑:当我打开页面 a 时出现的错误是: Parse error: syntax error, unexpected 'setcookie' (T_STRING) in /app/lab10/10_1/10_1a.php on line 3

I assume that you try to access your web page using an insecure ( http ) connection but supplied TRUE as the last parameter which means:我假设您尝试使用不安全( http )连接访问 web 页面,但提供TRUE作为最后一个参数,这意味着:

secure: Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client .安全:表示 cookie只能通过来自客户端的安全 HTTPS 连接传输 When set to TRUE , the cookie will only be set if a secure connection exists.当设置为TRUE时,只有存在安全连接时才会设置 cookie。 On the server-side, it's on the programmer to send this kind of cookie only on secure connection (eg with respect to $_SERVER["HTTPS"] ).在服务器端,程序员只能在安全连接上发送这种 cookie(例如,相对于$_SERVER["HTTPS"] )。

from the PHP Manual ( https://www.php.net/manual/en/function.setcookie.php )来自 PHP 手册( https://www.php.net/manual/en/function.setcookie.php

If you try setcookie('Country', $value, 0, '', '', FALSE);如果你尝试setcookie('Country', $value, 0, '', '', FALSE); or just setcookie('Country', $value, 0);或者只是setcookie('Country', $value, 0); what will leave the default values it should work.什么会留下它应该工作的默认值。 Alternatively, you can access your page using https .或者,您可以使用https访问您的页面。

I would further recommend to use a browser plugin like EditThisCookie (for Google Chrome) for testing and analysing cookie-related issues: https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg我会进一步建议使用像 EditThisCookie(用于 Google Chrome)这样的浏览器插件来测试和分析 cookie 相关问题: https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM