简体   繁体   中英

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. The task is to open the page a, then click on the link "pagina successiva" (next page in English). The browser will open the page b which should read the value of the cookie Country and and visualize (if exists). In professor's request it is written that I don't have to set an expire time. Here is my code of page 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:

<!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.

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

I assume that you try to access your web page using an insecure ( http ) connection but supplied TRUE as the last parameter which means:

secure: Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client . When set to TRUE , the cookie will only be set if a secure connection exists. 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"] ).

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

If you try setcookie('Country', $value, 0, '', '', FALSE); or just setcookie('Country', $value, 0); what will leave the default values it should work. Alternatively, you can access your page using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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