0

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

1 Answers1

2

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 (e.g. 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

Johannes
  • 1,478
  • 1
  • 12
  • 28
  • It works! Thanks a lot. I have undesrtood the problem about https, but do you know how to set a secure channel? The university provided me the server where I can upload HTML, PHP, CSS files and work on it... How can I establish an HTTPS instead of HTTP? – Davide Gariglio Jun 12 '20 at 08:37
  • You can check if a valid certificate was mainted for this by just calling the page as `https://yourpage.com/your_path` instead of `http://yourpage.com/your_path`. This will tell the browser to try to establish a secure connection. If this works, you can redirect your page permanently to `https` using e.g. the reply of this SO post: https://stackoverflow.com/questions/4398951/force-ssl-https-using-htaccess-and-mod-rewrite – Johannes Jun 12 '20 at 08:42
  • Thank you so much. The server tells me that it is impossible to establish a secure connection. However I understand. Thank you very much, I've been very clear in tour explenation. – Davide Gariglio Jun 12 '20 at 09:13
  • If you want to try it out and develop locally, I can recommend XAMPP (https://www.apachefriends.org/de/index.html). It's free, works on Windows/Mac/Linux and very easy to install. There you can play around with some settings without the need to rely on a proper server configuration done by others. – Johannes Jun 12 '20 at 09:22