How to get cookie value

Cookie creation

session_start();
$params = session_get_cookie_params();    
setcookie(session_name('USERNAME'),'HAMZA',1,
      isset($params['path']),
      isset($params['domain']),
      isset($params['secure']),
      isset($params['httponly']));

session_regenerate_id(true);
echo "COOKIE IS CREATED SUCCESSFULLY !";

Now retrieving cookie value

session_start();
$NAME=$_COOKIE['USERNAME'];
echo $_COOKIE["USERNAME"];

if(isset($NAME))
{
      if($NAME=='USERNAME')
      {
          echo "success";
      }
      else
     {
          echo "error";       
     }    
 }

Please help me!

Result

Why they create Auto random Value Like: u8omuum6c9pkngrg4843b3q9m3). But I want to get my original cookie value, which is "HAMZA" ?????

+9
source share
3 answers

This is a cookie format.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

so the first variable is the cookie name

you can read it using

$_COOKIE['YOUR COOKIE NAME'];
+16
source

The session_name function will give you a hash, which is usually your session identifier. It looks like you want to keep the username in the session, right? In this case, you should use the $ _SESSION array.

Code example:

setcookie($_SESSION['USERNAME'],'HAMZA',1,
      isset($params['path']),
      isset($params['domain']),
      isset($params['secure']),
      isset($params['httponly']));

And you can get it like this:

$myCookie = $_COOKIE[$_SESSION['USERNAME']];

, . $ _COOKIE ['USERNAME'] 'HAMZA', :

setcookie('USERNAME','HAMZA',1,
      isset($params['path']),
      isset($params['domain']),
      isset($params['secure']),
      isset($params['httponly']));

, $ NAME == 'USERNAME' , $ NAME == 'HAMZA':

$NAME=$_COOKIE['USERNAME'];
echo $_COOKIE['USERNAME'];

if(isset($NAME))
{
      if($NAME=='HAMZA')
      {
          echo "success";
      }
      else
     {
          echo "error";       
     }    
 }
+1

In set_cookie.php, we set cookies for the username and password using the setcookie () function.

<?php
    $username = "test user";
    setcookie('username', $username, time() + 60 * 60 * 24 * 30); // Cookie sets for 30 days
    $password = "test password";
    setcookie('password', $password, time() + 60 * 60 * 24 * 30); // Cookie sets for 30 days
    ?>
    <a href="/get_cookie.php">Click here</a> to get cookie values.

In get cookie.php, we first check for valid cookie values. If it is not, he says that “cookies have not been set”, otherwise he prints a success message.

<?php
    if ($_COOKIE['username'] == "" || $_COOKIE['password'] == "")
    {
    ?>
    No cookies were set.<br>
    <?php
    }
    else
    {
    ?>
    Your cookies were set:<br>
    Username cookie value: <b><?php echo $_COOKIE['username']; ?></b><br>
    Password cookie value: <b><?php echo $_COOKIE['password']; ?></b><br>
    <?php
    }
    ?>

You can also get help here https://www.etutorialspoint.com/index.php/61-set-and-get-cookie-values

0
source

All Articles