Jump to content


Photo

problem with cookies


  • Please log in to reply
10 replies to this topic

#1 icon

icon
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 April 2006 - 08:25 PM

hi


i have this code:

<?

$user="username";
$pass="password";
$database="db";

$username=$_POST['username'];
$password=$_POST['password'];
$first=$_POST['first'];
$last=$_POST['last'];
$occupation=$_POST['occupation'];
$location=$_POST['location'];
$email=$_POST['email'];

$username = $_REQUEST['username'] ;
$password = $_REQUEST['password'] ;


if (empty($username) || empty($password)) {

include "validate_error.php";

} else {

mysql_connect(localhost,$user,$pass);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT id FROM user WHERE username='$username' AND password='$password'";

$result = mysql_query($query) or die ("Query failed at login verifcation.");

$worked = mysql_num_rows($result);


If ($worked == "") {
include "error_username.html";

} else {

setcookie("cookie", $username, ".domain.net", 0);

header('Location: [a href=\"http://www.domain.net/web/00,,111.111.html');\" target=\"_blank\"]http://www.domain.net/web/00,,111.111.html');[/a]

}

}
?>

but it say Warning: setcookie() expects parameter 3 to be long

why is this happening

thanks

#2 hadoob024

hadoob024
  • Members
  • PipPipPip
  • Advanced Member
  • 192 posts

Posted 13 April 2006 - 08:55 PM

Here's the function declaration for setcookie():
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )


Setcookie() expects an integer for the third parameter, but I think you called it using a string value as the third parameter.

#3 icon

icon
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 April 2006 - 10:16 PM

[!--quoteo(post=364570:date=Apr 13 2006, 03:55 PM:name=hadoob024)--][div class=\'quotetop\']QUOTE(hadoob024 @ Apr 13 2006, 03:55 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Here's the function declaration for setcookie():
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
Setcookie() expects an integer for the third parameter, but I think you called it using a string value as the third parameter.
[/quote]

thanks it ok now but now it aint setting the cookie

this is what i enter to solve the other problem

setcookie("cookie", $username, time(), "/", ".domain.net", 0);

but i looked in the cookie folder of the broswer and there aint a cookie there for my domain but if i run the test.php with this bit of code it sets the cookie

------test.php----------
<?
setcookie("time_visited", date('m/D/Y'), time()+14400, "/", ".domain.net", 0);
?>

any ideas

thanks :@

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 13 April 2006 - 10:22 PM

[!--quoteo(post=364599:date=Apr 13 2006, 05:16 PM:name=paulley12)--][div class=\'quotetop\']QUOTE(paulley12 @ Apr 13 2006, 05:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
setcookie("cookie", $username, time(), "/", ".domain.net", 0);

but i looked in the cookie folder of the broswer and there aint a cookie there for my domain but if i run the test.php with this bit of code it sets the cookie

------test.php----------
<?
setcookie("time_visited", date('m/D/Y'), time()+14400, "/", ".domain.net", 0);
?>

any ideas

thanks :@
[/quote]

you are setting the expiration to the current time. your browser will delete it the moment it's set. try adding the +14400 after time() -- that should work.

#5 icon

icon
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 April 2006 - 10:27 PM

[!--quoteo(post=364603:date=Apr 13 2006, 05:22 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Apr 13 2006, 05:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
you are setting the expiration to the current time. your browser will delete it the moment it's set. try adding the +14400 after time() -- that should work.
[/quote]

thanks , but what i want to do is have the cookie deleted when the browser closes

thanks for all ya help

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 13 April 2006 - 10:35 PM

[!--quoteo(post=364606:date=Apr 13 2006, 05:27 PM:name=paulley12)--][div class=\'quotetop\']QUOTE(paulley12 @ Apr 13 2006, 05:27 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thanks , but what i want to do is have the cookie deleted when the browser closes

thanks for all ya help
[/quote]

I can't remember exactly, so you'll have to experiment. I think it's two double quotes with nothing in the middle. If that doesn't work, try the word NULL -- but I don't think NULL works.

so:

setcookie("time_visited", date('m/D/Y'), "", "/", ".domain.net", 0);

or (not likely)

setcookie("time_visited", date('m/D/Y'), NULL, "/", ".domain.net", 0);

#7 icon

icon
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 April 2006 - 10:43 PM

[!--quoteo(post=364611:date=Apr 13 2006, 05:35 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Apr 13 2006, 05:35 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I can't remember exactly, so you'll have to experiment. I think it's two double quotes with nothing in the middle. If that doesn't work, try the word NULL -- but I don't think NULL works.

so:

setcookie("time_visited", date('m/D/Y'), "", "/", ".domain.net", 0);

or (not likely)

setcookie("time_visited", date('m/D/Y'), NULL, "/", ".domain.net", 0);
[/quote]

now it come up with the other problem

Warning: setcookie() expects parameter 3 to be long :@

#8 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 13 April 2006 - 10:56 PM

[!--quoteo(post=364615:date=Apr 13 2006, 05:43 PM:name=paulley12)--][div class=\'quotetop\']QUOTE(paulley12 @ Apr 13 2006, 05:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
now it come up with the other problem

Warning: setcookie() expects parameter 3 to be long :@
[/quote]

Just tested with NULL and it works... NULL is a value, so don't enclose it in quotes or anything. Just like this:

setcookie("time_visited", date('m/D/Y'), NULL, "/", ".domain.net", 0);

if it still gives you trouble, everything after name is optional. Worst case scenario, just put the name and the value -- leave the rest off.

setcookie("time_visited", date('m/D/Y'));

#9 hadoob024

hadoob024
  • Members
  • PipPipPip
  • Advanced Member
  • 192 posts

Posted 13 April 2006 - 11:38 PM

could there be a problem with the if/else logic? could it just not be getting to that part of the code, which is why no cookie is being set? does the header redirection work?

so it seems that if you just run the test script, where the only command is the setcookie() command, it works, right? i wonder if that header right after the setcookie() command is causing the cookie not to be set. just to test it, i'd try commenting out the header() and putting an exit() in there. if the cookie gets set, then you know what the problem is.

#10 icon

icon
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 14 April 2006 - 05:45 PM

[!--quoteo(post=364619:date=Apr 13 2006, 05:56 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Apr 13 2006, 05:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Just tested with NULL and it works... NULL is a value, so don't enclose it in quotes or anything. Just like this:

setcookie("time_visited", date('m/D/Y'), NULL, "/", ".domain.net", 0);

if it still gives you trouble, everything after name is optional. Worst case scenario, just put the name and the value -- leave the rest off.

setcookie("time_visited", date('m/D/Y'));
[/quote]


thanks mate it work great now, oh just one more thing how would i dispaly the username which i have logged in has

cheers

#11 icon

icon
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 15 April 2006 - 12:11 AM

dont worry i have sorted, thanks again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users