Cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Cookies

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why not set it with PHP?

setcookie()

Javascript can specify the path and domain values, but it's not overly reliable.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Cookies

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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).
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Cookies

Post 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
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Post the code, no one is scared of long code here
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Well I'm a little frightened, but I have my blanket today 8)
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Cookies

Post by user___ »

feyd | Please use

Code: Select all

,

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]

Code: Select all

$thevar = $_COOKIE['tmsms'];
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

,

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]
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Cookies

Post 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.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not receive a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Cookies`

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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=/";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

@user__:
Please do not bump your posts.
Post Reply