PHP Cookie is not set on remote server

0

I implemented a cookie that works on the local server but does not work on the remote server, I do not 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 } ?>
    
asked by anonymous 09.07.2014 / 15:29

2 answers

1

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

Maybe this is your problem.

path

  

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

domain

  

The domain for which the cookie will be available. Configuring the   domain to 'www.example.com' will make the cookie available   in the www subdomain and in the top subdomains. Available Cookies   to a lower domain such as 'example.com' will be available for   subdomains such as 'www.example.com'. Old Browsers Still   implement RFC 2109 and may require one. at the beginning to   work with all subdomains.

link

    
09.07.2014 / 16:39
0

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

    
08.07.2017 / 01:03