Hello,

I have a project hosted on a live server has a domain like: www.abc.com

On live domain:
I am unable to set cookies in IE (7, 8, 9, 10) - as it is explained in the code below - (cookies are enabled on the IE browser), but able to set cookies in Firefox, Chrome, Opera and Safari without any problem. The server time is correct (current time of Jordan)

On localhost:
Cookies are working perfect on all browsers!

My code explained exactly as below:

In the page: "localhost/test.php", I put the below code to read the content of show.php which resides in the live server:

 <?php foreach (file('') as $o) echo $o ?>

In the page

 function hookscript(url){
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = url || null;
        document.getElementsByTagName("head")[0].appendChild(s);
    }


    hookscript('');

In the page

 function hookscript(url){
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = url || null;
        document.getElementsByTagName("head")[0].appendChild(s);
    }

    <?php
    $part = $_GET['part'];
    ?>

    <?php
    if($part==1){
    ?>

        <?php setcookie('test',time(),time()+3600) ?>
        hookscript('');

    <?php
    }

    if($part==2){
        if(isset($_COOKIE['test'])){
    ?>
            alert('Cookies SET');
        <?php
        }
        else{
        ?>
            alert('Cookies NOT SET'); // WHY!

    <?php
        }
    }
    ?>

Would appreciate your hints! Thank you!

Dani AI

Generated

Short version: this is a cross-site / third-party cookie situation in IE. The page you serve from localhost pulls a script from abc.com, so any Set-Cookie from abc.com is a third‑party cookie for the browser. Modern browsers are permissive here, but Internet Explorer will block third‑party cookies unless the response meets its privacy heuristics (or the user has relaxed privacy settings). was right that this is a client-side/IE policy issue rather than a PHP bug.

How to diagnose

  • In IE use the F12 Network tool (or a proxy) and request http://abc.com/aj/ajax.php?part=1 directly and also while the page is served from localhost. Confirm the response actually includes a Set-Cookie header. If the header is present when requested directly but the cookie isn’t stored when loaded as a cross‑domain script, IE is blocking it as third‑party.
  • Make sure your PHP actually sends headers before any output (setcookie/header calls must occur before any body text).

Practical fixes to try

  • Ensure you use the exact same host in the script URL and the cookie domain (www vs non‑www mismatch is a common gotcha). If you want the cookie to apply to all subdomains use the domain parameter: '.abc.com'.
  • For IE you typically need to send a P3P compact policy header along with the Set-Cookie to satisfy its privacy checks. Example (send before any output):
header('P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
setcookie('test', 'value', time()+3600, '/', '.abc.com');

Alternatives and cautions

  • P3P is deprecated but still used by older IE versions — it’s a pragmatic workaround, not a privacy guarantee. Safer long‑term fixes: make the request same‑origin (host the wrapper on abc.com), use a hidden iframe served from abc.com to set the cookie, or perform server‑side proxying so the browser sees the response as first‑party. These approaches avoid relying on IE’s P3P quirks.

Recommended Answers

All 4 Replies

Member Avatar for Member #949455

I am unable to set cookies in IE (7, 8, 9, 10) - as it is explained in the code below - (cookies are enabled on the IE browser), but able to set cookies in Firefox, Chrome, Opera and Safari without any problem. The server time is correct (current time of Jordan)

This is nothing to do with PHP.

PHP can enable cookies on all browsers but in your situation it's more related to a browsers setting rather than code itself.

Check the cookie settings of the other browsers and if they're set to block all or empty on exit make sure all of them have the same setting.

There is a work around with IE version in regards with jQuery/javascript.

I don't think I seen or heard anyone has issue enabling cookies in php, in general.

This is an client-side issue rather a server-side issue.

Does that make sense?

Thanks LastMitch! Actually when I test the code on localhost instead of abc.com, it works fine in IE and all browsers. But, when I test it on abc.com, then it doesn't work in IE but works perfect on (FF, Chrome, Opera and Safari).

It is the first time to face such problem :( I don't find any setting to change in IE, everything seems fine. Could you recommend anything from your side please?

You can try the code on your server and see that. I also tried it on another server but same problem :(

Your help is greatly appreciated :)

Thank you and regards!

I am still unable to find an answer! Your help is greatly appreciated! Thank you!

Member Avatar for Member #949455

I am still unable to find an answer! Your help is greatly appreciated! Thank you!

Have you try putting the whole url?

hookscript('');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.