FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Problem setting cookie





DanielXP
This is the code i have as.

Login.php in the directory 'modules'.
Code:
<?php
ob_start();
include("../config.php");
$username = htmlspecialchars(addslashes($_GET[username]));
$password = sha1(md5(sha1(sha1($_GET[password]))));
$info = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if(mysql_num_rows($info) == '0') {
//not found
}elseif($data[password] != $password) {
//wrong password
}elseif($data[rank] == '0') {
//banned account
}else{
setcookie("pnid", $data[id], time()+3600, '/');
setcookie("pnpassword", $data[password], time()+3600, '/');
include("../template/Defualt/passlogin.php");
}
?>

(config is in the directory before)
Now when it returns back to the main page of thes script (before the directory 'modules') it wont get any cookies.

Code:
<?php
echo $_COOKIE[pnid];
?>

Displays nothing.

Also i went into the modules directory and typed 'javascript:alert(document.cookie);' and it returned no set cookies.

I really need help as this has never happern to me before.

Thanks,
Daniel
kv
I am not sure if you copied the actual code, but if you did, here is the error

Code:
<?php
echo $_COOKIE[pnid]; //no quotes around pnid
?>


It should have been

Code:
<?php
echo $_COOKIE["pnid"];
?>


Also try this

Code:
setcookie("pnid", $data[id], time()+3600); //observe no fourth parameter


Since you are setting the cookie at root dir, it shouldn't matter.
DanielXP
It sets the cookies in the modules/ directory but not the one before.
DanielXP
the ", '/'" should be in there to allow it in all directorys?
manum
daniel xp is right i guess with that / thing....try it out it'll work
DanielXP
I have tryed it out.

Its what I did first but it didn't echo out the cookie on the dir before.
kv
The cookies are domain specific. If your script is in modules directory, and the you are accessing it as http://modules.<yourdomain>/login.php, then modules directory will be the root directory.

If that is not the case, and you are accessing it as http://<yourdomain>/modules/login.php, it should work.

You can also try using domain for setcookie, instead of the path.

Code:

setcookie(<cookie name>, <value>, time()+3600, "/", ".example.com", 1);
DanielXP
Nope still not working.

I went into the folder 'modules' and type in 'javascript:alert(document.cookie)' in the address bar and it returned no cookies even in that folder.
kv
did you check if cookies are enabled in your browser?

Also, setcookie sends a header, which means that setcookie should be called before any output. You can check this by the return value of setcookie. It returns TRUE/FALSE.
DanielXP
kv wrote:
did you check if cookies are enabled in your browser?

Also, setcookie sends a header, which means that setcookie should be called before any output. You can check this by the return value of setcookie. It returns TRUE/FALSE.


1) Yes I need cookies (To be on this site and that stuff..)
2) Sorry dunno what you mean. Sad
mathiaus
I hope your using firefox. If so, download this extension and check exactly what cookies are being set. You may find that they are being set, just for the wrong domain, path etc. (filter for your domain)
http://addneditcookies.mozdev.org/

Post the results here Smile
kv
DanielXP wrote:


2) Sorry dunno what you mean. Sad


You should not have any echo statements or any function call which outputs any content before calling setcookie. If you are using echo statement before setcookie, it does not work. Try this. Put the setcookie in the first line of your file. Check if it works.
manum
i guess u shld check out the php website to learn cookie stuff generally a super global array $_COOKIE
m-productions
mathiaus wrote:
I hope your using firefox. If so, download this extension and check exactly what cookies are being set. You may find that they are being set, just for the wrong domain, path etc. (filter for your domain)
http://addneditcookies.mozdev.org/

Post the results here Smile


is there a version of this that works on the newst firefox? it wont install because it says its not compiled for this firefox
mathiaus
Yeah I had to find a new one. View Cookies
Related topics
Logout problem (Cookie?)
the simple guestbook
cookie problems?
one line securing of php pages (login of users) (user auth)
go to ipower.com
PHPNuke problem.
ANWR help please!
Problem with cookies
Home Server software
Domain Webalizer Stats
Should Hillary concede the nomination to Barack Obama?
problem setting up new domain
500 internal server error upon executing PHP file
Allegro or SDL?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.