Unable to set cookie from PHP called by ASP Msxml2.ServerXMLHTTP.6.0 object

303 Views Asked by At

I'm calling a php script from an asp page. The problem is that the php script called in this way cannot set the cookie. Here are the 2 scripts:

setcookie.asp

<%
    Dim objXMLHTTP
    Set objXMLHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
    objXMLHTTP.Open "POST", "http://127.0.0.1/setcookie.php", False
    objXMLHTTP.Send
    if len(objXMLHTTP.responseText)>0 then response.write "Result: "&objXMLHTTP.responseText
    Set objXMLHTTP = Nothing
%>

setcookie.php

<?php
setcookie('mycookie', '12345', time() + (86400 * 30), "/");
echo 'ok';
?>

The two pages run under the same website, in the same folder. I tried with both "POST" and "GET" with no success. If I run the php directly, cookie is created.

Any advice? Thanks

1

There are 1 best solutions below

2
On BEST ANSWER

You're using server side code to call your php script. This means that the cookie will be created on the server, not on the user's machine. Even if you're using a development server on your own machine your browser probably won't know where to look for the cookie if it hasn't set it. You need to use client side code to to call your php page, you could look for a Javascript/Ajax solution or you could use a zero size iframe.

Alternatively you could learn how to use Classic ASP to set cookies.

Using Cookies in ASP