-1

I am currently working on a little website on which I have to make an account creation system. In order to know whether the user is already connected or not I would like use cookies cuz this was looking like the easiest way. However, after checking the official documentation on php.net, and some other forums, I'm still not able to create my cookie. Here is my code :

function connexionOK() {
  $cookieName = "CookieCo";
  $cookieValue = "true";
  $isSent = setcookie($cookieName, $cookieValue, time() + 3600, "/");

  if($isSent) {
     echo '<script type="text/javascript">';
     echo "alert('Cookie envoyé');";
     echo "</script>";
  }
  else {
     echo '<script type="text/javascript">';
     echo "alert('Cookie failed');";
     echo "</script>";
  }}

So the page is indeed alerting me 'Cookie failed'.

Thanks for everyone who've been trying to help me ! :)

FabienLge
  • 3
  • 1
  • 3
  • 2
    From the manual: `If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.` Looks like you have output before you call setCookie() – John Conde Nov 28 '17 at 20:29
  • 1
    Worked for me... you need to make sure to call setcookie before any output is sent aka no echos or prints before – Phillip Weber Nov 28 '17 at 20:31
  • As add on to John's comment, or you also can't set and check on the same page. You must reload the page (without setting again) or check on a different page – Andreas Nov 28 '17 at 20:32
  • Also If you need to keep your existing output statements you can use [output buffering](http://php.net/manual/en/ref.outcontrol.php) – Accountant م Nov 28 '17 at 20:41

2 Answers2

0

Try

if(isset($_COOKIE['CookieCo'])){
     echo '<script type="text/javascript">';
     echo "alert('Cookie envoyé');";
     echo "</script>";
  }
  else {
     echo '<script type="text/javascript">';
     echo "alert('Cookie failed');";
     echo "</script>";
  }
El Dj
  • 385
  • 1
  • 7
  • 22
  • That will only work on a second request after the browser had received the headers of the first request, and sent the cookies back for the second request. Not what the questioneer asked. – IncredibleHat Nov 28 '17 at 20:47
  • Do or do not, there is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Nov 28 '17 at 21:10
0

You may be calling this function after headers were already sent:

try this and see if you get an exception:

function connexionOK() {
    $cookieName = "CookieCo";
    $cookieValue = "true";
    if(headers_sent()){
        throw new Exception('Cannot set cookie, headers already sent');
    }else{
        $isSent = setcookie($cookieName, $cookieValue, time() + 3600, "/");
        if($isSent){
            echo '<script type="text/javascript">';
            echo "alert('Cookie set');";
            echo "</script>";
        }else{
            echo '<script type="text/javascript">';
            echo "alert('Cookie not set');";
            echo "</script>";
        }
    }
}

connexionOK();

If so, you will need to refactor and make sure you setcookie before headers are sent.

EDIT: The above code was included to more clearly illustrate the problem - it is poorly written and includes a lot of redundancy as calling set setCookie while headers are sent will already throw an exception and probably terminate execution. If this is NOT the case, you will need to add checks and handle them accordingly.

Phillip Weber
  • 554
  • 3
  • 9