Asked  6 Months ago    Answers:  5   Viewed   262 times

Say I have a website called a.com, and when a specific page of this site is loaded, say page link, I like to set a cookie for another site called b.com, then redirect the user to b.com.

I mean, on load of a.com/link I want to set a cookie for b.com and redirect user to b.com.

I tested it, and browser actually received the cookie from a.com/link, but it didn't send that cookie on the redirection request to b.com. Is it normal?

Can we set cookies for other domains?

 Answers

94

You cannot set cookies for another domain. Allowing this would present an enormous security flaw.

You need to get b.com to set the cookie. If a.com redirect the user to b.com/setcookie.php?c=value

The setcookie script could contain the following to set the cookie and redirect to the correct page on b.com

<?php
    setcookie('a', $_GET['c']);
    header("Location: b.com/landingpage.php");
?>
Tuesday, June 1, 2021
 
Bono
answered 6 Months ago
30

You can do a relative redirect:

window.location.href = '../'; //one level up

or

window.location.href = '/path'; //relative to domain
Friday, June 11, 2021
 
twk
answered 6 Months ago
twk
80

You have to set them before anything is outputted

look there: How can I set, get and destroy cookies in Wordpress?

If you are using a theme in function.php

function set_new_cookie() {
    //setting your cookies there
}
add_action( 'init', 'set_new_cookie');

Your expiration date is 0 so you cookies will be deleted right away look at the php doc:

EDIT: From php.net:

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

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

You have to set it like this for example :

setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
Tuesday, September 21, 2021
 
Michał Turczyn
answered 2 Months ago
37

You should create a new manager for each instance by TCefCookieManagerRef.New(Path) and store the returned reference into some private field of your form. Then you need to return the stored reference to the Result parameter of the OnGetCookieManager event method:

type
  TForm1 = class(TForm)
    Chromium1: TChromium;
    procedure FormCreate(Sender: TObject);
    procedure Chromium1GetCookieManager(Sender: TObject;
      out Result: ICefCookieManager);
  private
    FCookieManager: ICefCookieManager2;
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  FCookieManager := TCefCookieManagerRef.New('C:UniquePathToTheCookieStorage');
end;

procedure TForm1.Chromium1GetCookieManager(Sender: TObject;
  out Result: ICefCookieManager);
begin
  Result := FCookieManager;
end;
Monday, October 18, 2021
 
keturn
answered 2 Months ago
85

I think you could do it this way:

...
int result = adminClasses.userfunctions.addNewUser(u);

if (result == 0) {
    request.getRequestDispatcher("signupSuccess.jsp").forward(request, response);
}
...

It will evaluate the result on the server side and then, depending on the result, will forward to a signupSuccess.jsp page – i.e. the URL will stay the same, but the response will include success page's evaluated content.

Wednesday, November 24, 2021
 
Kevin
answered 6 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share