Jump to content

How to store random value in a cookie?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
aaa2103

aaa2103

    Newbie

  • Members
  • Pip
  • 1 posts
Hello,

I am completely new to php development.

I have a php page where I am wanting to store a random value to a cookie. I have made the following code:-
---------------------------------------------------------------------------------------------------------
<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8" />

        <title></title>

    </head>

    <body>

        <form action="testCookies.php" method="post">

            <input type="submit" value="Test" id="btn_test" name="btn_test"/>

            </form>

        <?php 

        if (isset($_POST['btn_test'])) 

        {

         

         if(isset($_COOKIE["username"]))

         {

          

         }

         if(!isset($_COOKIE["username"]))

         {

        

         $my_RANDOM=rand();

        

         $expire=time()+10*5*1*0;

         setcookie("username", "$my_RANDOM", $expire);


         }

         print ($_COOKIE["username"]);

         

        }        

        

        ?>

    </body>

</html>
---------------------------------------------------------------------------------------------------------

But the above code is not at all working.

Please let me know how to make this thing done and oblige me thereby.

Thanks.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
The cookie has't been set yet. Setcookie sends i with the header, but when you try to read it can't be until next parsing when the cookie has been set in the browser
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
Something else i want to add the foollowing code:

setcookie("username", "$my_RANDOM", $expire);

if u want the send the var $my_RANDOM u must remove the quotes around it else it will just use the string called "$my_RANDOM" instead of the var u want to.

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

bleastan said:

Something else i want to add the foollowing code:
setcookie("username", "$my_RANDOM", $expire);

if u want the send the var $my_RANDOM u must remove the quotes around it else it will just use the string called "$my_RANDOM" instead of the var u want to.

Nope, that is totally false, because php parses strings with double quotes for variables. If you have written '$my_random' it would store the name.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
Microsuck

Microsuck

    Newbie

  • Members
  • PipPip
  • 14 posts
I could be wrong, but I believe the rand() function only generates a random integer only.

Again, I could be totally wrong.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

Microsuck said:

I could be wrong, but I believe the rand() function only generates a random integer only.

Again, I could be totally wrong.

Yes, you can divide it by the maximum (should be getrandmax()) to get an unfixed numeric between 0 and 1 inclusive. mt_rand is self seeding if I recall, and can produce more useful random numbers in the end over rand().
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users