Javascript redirect and set cookies

Asked

Viewed 365 times

1

I have the following code with Javascript

<script type="text/javascript">
  $(function() {
    $("#loginForm").submit(function(event) {
      $("#success").css("display", "block"), $('#success').html("Autenticando...");
      $('#info').hide();
      $('.form-group').hide();

      var rootUrl = "<?php echo URL_BASE; ?>";

      var username = $("#username").val();
      var password = $("#password").val();

      $.ajax( {
        method: "POST",
        url: "api/login.php",
        dataType: "JSON",
        data: $(this).serialize(),
        success: function(i) {
          if (i["status"] == "success") {
            $("#error").css("display", "none");
            location.href = rootUrl + "/options/?welcome=true";
          } else {
            if (i["status"] == "error") {
              $("#error").css("display", "block"), $("#error").html(i["message"]);
              $("#success").css("display", "none"), $("#success").empty();
              $(".form-group").show();
              $("#info").show();
              return false;
            }
          }
        }, error: function (error) { console.log(error); }
      });
      return false;
    });
  });
</script>

And in my PHP:

if (isset($cookies['auth_token'])) {

  Cookies::set('auth_token', $cookies['auth_token']);
  Cookies::set('user', $ttrUsername);
  Cookies::set('pass', $ttrPassword);

  $_SESSION[SITE_NAME . '_SESSION'] = $ttrUsername;

  echo json_encode(array(
            "status"          => "success",
          "message"     => "Autenticação bem sucedida, estamos te redirecionando.",
        ));

} else {
    echo json_encode(
                array(
                    "status" => "error",
                    'message'=> "Não foi possível autenticar com o Twitter.",
                ));
} 

The Problem that the Javascript redirects but does not take Cookies together, how to solve this?

EDIT 1

Where

Cookies::set('auth_token', $cookies['auth_token']);

Is equivalent to:

setcookie('auth_token', $cookies['auth_token'], time() + (2 * 3600));
  • Yes, if I withdraw location.href = rootUrl + "/options/?welcome=true"; of my javascript code Cookies appear, but if I let it redirects without Cookie created understood?

  • still not setting Cookies on the other page, PS: I am using friendly url.

  • Opa solved setcookie($key, $value, time() + (2 * 3600), '/');

1 answer

1


In the PHP manual of setcookie():

Way (path)

The path on the server on which the cookie will be available. If set to/, the cookie will be available throughout the domain. If set to '/ foo /', the cookie will only be available in the directory /foo/ and all subdirectories such as domain /foo/bar/ . The default value is the current directory in which the cookie is being configured.

You can do so, like you commented:

setcookie("TestCookie", "Value", time()+3600 , '/' );

Or even if you want to be available in the main domain and in any subdomain, provide the fifth parameter like this:

setcookie("TestCookie", "Value", time()+3600 , '/', '.example.com' );
  • solved like this setcookie($key, $value, time() + (2 * 3600), '/');, but I have another problem that I can not solve, I will open another question

  • @Willbb there is only one example, the name you define. rsrs

  • But it’s the same so I’ll mark it as solved

  • If you can also help me: https://answall.com/questions/217231/obtain-apenas-uma-part-da-string-de-cookies

Browser other questions tagged

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