Send a cookie from PHP page to ASP.NET page (Yes, we have to do this)

If you have to maintain a bunch of PHP sites and also want to make them to access your newer ASP.NET sites, you need to pass information between them.  One way is to use cookie.

1. At the top of the PHP page, do
   ob_start();  //buffering the output, otherwise you can’t set cookie
2. setcookie("HotAir", $user, time()+3600, ‘/’);
3. In your asp.net page
            HttpCookie cookieHotAir = Request.Cookies.Get("HotAir");

            if (cookieHotAir == null)
            {
                Response.Write("No HotAir Cookie");
                Response.End();
            }
            string temp = cookieHotAir .Value as string;
            Response.Write("HotAir =" + temp);
            Response.End();

That’s it.  You got your hot air.
              
Similar questions often raised is how to pass variables between PHP and ASP.NET, here I found some solutions from a post:

You can’t share session variables between PHP and ASP.NET (I assume you mean ASP.NET when you speak of "Visual Basic").

Sessions in both PHP and ASP.NET are instantiated at the application
level; since they are different applications, you can’t share the
values between the two.

You could, in theory, save the session key from PHP and attempt to
retrieve it with ASP.NET and vice-versa. However, you’ll likely find
that there is significant corruption of the variables between the two,
especially as you attempt to "hand them off" repeatedly.

It’s far wiser to use a stated (written) method of storing variables. You can do it a number of ways:

1. Pass variables between pages via GET (querystring) or POST (submitted form).

2. Save the information in a cookie and get it that way. ASP and PHP
can read cookies set by one another provided the cookies are in the
same domain (for that matter, JavaScript can also read cookies set by
PHP and ASP.NET in the same domain).

3. Save the variables to a database, XML or flat file and read them back in for each page.

This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Send a cookie from PHP page to ASP.NET page (Yes, we have to do this)

  1. Nitish Kumar says:

    Nice article..I have a problem related to above article.
    I have an asp.net website and i have added a wordpress blog to it in a subdirectory.
    Now i want that once the user log in asp.net website he/she can visit the wordpress blog without the need to login again.

    I am able to pass the cookie between asp to wordpress with the username and password. The cookie is passed sucessfully.But the value in the cookie on further redirection within wordpress is deleted and replaced by “+” symbol. Also many extra cookies are generated.

Leave a comment