Page 1 of 1
Cookies
Posted: Tue Jan 02, 2007 2:38 pm
by user___
Hi guys,
I am developing an application that uses cookies but for some hours I have been getting an error. I set the cookie by using a .swf file which calls a JavaScript function that sets the Cookie. Then I try to get it by using Php nd this is the point when I do not have it. When I check the browser properties it the cookie is there. When I call it from the index page og the portal it works too but when I try to call it by using a Php script placed in another file different from the index one I can't get it. I think that this may be caused by the path because when I check the browser I see the root(index) path. My question is how to solve the problem with the path(If this is problem). WhenI use the $GLOBALS var I do not have it either.
Posted: Tue Jan 02, 2007 2:53 pm
by feyd
Why not set it with PHP?
setcookie()
Javascript can specify the path and domain values, but it's not overly reliable.
Cookies
Posted: Tue Jan 02, 2007 3:01 pm
by user___
Yes, I have though about this but the problem is that I am not sure whther I can use Php in a .js file which is included in HTML. If it is possible I would appreciate it if you tell me how to do that.
Posted: Tue Jan 02, 2007 3:15 pm
by Kieran Huggins
I'd be willing to bet you're not using the $_COOKIE superglobal:
http://www.php.net/manual/en/reserved.v ... es.cookies
If you try that and still have an issue, post your javascript and php code (in appropriate bbcode tags).
Cookies
Posted: Tue Jan 02, 2007 3:30 pm
by user___
But I do. I use it. I am giving you the pseudo code because the real one is a very long one.
In Flash:
Simple HTML with a href="javascript:Function that sets the cookie"(As far as I have been testing until now it works)
JavaScript:
Here is the function called from the href above
function ...(id) {
Do some validation
Gets the id
Sets the cookie}//I used another JavaScript function to check whther the cookie is set and it is
Php:
//First do some validation
use
$somevar = $_COOKIE['the name of the cookie'];
//Validation does not make any problems
//I tried to make it wotk without them
Posted: Tue Jan 02, 2007 3:43 pm
by Z3RO21
Post the code, no one is scared of long code here
Posted: Tue Jan 02, 2007 3:49 pm
by Kieran Huggins
Well I'm a little frightened, but I have my blanket today

Cookies
Posted: Tue Jan 02, 2007 3:51 pm
by user___
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I will post only the core of it:
[syntax="javascript"] document.cookie = name + "=" +"; expires=Thu, 01-Jan-1970 00:00:01 GMT";
var when = new Date();
when.setTime(when.getTime() + 24 * 60 * 60 * 1000);
// 24 hours from now
when.setFullYear(when.getFullYear() + 1);
// One year from now
document.cookie=name+"="+escape(value)+
";expires="+when.toGMTString();
[/syntax]
This is a short one but only this matters with the very creation of the cookie. As I have already pointed the problem is that I do not get the cookie in the Php.[/quote]
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Cookies
Posted: Tue Jan 02, 2007 4:17 pm
by user___
Suggestions? I have tried by using setcookie() in the root file and it works but I want to know where the problem comes from.
Posted: Tue Jan 02, 2007 4:24 pm
by Kieran Huggins
Just checking: You do mean on subsequent page loads, right? The cookie will only be available the next time you load a page.
Cookies`
Posted: Tue Jan 02, 2007 4:31 pm
by user___
Unfortunately I have tried this too but it does not work.
Let me try to explain better if I could
I have an index.php file
in this file I include another file that uses some conditions and then by using Ajax on the html page(It is simple and has nothing to do with cookies that's why I have posted it) I display the cookie by calling the .php file that contains the Php code. The scripts breaks at the point of the displaying.
Posted: Tue Jan 02, 2007 4:51 pm
by Kieran Huggins
try:
Code: Select all
var when = new Date();
when.setTime(when.getTime() + 24 * 60 * 60 * 1000); // 24 hours from now
when.setFullYear(when.getFullYear() + 1); // a year after that? I guess, it's your code.
document.cookie = name + "=" + escape(value) + "; expires="+ when.toGMTString() + "; path=/";
Posted: Tue Jan 02, 2007 5:14 pm
by RobertGonzalez
@user__:
Please do not bump your posts.