|
|
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
|
|
0
|
|
|
|
Reply
|
kkrolski (78)
|
2/26/2004 8:30:40 PM |
|
Ken wrote:
> 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
>
>
A javascript cookie to a PHP cooke? eh? a cookie is a cookie, as far as
I know.
~Cameron
|
|
0
|
|
|
|
Reply
|
foo33 (1360)
|
2/26/2004 8:40:24 PM
|
|
Ken wrote:
> I am confused about using data in a JavaScript cookie in a PHP file.
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 =--
|
|
0
|
|
|
|
Reply
|
hexkid2 (1017)
|
2/26/2004 9:23:34 PM
|
|
"Ken" <kkrolski@wi.rr.com> wrote in message news:<QHs%b.10446$QP.1124@twister.rdc-kc.rr.com>...
> 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
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($_COOKIE);
?>
</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($arrValues);
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.
|
|
0
|
|
|
|
Reply
|
shawn2738 (281)
|
2/27/2004 6:42:35 PM
|
|
|
3 Replies
28 Views
(page loaded in 0.76 seconds)
|
|
|
|
|
|
|
|
|