This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » Setup & design » redaing Cookies
expandshrink

redaing Cookies

redaing Cookies

Tuesday 22 July 2003 10:33:37 am - 10 replies

Hello,

Sorry, that I'm cross posting, but I don't know where this topic belongs to.

I'm running a script with eZp that needs to read some value from a cookie like this:

$var_A = $_COOKIE['var_A'];

This script works perfect standalone., but when I run it inside eZp the value of var_A is empty.

Is there a way to solve this problem?
Thank you in advance

Tuesday 22 July 2003 10:38:01 am

You should use the eZHTTPTool class to fetch variables. I would not recommend using cookies for variable storage, use session instead.

The global variables are not available in eZ publish modules. You need to fetch them from the global array directly
$GLOBALS["_COOKIE"]['var_A'];

--bård

Tuesday 22 July 2003 6:38:44 pm

Thank your for your reply Bard.

But I don't I understand what your're trying to tell me.
I have cookie with this entry:

member_id some_value
etc...

Reading at your reply I thought that I need to fetch the member_id value like this:

$member_id = $GLOBALS["_COOKIE"]['member_id'];

But this didn't work. So I went on to read SDK and there was some info about fetching variables with eZHTTPTool
So I tried this:

$http =& eZHTTPTool::instance();
$member_id = $GLOBALS["_COOKIE"]['member_id'];

without any luck. Can you explain me further how to do this?
Thank you very much.

Wednesday 23 July 2003 7:22:35 am

In my exploring to see how the GLOBAL variables work, I tried this:

$var = $GLOBALS["eZINIOverrideDirList"];
echo (" $var" ) ;

Just an example I took from the ezini.php file.
But then again the echo isn't showing me anything.

What's the problem do you think?

Modified on Wednesday 23 July 2003 7:24:10 am by John van der Boom

Wednesday 23 July 2003 9:42:22 am

The first question is where is the script run, in the index.php or trough a module or something else?

You could try this.

global $_COOKIE;
$var_A = $_COOKIE['var_A'];

you can also see what the $_COOKIE variable contains with
print( "<pre>" );
var_dump( $_COOKIE );
print( "</pre>" );

Maybe this documentation have some clues
http://www.php.net/manual/en/function.setcookie.php

Wednesday 23 July 2003 12:58:44 pm

I think this problem has to do with the way I'm calling the secript. Why do I think so, cause standalone the script works perfectly and with the:

global $_COOKIE;
print( "<pre>" );
var_dump( $_COOKIE );
print( "</pre>" );

I see all the variables in the cookie.

The script is being called in eZp like this:

{"http://localhost/scripts/myscript.php"|insert}

So the script is running trough the content module, I think....
Seems like this is giving me the cookie problem. Everything works perfect except the cookie calling.
In a previous reply Bard wrote this: "The global variables are not available in eZ publish modules."

Is this the problem? If yes, is there another way to solve this?
Because I don't know how to run the script in the index.php and still outputing it in the templates.

Thank your for your time and patient.

Modified on Wednesday 23 July 2003 10:30:57 pm by John van der Boom

Friday 25 July 2003 7:22:22 pm

Is it possible to make a module that make it possibel to set and read cookies?
Because I think I'll gonna try and program this as a module (if this is the correct term)

Saturday 26 July 2003 12:35:15 pm

add
global $_COOKIE;
to the top of your scripts, and they should work well

Saturday 26 July 2003 1:17:36 pm

Actually there's no need for a
global $_COOKIE;
I read trough the PHP documentation and some PHP variables are always available globally.

Secondly eZ publish does not touch cookies directly, the session system will set a cookie but all of this code is handled by PHP. So if the _COOKIE variable is empty there must be a configuration problem or the setcookie code is not run.

From the setcookie documentation:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Do you get some errors on your page, like HTTP headers already sent? (Debug must be enabled too see this)

Saturday 26 July 2003 4:27:00 pm

If that's the case, a simple solution is to enable the output buffer. This can be set in system global php.ini, or in apache config file.

Below is cutted from php.ini
; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = Off

Monday 28 July 2003 12:24:52 pm

I'm not having problem setting the cookie but getting the values from the cookie. The cookies are already set with setcookie(). Indeed $_cookies should always be available in the script. That's why I'm not getting why it won't work if I include the script with the operator {"http://localhost/scripts/myscript.php|insert"}

Since I can't change the php.ini settings (btw the output_buffering of mine is set to "no_value"blunk.gif Emoticon. I have to try something else.

I'm echoing the cookie vars in index.php :

$myvar = $_cookie['var_A'];
echo $myvar;

This is working fine.
Now I want this variable ($myvar) to be known in template is this possible?

How can I accomplish this?
Thanks so far for your help

Modified on Tuesday 29 July 2003 10:46:15 am by John van der Boom

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from