PHP / Javascript混合Cookie功能

I'm attempting to create a function that blends javascript in php for those nasty cookies. Here's one that works;

function CookieSet($name,$value){
  $exp=date('D, d M Y G:i:s e', strtotime('midnight + 1days'));
  print("<script language=\"javascript\" type=\"text/javascript\">");
  print("document.cookie = \"".$name."=".$value."; expires=".$exp.";\";");
  print("</script>");
}

I have a similar function using the same methodology to delete cookies (resets time to expired.) I need to get the cookie information returned in php in similar fashion. I've created the following function but can seem to get it to work - php doesn't like it. I'm sure it's in punctuation somewhere but can't seem to locate it.

function CookieGet($name) {
print("<script language='javascript' type='text/javascript'>");
print("var name = ".$name." + \"=\";");
print("var ca = document.cookie.split(';');");
print("for(var i=0; i<ca.length; i++) {");
    print("var c = ca[i];");
    print("while (c.charAt(0)==' ') c = c.substring(1);");
    print("if (c.indexOf(name) == 0) ".$str=." c.substring(name.length, c.length);");
print("}");
print("</script>");
return $str;
}

The overall program is used to deliver a widget to several sites. Using pure PHP cookie support, the cookie domain is set to the source domain. Creating the cookie via Javascript sets the cookie domain to the destination domain. I'm well aware that Javascript runs in the browser and PHP runs on the server-side. I'm trying to work around the cross-domain cookie problem. Yes, I've reviewed other related solutions. If it weren't for remote delivery, this wouldn't be an issue.

Any help is appreciated.

Your function for printing JS code for cookie deletion has a problem with one line Try changing this:

print("if (c.indexOf(name) == 0) ".$str=." c.substring(name.length, c.length);");

into this:

print("if (c.indexOf(name) == 0) " . $str . " c.substring(name.length, c.length);");

I deleted the = after $str, because that caused a parse error in PHP.

But since $str is empty and not doing anything you could also go with this:

print("if (c.indexOf(name) == 0) c.substring(name.length, c.length);");

Hope that helps.

Thi should be a comment, but it's a bit long.

I need to get the cookie information returned in php in similar fashion

I don't know what you mean. PHP writes html (and javascript, and CSS, and images and....). There is a mechanism for getting the cookies sent in an HTTP request ($_COOKIE) hence the objective seems very strange. There are other methods of getting the data back serverside (in a POST, in the query of a URL, as a custom header in an AJAX request....) but without knowing what you are trying to achieve and why you can't use a normal method it's rather hard to advise.

Your PHP function seems very confused. Its rather hard to infer what you expect it to do.

print("if (c.indexOf(name) == 0) ".$str=." c.substring(name.length, c.length);");
...
return $str;

$str is unset at the point where this line executes. You're assigning it the value " c.substring(name.length, c.length);" (and also returning the same string).

By generating javascript code from PHP code you have to debug both the PHP and Javascript code simultaneously to get a working solution. The right way to solve the problem is to implement functions (or objects) in javascript, and get them working properly in javascript (using the debugger, console, alerts, setting innerHTML etc). then invoke them from PHP by writing a very simple Javascript injecting any literals appropriately...

function CookieGet($name) {
   print "<script language='javascript' type='text/javascript'>
";
   print "jsGetCookie('$name');
"
   print "</script>
";
}