PHP Cookie is not set on remote server

Asked

Viewed 795 times

0

I implemented a cookie that works on the local server but doesn’t work on the remote server, I don’t understand why. Thanks for any help. I have the following code:

<?php


            if (isset($_POST['name']) && !empty($_POST['name'])) {
                $firstName = ucwords($_POST['name']);
                setcookie('Cookie', $firstName, time() + 60 * 60 * 24 * 30);

                insertData($firstName);
                $last = mysqli_insert_id($db);

                ?>
<h1>Hello, <?php echo $firstName; ?>! How are you?</h1>
<form action="" method="POST">
                            <input id="email" name="email" type="text" value='' autofocus="autofocus">
                            <input name="id" type="hidden" value="<?php echo $last; ?>">
                            <input name="name2" type="hidden" value="<?php echo $firstName; ?>">
                            <button name='submit2' type="submit"></button>
                        </form>

<?php
}



else if (isset($_POST['submit2'])) {
            $mail = $_POST['email'];
            $last = $_POST['id'];
            $firstName = $_POST['name2'];
            updateData($mail, $last);

            ?>
<h1>Awesome, <?php echo $firstName; ?>. I’ll keep you updated.</h1>

<?php }




else if (isset($_COOKIE['Cookie'])) { ?>
<h1>Hi, <?php echo $_COOKIE['Cookie']; ?>. Welcome back.</h1>


<?php }




else { ?>
<h1>Hello, my name is Lisa.</h1>
<form id="form1" action="" method="POST">
                        <input id="name" name="name" type="text" autocomplete="off" autofocus="autofocus">
                        <button id="button1" type="submit"></button>
                    </form>

<?php } ?>

2 answers

1

In his setcookie you can identify the path and also the domain, so that the cookie can be stored and also read by your server.

Maybe that’s your problem.

path

The path on the server where the cookie will be available. If configured for '/', the cookie will be applicable to all Omain. If configured for the directory '/foo/', the cookie will be available only within the directory /foo/ and all subdirectories as /foo/bar from Domain. The default value is the current directory where the cookie is being configured.

Domain

The domain for which the cookie will be available. Setting domain for 'www.example.com' will make the cookie available in the www subdomain and the upper subdomains. Cookies available for a lower domain, like 'example.com' will be available for top subdomains such as www.example.com. Older browsers still implement » RFC 2109 and may require a . at the beginning for work with all subdomains.

http://www.php.net/manual/en/function.setcookie.php

0

Cookies should be placed at the beginning of the page, it loads before other elements. In the case of your code, create a session_start() and then pass the data to the cookie.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.