-1

I have 2 files: first website.html and the second is php.php

In php I have a function that gets cookie from website.html

How can I print the content that was insert in the form below that form without riderecting to php.php

*how can I use data that is stored in the cookie in the HTML website using only php and html

  • 2
    you can read the cookie by javascript see http://stackoverflow.com/questions/5639346/shortest-function-for-reading-a-cookie-in-javascript – Satish Sharma May 20 '14 at 10:11

2 Answers2

0

Given some reasonable assumptions: You can't.

HTML is not a programming language and has no way to access cookies or add them to the DOM.

The sensible thing to do is to use PHP. This is most easily achieved by using a .php file instead of a .html file (although you can configure your server to parse .html for PHP directives).

You could also use JavaScript (so long as you didn't set the cookie to be http_only, which is usually a sensible thing to do).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

You can read the cookie form javascript in html page.

Read Cookie by Name (JS)

function readCookie(name) {
    name += '=';
    for (var ca = document.cookie.split(/;\s*/), i = ca.length - 1; i >= 0; i--)
        if (!ca[i].indexOf(name))
            return ca[i].replace(name, '');
}

Read Cookie by Name (Jquery)

(function(){
    var cookies;

    function readCookie(name,c,C,i){
        if(cookies){ return cookies[name]; }

        c = document.cookie.split('; ');
        cookies = {};

        for(i=c.length-1; i>=0; i--){
           C = c[i].split('=');
           cookies[C[0]] = C[1];
        }

        return cookies[name];
    }

    window.readCookie = readCookie; // or expose it however you want
})();

Reference :

What is the shortest function for reading a cookie by name in JavaScript?

Community
  • 1
  • 1
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51