bob2006 Posted November 14, 2010 Share Posted November 14, 2010 i have never been able to get this to work but i am at it agian in this code the php is not making the cookie can any one tell me why <?php function getRandomString($length = 5) { $validCharacters = "abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ+-*#&@!?1234567890"; $validCharNumber = strlen($validCharacters); $result = ""; for ($i = 0; $i < $length; $i++) { $index = mt_rand(0, $validCharNumber - 1); $result .= $validCharacters[$index]; } return $result; } setcookie("code", "getRandomString()", 3600000); echo getRandomString(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/ Share on other sites More sharing options...
Pikachu2000 Posted November 14, 2010 Share Posted November 14, 2010 Unquote the function call in setcookie(). Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134066 Share on other sites More sharing options...
bob2006 Posted November 14, 2010 Author Share Posted November 14, 2010 That was not the only problem i was missing the time() in my expire lol all fix thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134125 Share on other sites More sharing options...
bob2006 Posted November 14, 2010 Author Share Posted November 14, 2010 i know this has no relation to this post but how can i do this function getRandomString($length = 75) { $validCharacters = "abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ+-*#&@!?1234567890"; $validCharNumber = strlen($validCharacters); $result = ""; for ($i = 0; $i < $length; $i++) { $index = mt_rand(0, $validCharNumber - 1); $result .= $validCharacters[$index]; } return $result; } $string= 'getRandomString'; $expire=time()+60*60*24; setcookie("code", getRandomString(), $expire); mysql_query("UPDATE admin SET cookie_code='$string' WHERE login='$username'")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134134 Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 how can you do what? Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134136 Share on other sites More sharing options...
bob2006 Posted November 14, 2010 Author Share Posted November 14, 2010 I am trying to update a variable in the databases called cookie_code with the function getRandomString mysql_query("UPDATE admin SET cookie_code='21121' WHERE login='$username'")or die(mysql_error()); but all it is putting in the databases is getRandomString() And not the string i know it is doing this because the Function is not a virable so i try make it a Variable $string= 'getRandomString'; and that did not help at all Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134138 Share on other sites More sharing options...
Pikachu2000 Posted November 14, 2010 Share Posted November 14, 2010 When you enclose it in quotes, it becomes a literal string. $string = getRandomString(); Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134141 Share on other sites More sharing options...
bob2006 Posted November 14, 2010 Author Share Posted November 14, 2010 it really that easy grr i did that because of the example here:http://php.net/manual/en/functions.variable-functions.php Thank you Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134144 Share on other sites More sharing options...
bob2006 Posted November 14, 2010 Author Share Posted November 14, 2010 Completed Code function getRandomString($length = 75) { $validCharacters = "abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ+-*#&@!?1234567890"; $validCharNumber = strlen($validCharacters); $result = ""; for ($i = 0; $i < $length; $i++) { $index = mt_rand(0, $validCharNumber - 1); $result .= $validCharacters[$index]; } return $result; } $string= getRandomString(); $expire=time()+60*60*24; setcookie("code", getRandomString(), $expire); mysql_query("UPDATE admin SET cookie_code='$string' WHERE login='$username'")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/218641-setcookie/#findComment-1134148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.