PHP reading of JAvascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    #1

    PHP reading of JAvascript

    I am new to PHP. But this seems like a terrific language. I have been
    using JavaScript up to now but it looks like I will be changing to PHP.
    Seems to have more potential.

    I am confused about using data in a JavaScript cookie in a PHP file.

    The JavaScript Cookie

    document.cookie = "order=" + escape("website = " + website + "*" + "sale = "
    + sale + "*" + "name = " + name + "*" + "addr1 = " + addr1 + "*" + "addr2 =
    " + addr2 + "*" + "city = " + city + "*" + "countycode = " + countycode +
    "*" +"state = " + state + "*" + "zip = " + zip + "*" + "mr = " + mr + "*" +
    "fname = " + fname + "*" + "lname = " + lname + "*" + "dept = " + dept + "*"
    + "phone = " + phone + "*" + "ext = " + ext + "*" + "email = " + email + "*"
    + "#");
    variable: website =

    value: website

    delimiter: *

    End of cookie: #

    Is there a command to convert a JavaScript cookie to a PHP cookie?
    I have tried to read the cookie in PHP with no success.

    $_COOKIE['order'];

    $_COOKIE['order='];

    How do I pick out the variables and values in PHP from the JavaScript
    cookie?

    Thanks for your help.

    Ken


  • Cameron

    #2
    Re: PHP reading of JAvascript

    Ken wrote:[color=blue]
    > I am new to PHP. But this seems like a terrific language. I have been
    > using JavaScript up to now but it looks like I will be changing to PHP.
    > Seems to have more potential.
    >
    > I am confused about using data in a JavaScript cookie in a PHP file.
    >
    > The JavaScript Cookie
    >
    > document.cookie = "order=" + escape("website = " + website + "*" + "sale = "
    > + sale + "*" + "name = " + name + "*" + "addr1 = " + addr1 + "*" + "addr2 =
    > " + addr2 + "*" + "city = " + city + "*" + "countycode = " + countycode +
    > "*" +"state = " + state + "*" + "zip = " + zip + "*" + "mr = " + mr + "*" +
    > "fname = " + fname + "*" + "lname = " + lname + "*" + "dept = " + dept + "*"
    > + "phone = " + phone + "*" + "ext = " + ext + "*" + "email = " + email + "*"
    > + "#");
    > variable: website =
    >
    > value: website
    >
    > delimiter: *
    >
    > End of cookie: #
    >
    > Is there a command to convert a JavaScript cookie to a PHP cookie?
    > I have tried to read the cookie in PHP with no success.
    >
    > $_COOKIE['order'];
    >
    > $_COOKIE['order='];
    >
    > How do I pick out the variables and values in PHP from the JavaScript
    > cookie?
    >
    > Thanks for your help.
    >
    > Ken
    >
    >[/color]

    A javascript cookie to a PHP cooke? eh? a cookie is a cookie, as far as
    I know.

    ~Cameron

    Comment

    • Pedro Graca

      #3
      Re: PHP reading of JAvascript

      Ken wrote:[color=blue]
      > I am confused about using data in a JavaScript cookie in a PHP file.[/color]

      I guess you need to get a better idea what a cookie is:
      A cookie is some data that the server asks [1] the client to save and
      send back to the server for every later request.

      [1] The server can ask the client with a HTTP header or
      by having the client run a JavaScript script.
      In either case the client may not accept the cookie.

      If the client accepts cookies and therefore sends them to the server for
      every request, the server can do different things based on that data.


      Summing up: on the server, with PHP, to use a cookie sent by the client
      we get the pieces of data from the $_COOKIE array. It does not matter
      /how/ the cookie was created!

      A "JavaScript cookie" exists, and (maybe) can be manipulated on the client.
      A "PHP cookie" exists on the server, and can be used to eg. format data.


      PS: my usual browsing is with JavaScript disabled, and session cookies
      enabled.
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Shawn Wilson

        #4
        Re: PHP reading of JAvascript

        "Ken" <kkrolski@wi.rr .com> wrote in message news:<QHs%b.104 46$QP.1124@twis ter.rdc-kc.rr.com>...[color=blue]
        > I am new to PHP. But this seems like a terrific language. I have been
        > using JavaScript up to now but it looks like I will be changing to PHP.
        > Seems to have more potential.
        >
        > I am confused about using data in a JavaScript cookie in a PHP file.
        >
        > The JavaScript Cookie
        >
        > document.cookie = "order=" + escape("website = " + website + "*" + "sale = "
        > + sale + "*" + "name = " + name + "*" + "addr1 = " + addr1 + "*" + "addr2 =
        > " + addr2 + "*" + "city = " + city + "*" + "countycode = " + countycode +
        > "*" +"state = " + state + "*" + "zip = " + zip + "*" + "mr = " + mr + "*" +
        > "fname = " + fname + "*" + "lname = " + lname + "*" + "dept = " + dept + "*"
        > + "phone = " + phone + "*" + "ext = " + ext + "*" + "email = " + email + "*"
        > + "#");
        > variable: website =
        >
        > value: website
        >
        > delimiter: *
        >
        > End of cookie: #
        >
        > Is there a command to convert a JavaScript cookie to a PHP cookie?
        > I have tried to read the cookie in PHP with no success.
        >
        > $_COOKIE['order'];
        >
        > $_COOKIE['order='];
        >
        > How do I pick out the variables and values in PHP from the JavaScript[/color]

        Try this:

        <html>
        <head>
        <script>
        document.cookie = "hello=foo; "
        document.cookie = "bob=hope";
        </script>
        </head>

        <body>
        <a href="#" onclick="alert( document.cookie );">TEST</a>
        <?PHP
        print_r($_COOKI E);
        ?>
        </body>
        </html>

        You'll have to reload the page once for PHP to be able to read the
        cookie (because PHP parses the page before Javascript sets the
        cookie). That should clear up any confusion you have about cookies.
        But if you can avoid Javascript and just rely on PHP, you should.

        If you're storing multiple fields in a single cookie and have
        delimiters, do something like:

        $arrValues = array();

        $arrFoo = preg_split("/\*/", $_COOKIE['cookiename']);
        foreach($arrFoo as $var){
        list($key,$val) = preg_split("/\=/", $var);
        $arrValues[$key] = $val;
        }

        print_r($arrVal ues);

        Though that's untested, so don't blame me if I screwed something up
        (I'm not sure the backslashes in the 2 preg_splits are required).
        Also, check php.net for the htmlentities, urlencode and urldecode
        functions.

        Comment

        Working...