PDA

View Full Version : php and miva once again


erichar11
08-05-06, 12:42 AM
So on my pages external to miva I run a php script which populates a bunch of photos. All works well for the most part. Now, on my miva pages I run the same script using OpenUI "call" method to execute the script. The script executes but has no data. The problem is that the script makes a call to get a php cookie I previously set. When run inside Miva, the script for some reason doesn't see the cookie set via php. I know the php cookie is there on the Miva page because I can see it using my cookie viewing tool. So how does one pass a cookie to Miva that is to be used inside a php script? Any thoughts!!

ILoveHostasaurus
08-05-06, 12:44 AM
I don't think that will work. The CALL tag in Merchant is effectively activing as a web browser and returning the source of the page, but a cookie is not served as part of the page data, it's part of the http header data which would not be passed through to the real client computer. The same is true in the opposite direction; the CALL will not pass cookies to the PHP that the client computer has sent in to Merchant with the request.

erichar11
08-05-06, 12:58 AM
I don't think that will work. The CALL tag in Merchant is effectively activing as a web browser and returning the source of the page, but a cookie is not served as part of the page data, it's part of the http header data which would not be passed through to the real client computer. The same is true in the opposite direction; the CALL will not pass cookies to the PHP that the client computer has sent in to Merchant with the request.

Thanks, do you think it would be possible to use javascript instead to retrieve the cookie rather than using a php call to retrieve the cookie? Or am I going to bump up against the same problem.

It's pretty essential to what I'm trying to do and it requires the use of a php script (or other programming languages) to access a library for a web services api.

Any suggestions or thoughts would be great. Would moving to Miva 5 help here?

ILoveHostasaurus
08-05-06, 01:00 AM
I think you might need to make your content into an iframe so it has access to get and set cookies.

erichar11
08-05-06, 02:05 AM
not sure I follow you so bare with me. I have never used iframe. So I'll do some research. But basically what your saying is wrap the contents of my php script in an iframe tag and I should be able to get the headers information with cookies. So in my code within miva I have the php script run within a td tag. I should add a iframe tag around the php script which should allow me to get the header info. I will give it a try.

Seems to me this is a big limitation of miva for people who want to wrap webservices into Miva. Is there an alternative solution or something better? Just trying to get some ideas. Thanks.

ILoveHostasaurus
08-05-06, 02:14 AM
Yeah, an iframe is embedded in a document using a url just like regular frames. You can add variables to the iframe url though since merchant is always going to run and output its content first, including processing those variables. So you can specify the source of your iframe to be /include.php?var=%VAR{g.blah}% or whatever you need to pass to it, but it will have full interaction with the client computer because of it being a frame and the client doing the actual request.

This limitation is the same for any template-based cart though, it's not unique to Merchant. For example, if you wanted to include a product page from Merchant in a PHP Nuke CMS system, you'd have to do something similar or the Merchant session id would never make it back to the client computer.

Bruce - PhosphorMedia
08-05-06, 02:34 AM
What might be the issue is that you need to eval the call statement. Not sure how OpenUI's version is working but, when you use the Mivascript version, MvCall does nothing until you eval the "callvalue" which will contain the content of the called page. Just for grins, try evaling g.callvalue right after the %CALL() statement...otherwise, do what David suggests.

erichar11
08-05-06, 03:21 AM
Hi bruce, not really familiar with openui so not sure what you mean by use eval. What code should I be using? Something like this:

%CALL(script goes here)%
%EVAL(g.callvalue)%

Can you provide an example?

Bruce - PhosphorMedia
08-05-06, 06:35 AM
nope, just %VAR(g.callvalue)%...but its a long shot...reading the somewhat terse description, it appears that the %$CALL()% function should actually display the resulting callvalue...

but then again, reading David's response, I think he has his finger on the problem. Its not the Call function, its that your script is looking for a s.http_cookie value which isn't available just by using MvCall. You could try:

%ASSIGN(g.ThisPath|'ThePathToPHP?'$g.session_id)%
%CALL(g.ThisPath|GET)%

but I'm not sure if OpenUI will parse that syntax.

erichar11
08-05-06, 06:14 PM
Bruce, I tried this but OUI didn't seem to recognize the g.thisPath|Get in the %CALL%.

I'm at a lost, why couldn't I just call
%CALL(http://www.somedomain.myscript.php?username="somevalue")%

In this case I could populate the username with let's say a value from a javascript function(which gets the cookie I need) and have myscript parse the querystring rather than trying to have the script read a cookie.

Brandon MUS
08-07-06, 10:21 PM
What function are you using to read the cookie in PHP? I have included PHP in my miva layouts before that read a cookie, but I had to use javascript to read the cookie.

Bruce - PhosphorMedia
08-07-06, 10:44 PM
Bruce, I tried this but OUI didn't seem to recognize the g.thisPath|Get in the %CALL%.

I'm at a lost, why couldn't I just call
%CALL(http://www.somedomain.myscript.php?username="somevalue")%

In this case I could populate the username with let's say a value from a javascript function(which gets the cookie I need) and have myscript parse the querystring rather than trying to have the script read a cookie.

Yes you could do it that way...what I was showing should have resulting in the same thing...OpenToken syntax is not heavily documented so some amount of trial and error is neccessary. g.thisPath should have contained the url plus the session id aurgument (essentially the cookie value).

erichar11
08-08-06, 04:37 AM
Brandon, in php I'm doing the following on my script page

$username = $_COOKIE['MyCookie'];
This doesn't seem to work because of the reasons mentioned above. My thought would be to use Javascript within the php script to get the cookie. But I'm still not sure whether a php script run from Miva (via %call("myscript.php") will execute Javascript. I tried a couple of times but it didn't work.

Do you have a simple example of using javascript within a php script to get cookies?

Thanks

Brandon MUS
08-08-06, 06:05 PM
unfortunately you can't use javascript "within" a PHP script. You can display the javascript on the page, but if you are hoping to use server side functions with the results from a javascript script on a page, I don't believe you can (or at least I have never done it). You are asking for a server side script (PHP) to work with data on the client side (JS). The JS values are dependent on the user and tbe browser, where the PHP values are determined before it can even send the request to the user.

My only suggestion is to try another PHP function: http_parse_cookie() (http://us3.php.net/manual/en/function.http-parse-cookie.php), or try handling all cookie related data in Javascript.

I have used a %CALL(xx.php)% that has a javascript script that reads and displays cookie information.

Bruce - PhosphorMedia
08-08-06, 06:48 PM
I have used a %CALL(xx.php)% that has a javascript script that reads and displays cookie information.


So, if you can forward the cookie data in an URL argument with the %CALL()% function, why not just use the miva scipt system variable s.http_cookie?

Brandon MUS
08-08-06, 08:44 PM
I didn't forward the information, but I think it would be a possibility. My CALL() was a direct URI.

Bruce - PhosphorMedia
08-09-06, 12:21 AM
don't have time to test it, but can the url in the %call() statement be a variable...i see that the example shows it as a literal, but not enclosed...so from that, one would actually assume that the Call function cannot take a variable.

erichar11
08-09-06, 05:21 AM
Hi Brandon, can you give me a quick example of how you used javascript to gain access to the cookie. Thanks.

Brandon MUS
08-09-06, 06:08 PM
Sure. Here's a complete script:
//Set cookie
function setCookie(name, value, expires, path, domain, secure) {
var today = new Date();
today.setTime(today.getTime());
if (expires) expires = expires * 1000 * 60 * 60 * 24;

var expires_date = new Date(today.getTime() + (expires));
document.cookie = name+"="+escape(value) +
((expires) ? ";expires="+expires_date.toGMTString() : "") + //expires.toGMTString()
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

//Get cookie
function getCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf( ";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}

//Delete cookie
function deleteCookie(name, path, domain) {
if (getCookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//Create cookies
shippingName = "%VAR(Customers.d.ship_fname)%";
billingName = "%VAR(Customers.d.bill_fname)%";
var expireDate = new Date((new Date()).getTime() + 24 * 3600 * 1000);

if (shippingName != "") {
custName = shippingName;
} else {
custName = billingName;
}
deleteCookie("custName", "/", "");
setCookie("custName", escape(custName), expireDate.toGMTString(), "/", "", "");

//read
var strCustName = unescape(getCookie("custName", "/", ""));

erichar11
08-09-06, 09:32 PM
Hi Brandon, thanks for the help and the code. I have a similar js file for getting and setting cookies. In the code you mentioned above:

"var strCustName = unescape(getCookie("..........."));

is that being placed inside your php file. I tried something similar to this but couldn't get it to work. Or are you passing the cookie value to your php script via a variable: In other words:

call(myscript.php?var = strCustomerName)

Once again thanks for the help as between miva, php, and javascript, I'm a little confused.

Eric

Brandon MUS
08-09-06, 11:37 PM
No, the script I gave you is only for javascript. It is unable to function with php because of the client-side/server-side conflict.

I have the call as simply %CALL("myscript.php")%

If you want to interact w/ the data on the server-side, you will need to read the cookies in php ($_COOKIES). Our theory so far in this thread is it can't be done because CALL() doesn't pass the HTTP header information. If you can get the %CALL("myscript.php?var=Customer Name")% to work, that may be the only way left for you.